Python 1
Python 1
What is Programming
Programming is a way for us to tell computers what to do. Computer is a very dumb machine and it only
does what we tell it to do. Hence we learn programming and tell computers to do what we are very slow
at - computation. If I ask you to calculate 5+6, you will immediately say 11. How about 23453453 X
56456?
You will start searching for a calculator or jump to a new tab to calculate the same. This 100 days of
code series will help you learn python from starting to the end. We will start from 0 and by the time we
end this course, I promise you will be a Job ready Python developer!
What is Python?
Python is a dynamically typed, general purpose programming language that supports an object-
oriented programming approach as well as a functional programming approach.
Python is an interpreted and a high-level programming language.
It was created by Guido Van Rossum in 1989.
Features of Python
Python is simple and easy to understand.
It is Interpreted and platform-independent which makes debugging very easy.
Python is an open-source programming language.
Python provides very big library support. Some of the popular libraries include NumPy,
Tensorflow, Selenium, OpenCV, etc.
It is possible to integrate other programming languages within python.
1
Jadav Dharmesh
2 - Modules and pip in Python!
Module is like a code library which can be used to borrow code written by somebody else in our python program.
There are two types of modules in python:
Built in Modules - These modules are ready to import and use and ships with the python interpreter. there is no need to
install such modules explicitly.
External Modules - These modules are imported from a third party file or can be installed using a package manager like
pip or conda. Since this code is written by someone else, we can install different versions of a same module with time.
import pandas
Similarly we can install other modules and look into their documentations for usage instructions.
We will find ourselved doing this often in the later part of this course
import pandas
Example
print("Hello World", 7)
print(5)
print("Bye")
print(17*13)
Python Comments
3
Jadav Dharmesh
A comment is a part of the coding file that the programmer does not want to execute, rather the programmer
uses it to either explain a block of code or to avoid the execution of a specific part of code while testing.
Single-Line Comments:
To write a comment just add a ‘#’ at the start of the line.
Example 1
Output:
This is a print statement.
Example 2
Output:
Hello World !!!
Example 3:
print("Python Program")
#print("Python Program")
Output:
Python Program
Multi-Line Comments:
To write multi-line comments you can use ‘#’ at each line or you can use the multiline string.
#If the condition is false then it will execute another block of code.
4
Jadav Dharmesh
p=7
if (p > 5):
else:
Output:
p is greater than 5.
p=7
if (p > 5):
else:
Output
p is greater than 5.
5
Jadav Dharmesh
More on Print statement
The syntax of a print statement looks something like this:
a=1
b = True
c = "Harry"
d = None
a=1
print(type(a))
b = "1"
6
Jadav Dharmesh
print(type(b))
complex: 6 + 2i
3. Boolean data:
Boolean data consists of values True or False.
Example:
list1 = [8, 2.3, [-4, 5], ["apple", "banana"]]
print(list1)
Output:
[8, 2.3, [-4, 5], ['apple', 'banana']]
Tuple: A tuple is an ordered collection of data with elements separated by a comma and enclosed within
parentheses. Tuples are immutable and can not be modified after creation.
Example:
tuple1 = (("parrot", "sparrow"), ("Lion", "Tiger"))
print(tuple1)
Output:
7
Jadav Dharmesh
(('parrot', 'sparrow'), ('Lion', 'Tiger'))
Example:
dict1 = {"name":"Sakshi", "age":20, "canVote":True}
print(dict1)
Output:
{'name': 'Sakshi', 'age': 20, 'canVote': True}
Example Program
a = complex(8, 2)
b = True
c = "Harry"
d = None
print(a)
print(b)
a1 = 9
print(a + a1)
print("The type of a is ", type(a))
print("The type of b is ", type(b))
print("The type of c is ", type(c))
list1 = [8, 2.3, [-4, 5], ["apple", "banana"]]
print(list1)
tuple1 = (("parrot", "sparrow"), ("Lion", "Tiger"))
print(tuple1)
dict1 = {"name":"Sakshi", "age":20, "canVote":True}
print(dict1)
7 - Operators
Python has different types of operators for different operations. To create a calculator we require
arithmetic operators.
Arithmetic operators
Operator Operator Name Example
+ Addition 15+7
8
Jadav Dharmesh
Operator Operator Name Example
- Subtraction 15-7
* Multiplication 5*7
** Exponential 5**3
/ Division 5/3
% Modulus 15%7
// Floor Division 15//7
Exercise
n = 15
m=7
ans1 = n+m
print("Addition of",n,"and",m,"is", ans1)
ans2 = n-m
print("Subtraction of",n,"and",m,"is", ans2)
ans3 = n*m
print("Multiplication of",n,"and",m,"is", ans3)
ans4 = n/m
print("Division of",n,"and",m,"is", ans4)
ans5 = n%m
print("Modulus of",n,"and",m,"is", ans5)
ans6 = n//m
print("Floor Division of",n,"and",m,"is", ans6)
Explaination
Here 'n' and 'm' are two variables in which the integer value is being stored. Variables 'ans1' ,
'ans2' ,'ans3', 'ans4','ans5' and 'ans6' contains the outputs corresponding to addition,
subtraction,multiplication, division, modulus and floor division respectively.
a = 50
b=3
9 - Typecasting in python
The conversion of one data type into the other data type is known as type casting in python or type conversion in
python.
9
Jadav Dharmesh
Python supports a wide variety of functions or methods like: int(), float(), str(), ord(), hex(), oct(), tuple(), set(),
list(), dict(), etc. for the type casting in python.
Explicit typecasting:
The conversion of one data type into another data type, done via developer or programmer's intervention or
manually as per the requirement, is known as explicit type conversion.
It can be achieved with the help of Python’s built-in type conversion functions such as int(), float(), hex(), oct(),
str(), etc .
number = 7
Output:
The Sum of both the numbers is 22
Python converts a smaller data type to a higher data type to prevent data loss.
10
Jadav Dharmesh
# Python automatically converts c to float as it is a float addition
c=a+b
print(c)
print(type(c))
Ouput:
<class 'int'>
<class 'float'>
10.0
<class 'float'>
Syntax:
variable=input()
But input function returns the value as string. Hence we have to typecast them whenever required to another datatype.
Example:
variable=int(input())
variable=float(input())
We can also display a text using input function. This will make input() function take user input and display a message as
well
Example:
a=input("Enter the name: ")
print(a)
Output:
Enter the name: Harry
Harry
11
Jadav Dharmesh