Python - Revision Tour
Python - Revision Tour
com
Revision Tour
Control Flow Statements in Python
Empty Statement
Simple Statement
Compound Statement
Compound_Statement_Header :
indented_body containing multiple
simple or compound statement
if Statement of Python
‘if’ statement of python is used to execute
statements based on condition. It tests the
condition and if the condition is true it
perform certain action, we can also provide
action for false situation.
if statement in Python is of many forms:
if without false statement
if with else
if with elif
Nested if
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Simple “if”
In the simplest form if statement in Python
checks the condition and execute the
statement if the condition is true and do
nothing if the condition is false.
Syntax: All statement
belonging to if
if condition: must have same
indentation level
Statement1
Statements ….
** if statement is compound statement having
header and a body containing intended
statement.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
bonus = 0
sale = int(input("Enter Monthly Sales :"))
if sale>50000:
bonus=sale * 10 /100
print("Bonus = " + str(bonus))
if with else
if with elif
if with elif is used where multiple chain of condition is to
be checked. Each elif must be followed by condition: and
then statement for it. After every elif we can give else
which will be executed if all the condition evaluates to
false
Syntax:
if condition:
Statements
elif condition:
Statements
elif condition:
Statements
else:
Statement
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Nested if
In this type of “if” we put if within another if as a
statement of it. Mostly used in a situation where we
want different else for each condition. Syntax:
if condition1:
if condition2:
statements
else:
statements
elif condition3:
statements
else:
statements
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Storing Condition
range() function
range() function
For e.g.
range(1,10) will generate set of values from
1-9
range(0,7) will generate [0-6]
Default step value will be +1 i.e.
range(1,10) means (1,2,3,4,5,6,7,8,9)
range() function
range() function
>>>’a’ in ‘apple’
True
>>>’national’ in ‘international’
True
for loop
School=["Principal","PGT","TGT","PRT"]
for sm in School:
print(sm)
for ch in ‘Plan’:
print(ch)
for i in range(1,101):
print(i,end='\t')
while loop
i=1
while i<=10:
print(i)
i+=1
Example – break
for i in range(1,20):
if i % 6 == 0:
break
print(i)
print(“Loop Over”)
Example – continue
for i in range(1,20):
if i % 6 == 0:
continue
print(i, end = ‘ ‘)
print(“Loop Over”)
i=1 Output
1
while i<=10: 2
3
print(i) 4
5
i+=1 6
7
8
else: 9
10
print("Loop Over") Loop Over
STRING MANIPULATION
String operators
MEMBERSHIP OPERATORS
Membership operators (in and not in) are used
to check the presence of character(s) in any
string.
Example Output
‘a’ in ‘python’ False
‘a’ in ‘java’ True
‘per’ in ‘operators’ True
‘men’ in ‘membership’ False
‘Man’ in ‘manipulation’ False
‘Pre’ not in ‘presence’ True
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Comparison operators
We can apply comparison operators (==,
!=,>,<,>=,<=) on string. Comparison will be
character by character.
str1=„program‟
Comparison of
str2=„python‟ string will be
str3=„Python‟ based on ASCII
code of the
characters
Example Output
str1==str2 False
Characters Ordinal/
str1!=str2 True
ASCII code
str2==„pyth True A-Z 65-90
on‟
a-z 97-122
str2>str3 True
0-9 48-57
str3<str1 True
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
DETERMINING ORDINAL /
UNICODE OF A SINGLE
CHARACTER
Python allows us to find out the ordinal position
single character using ord() function.
String slicing
>>> str1="wonderful" >>> str1[3:3]
>>> str1[0:6] ''
'wonder' >>> str1[3:4]
>>> str1[0:3] 'd'
'won' >>> str1[-5:-2]
>>> str1[3:6] 'erf'
'der' >>> str1[:-2]
>>> str1[-1:-3] 'wonderf'
'' >>> str1[:4]
>>> str1[-3:-1] 'wond‘
'fu' >>> str1[-3:] Reverse
>>> str1[-3:0] 'ful‘ of string
'‘ >>>str1[::-1]
>>>str1[0::2] lufrednow
‘Wnefl’
Str.find(Sub)
0
Str.find(Sub,1)
6
Str.find(‘y’,6,11)
10
Str.find(‘pinky’)
-1
String.isalnum() Return True if the string is ‘hello123’.isalnum()
alphanumeric character True
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Python string manipulation
function
Function name Purpose Example
String.isalnum() S=“[email protected]
S.Isalnum()
False
String.isalpha() Return True if string Str1=“hello”
contains only alphabets Str2=“hello123”
characters Str1.isalpha()
True
Str2.isalpha()
False
String.isdigit() Return True if string s1=“123”
contains only digits s1.isdigit()
True
Str2.isdigit()
False
String.islower() Return True if all Str1.islower()
character in string is in True
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & lower case
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Python string manipulation
function
Function name Purpose Example
String.isspace() Return True if only S=“ “
whitespace characters in S.isspace()
the string True
S2=‘’
S2.isspace()
False
String.upper() Return True if all S=“HELLO”
characters in the string S.isupper()
are in upper case True
String.lower() Return copy of string S=“INDIA”
converted to lower case S.lower()
characters india
String.upper() Return copy of string S=“india”
converted to upper case S.lower()
characters INDIA