Chapter-4 Conditional and Iterative Statements in Python
Chapter-4 Conditional and Iterative Statements in Python
else :
disc = Amt * .05 block 2
print(“Discount :”, disc) (will be executed incase condition is False)
The if..elif statement
• The if - elif statement has • Syntax
multiple test conditions and if <condition1> :
statement
in case the condition1 is Block 1
[statements]
True, it executes statements elif <condition2> :
in block1, and in case the statement
condition1 is False, it moves [statements] Block 2
to condition2, and in case elif <condition3> :
the condition2 is True, statement
[statements] Block 3
executes statements in
:
block2, so on. In case none :
of the given conditions is else :
true, then it executes the statement
statements under else block [statements]
Example of if-elif statement
• Prog to find discount (20%) if amount>3000, disc(10%) if
Amount <=3000 and >1000, otherwise (5%).
Price = float (input(“Enter Price ? ” ))
Qty = float (input(“Enter Qty ? ” ))
Amt = Price* Qty
print(“ Amount :” , Amt)
if Amt >3000 :
disc = Amt * .20 block 1
print(“Discount :”, disc) (will be executed incase condition 1 is true)
elif Amt>1000:
disc = Amt * .10 block 2
print(“Discount :”, disc) (will be executed incase condition2 is True)
else :
block 3
disc = Amt * .05
(will be executed incase both the
print(“Discount :”, disc)
condition1 &conditon2 are False)
Example of Nested if statement
• Prog to find Largest of Three Numbers (X,Y,Z)
X = int (input(“Enter Num1 ? ” ))
Y = int (input(“Enter Num2 ? ” ))
Z = int (input(“Enter Num3 ? ” ))
if X>Y:
if X > Z:
Largest = X
else:
Largest = Z
else:
if X > Z:
Largest = X
else:
Largest = Z
print(“Largest Number :”, Largest)
Assignments
• WAP to input a number and check whether it is Even or Odd.
• WAP to input a number print its Square if it is odd, otherwise print
its square root.
• WAP to input a Year and check whether it is a Leap year.
• WAP to input a number check whether it is Positive or Negative or
ZERO.
• WAP to input Percentage Marks of a students, and find the grade as
per following criterion:
Marks Grade
>=90 A
75-90 B
60-75 C
Below 60 D
LOOPS
• It is used when we want to execute a sequence of
statements (indented to the right of keyword for) a
fixed number of times.
• Syntax of for statement is as follows:
– i) with range() function
– ii) with sequence
The for Loop
Starting Ending Step
• Syntax 1: value value value
"a" + 5 is invalid
• Note: both operands must be Note: operands must be one string
strings. & other Number
Membership Operators: (in & not in)
Then,
word [0:7] will give : WelCome
word [0:3] will give : Wel
word [2:5] will give : lCo (2,3,4)
word [-6:-3] will give : elC (-6,-5,-4)
word [ :5] will give : WelCo (0,1,2,3,4)
word [3: ] will give : Come (5,6,7,8)
STRING FUNCTIONS & METHODS:
1. string.capitalize():
it gives the copy of string with its first character
capitalized.
e.g. if t = "sumit rana” then R= t.capitalize()
will create a copy of string in R as,
R = "Sumit rana“
STRING FUNCTIONS & METHODS:
2. string.find(subString [,start [,end]):
it returns the lowest index in the string where the
subString is found with in the slice range of start &
end, returns -1 if subString not found.
e.g. t="We are learning Strings. Strings are used in Python"
k = "Strings"
t.find(k) gives : 16 (index no.)
t.find(k,20) gives : 25 (index no.)
t.find(k,20,30) will search from index 20 to 30
t.find(“Hello”) gives : -1 ( as string is not found)
STRING FUNCTIONS & METHODS:
3. string.isalnum(): it gives True, if the characters in the st
ring are alphanumeric (alphabet or number) and there
is atleast one character, False otherwise.
e.g. t ="Hello123” then t.isalnum() gives True
t ="Hello 123” then t.isalnum() gives False
Output: Output:
H H
e e
l l
l l
o o
for loop with string
e.g.1: e.g. 2:
T = “Hello” T = “Hello”
Len= len(T) Len= len(T)
for i in range(Len) : for i in range(0,Len) :
print(T[i]) print(T[i])
Output: Output:
H H
e e
l l
l l
o o