Python 5
Python 5
Session 5
What will you learn ?
• Introduction • Literals
• Introduction to Data Sets • Conditionals
• Concepts of Variables, Iterations & • Loops
Filtering • Functions
• Data Types • Lists & Tuples
• Sanity of Data • Sets & Dictionaries
• Introduction to Complex Data Types • File Handling
• Hands – On Python • Object Oriented Programming
Summary till date
Datatypes
In Python every entity is
an object including all Variables
datatypes
No need to declare variables in Python. It is
Dictionary Set Boolean created when the value is assigned to it
Numeric Sequence
dict() set() bool() first time. Python allows dynamic typing.
Variable name is any sequence of
Integer String characters a-z, A-Z, 0-9 and underscore but
int() str() not starting with numbers. Variables are
case sensitive.
Float List
float() list()
Complex Tuple
complex() tuple()
Summary Till Date - Operators
Operators Symbol / Keyword Name / Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Arithmetic // Floor division
** Exponential
== Double equal to
!= Not equal to
> Greater than
< Less than
Comparison >= Greater than equal to
<= Less than equal to
and Returns True if both statements are true
or Returns False if one of the statements are True
Logical not Reverse the result
in Returns True if the value is present in the sequence
Membership not in Returns True if the value is not present in the sequence
is Returns True if both variables point to the same object
Identity is not Returns True if both variable do not point to the same object
Escape characters
import math
It will import the math library
from math import *
It will import the entire contents of math library
from math import pi
It will import only variable pi from math library
import calendar as cal
It will import the calendar library and it can be accessed as cal
from calendar import month as m
It will import only month method from calendar library and it
can be accessed as m
Conditional Statements
if block:
The goal of this statement is to check if the given condition is
True or False.
If it is True, then Python will execute the following indented
lines of code.
i f a > b:
p r i n t ( ' a i s greater than b ' )
If Example
if – elif blocks:
elif stands for else if.
This is Python’s way of saying, if the first condition is False
then it will check the next condition.
i f a > b:
p r i n t ( ' a i s greater than b ' )
e l i f a < b:
p r i n t ( ' a i s less than b ' )
If Else, Elif
Problems
1 4 Even
2 6 Even
3 1931 Odd
4 -45 Odd
5 44 Even
if – elif blocks
i f a > b:
p r i n t ( ' a i s greater than
e l i bf ' a) < b:
p r i n t ( ' a i s less than b ' )
else:
p r i n t ( ' a and b are equal')
Nested Conditional Statements
But sometimes there may arise a condition where we may want to terminate the loop, skip an iteration or ignore
that condition. In such conditions we use loop control statements like break, continue and pass respectively.
Range
i =0
while i < 10:
i += 1
1. Using range():
for i in range(10):
pr i nt ( i )
This code is equivalent to the above while loop and it will also print numbers from 0 to 9
while loop:
It enables you to execute set of statements as long as the given
condition is True.
i =0
while i < 10:
pr i nt ( i )
i += 1
This code will print numbers from 0 to 9.
Resources
• https://drive.google.com/drive/my-drive
• https://www.python.org/
• https://www.python.org/psf/
• http://www.replit.com/
• https://docs.replit.com/tutorials/introduction-to-the-repl-it-ide