PYTHON
BY: NAVDEEP PUBLICATIONS
Uses of Python
To create web applications.
To develop software programs.
To connect to a database system for file
management.
To be used along side software to create
workflow.
To handle a large amount of data and perform
complex mathematical calculations.
Features of Python
Easy to Learn and Use
Easy to Read
Interpreted Language
Cross-Platform Language
Free and Open Source Software
Object Oriented Language
Case Sensitive Language
Downloading Python
Type the following URL in the address bar
https://www.python.org/
Click on Download Python 3.7.0
The Python exe file gets downloaded.
Click on the exe file and the installation will start.
PARTS of IDLE Shell Window
Keywords
These are the reserved words that cannot be used as a variable
name, function name or an identifier name. The are used to
define the syntax and structure of the Python language.
Eg:- break, if, print
Note:- Keywords are case sensitive and are written in lower
case
Identifiers
It is the name given to an entity such as class, function, variable
or module. It helps to differentiate one entity from another.
Rules for Writing Identifiers
An identifier name contains uppercase (A to Z) or
lowercase (a to z) letters, digits(0 to 9) or an
underscore( _ ). Eg:- VOLUME , Length12 , a_s2
An identifier cannot start with a digit. Eg:- 12area
Keywords cannot be used an identifiers.
Identifiers donot contain special symbols such as !,
@, #, $, % etc; Eg: b@123
Identifier names are case sensitive. Eg:- sum and
SUM are not the same.
Line Indentation
In Python line indentation is used to define a group of
statements or a block of code. We can give one, two,
three or four whitespaces to define a code block.
Eg:-
>>>if(a>b):
print(“a is greater”)
else:
print(“b is greater”)
Comments
A comment is a text string that is ignored by a
compiler or an interpreter and is not executed with the
program code. # symbol is used to write a comment.
For Eg:-
>>> # Use of Print Command
>>>print(“I am learning Python”)
Statements
These are the instructions executed by the interpreter. A
statement ends with a new line. The statements can be
extended by using line continuation( \ ) character. We
can use parenthesis (), braces {} and square brackets []
to make multiline statements.
x=10+20+30+\ x=(10+20+30+ month_names=
40+50+60+\ 40+50+60+ [‘January’,’Februa
70+80+90 70+80+90) ry’,’March’,’April]
Multiple Statements
We can write multiple statements in a single line by
using the semi-colon(;) symbol.
For Eg: - a=10; b=20; c=“Britannica”
For Eg: - a,b=15,5
>>>a=15;b=25;c=35 >>>x1,x2.x3=2,4,5
>>>print(a+b+c) >>>print(x1*x2*x3)
75 40
Multiple Statements
Shows an ERROR in the program if the variable on the
left side differs from the variables in the right hand side
and the exception is raised and the program stops.
>>> x1,x2=2,4
>>> print(x1+x3)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
print(x1+x3)
NameError: name 'x3' is not defined
Variables
It is a named memory location which is used to store a
data value. The data value stored in the variable can
change as many times the user wants during the execution
of the program. Each variable in program has an unique
name called as the identifier.
For Eg: We can use single English character as a variable
name. a, x, t etc;
We can use descriptive variable names such as grade, area
Variables in Python
In Python the declaration of the variable is done
automatically. This means we do not need to explicitly
declare the data type for variables. We can create a
variable by simply assigning a value to the variable.
Equal to (=) sign is used to assign a value to a variable.
For Eg:-
y=p # creates an integer type of variable
a=11.5 # creates a float type of variable
Name=“Gauri” # creates a string type of variable
Data Types
The type of data which a variable can store is called a
data type. It can be an integer, float and a string type of
value. For Eg:-
int x # it will accept an integer value which can be a
positive value, negative value or a whole number.)
float y # it will accept a decimal value which can be a
positive value or a negative value.)
x1,x2=5,-3 # x1 will store a positive value and x2 will
store a negative value.
PROGRAM TO SHOW THE USE OF
OUTPUT OF THE PROGRAM
DATA TYPES
PRINT command
The PRINT command in Python is used to display the output of
the program.
Ways to use PRINT command
print(value)
For Eg: print(9)
print(variable name)
For Eg:
y=12
print(y)
print(“The value of y is=“,y)
How to write a program in Python
Open Python IDLE 3.7.0
Click on File Menu and choose New option.
Type the program.
Save the program.
Click on Run Menu and choose Run Module or Press
F5.
The Python 3.7.0 Shell window will open and you
can see the output of the program.
Program to add two numbers
num1=65 # num1 is assigned an integer value
num2=100 # num2 is assigned an integer value
sum=num1+num2 # adding two values
print(sum) # to display the output of the program
Program to add two numbers with different data
types
num1=14.7 # num1 is assigned decimal value ie. Float data type
num2=23 # num2 is assigned an integer value
sum=num1+num2 # adding two values
print(sum) # to display the output of the program
Program to find the area of rectangle
area of rectangle = length * breadth
l=12 # length of rectangle
b=50.6 # breadth of rectangle
a=l*b # finding the area of rectangle
print(“The area of rectangle is=“,a) #displaying the
result of area of rectangle
Output
The area of rectangle is=607.2
Program to find the simple interest
Simple Interest=(p*r*t)/100
p=2000 # principal
r=0.05 # rate of interest per annum
t=2 # time period
si=(p*r*t)/100 # calculating simple interest
print(“Simple Interest=“,si) #displaying the result of simple
interest
Output
Simple Interest=2
Practice Questions
1. Write a program to display the message ‘I AM LEARNING
PYTHON PROGRAMMING’.
2. Take two variables a and b. Assign them the values as 45
and 72. Write a program to display the value of a and b.
3. To display the product of three value.
4. To find the perimeter of a square.
5. To find the perimeter of a rectangle.
6. To find the simple interest where principal=Rs. 4500, Rate
of Interest=4%, Time Period=4years
7. To find the area of rectangle.(a=1/2*b*h).