Material
Material
Compound_Statement_Header :
indented_body containing multiple
simple or compound statement
Compound Statement has:
▪ 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
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')
i=1
while i<=10:
print(i)
i+=1
Jump Statements – break & continue
i=1 else:
while i<=10: print("
print(i)
i+=1
Example (“else” with for)
names=["prayagraj","lucknow","indore","kanpur","agra","ghaziabad"
,"mathura","bhopal"]
city = input("Enter city to search: ")
for c in names:
if c == city:
print(“City Found")
break Output
else: Enter city to search : indore
City Found
print("Not found") Enter city to search : bhopal
Not found
Generating Random numbers
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
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
DETERMINING ORDINAL /
UNICODE OF A SINGLE
CHARACTER
Python allows us to find out the ordinal position
single character using ord() function.
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
Python string manipulation
function
Function name Purpose Example
String.isalnum() S=“ravi@gmail.com
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
lower case
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
Python string function
Function name Purpose Example
String.lstrip([chars]) Returns a copy of string with string=" hello"
leading characters removed. string.lstrip()
If no characters are passed then it 'hello'
removes whitespace string2="National"
It removes all combination of given string2.lstrip('nat')
characters i.e. if we pass “The” National
then its combination – The, Teh, string2.lstrip('Nat')
heT, ehT, he, eh, h, e, etc will be ional
removed string2.lstrip('at')
National
string2.lstrip('Na')
tional
string2.lstrip('atN')
ional
String.rstrip([chars]) Returns a copy of string with “saregamapadhanisa”.rstrip
trailing characters removed. Rest is (‘ania’)
same as lstrip saregamapadha