Unit4 Control Stmts
Unit4 Control Stmts
Conditional Statements
• The if statement is the conditional statement in Python. There are 3 forms of if
statement:
• Simple if statement
• The if..else statement
• The If..elif..else statement
Simple if statement
• The if statement tests a condition & in case the condition is True, it carries out some
instructions and does nothing in case the condition is False.
• Syntax
if <condition>:
statement
[statements]
e.g.
if amount>1000:
disc = amount * .10
Example of if statement
Prog to find discount (10%) if amount is more than 1000.
Price = float (input(“Enter Price ? ” ))
Qty = float (input(“Enter Qty ? ” ))
Amt = Price* Qty
print(“ Amount :” , Amt)
if Amt >1000:
disc = Amt * .10
print(“Discount :”, disc)
Prog to find discount (10%) if amount is more than 1000, otherwise (5%).
Price = float (input(“Enter Price ? ” ))
Qty = float (input(“Enter Qty ? ” ))
Amt = Price* Qty
print(“ Amount :” , Amt)
if Amt >1000 :
disc = Amt * .10
print(“Discount :”, disc)
else :
disc = Amt * .05
print(“Discount :”, disc)
else :
disc = Amt * .05
print(“Discount :”, disc)
if X>Y:
if X > Z:
Largest = X
else:
Largest = Z
else:
if X > Z:
Largest = X
else:
Largest = Z
print(“Largest Number :”, Largest)
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
Syntax 1:
for <Var> in range (val1, val2, Val3):
Statements to be repeated
Syntax 2:
for <Var> in <Sequence> :
Statements to repeat
Sum=0
For i in range(10, 1, -2):
print(i)
Sum=Sum + i
print(“Sum of Series”, Sum)
Note: the else clause of a loop will be executed only when the loop terminates normally
(not when break statement terminates the loop)
Nested for Loop
for i in range ( 1, 6 ):
print( )
for j in range (1, i + 1):
print(“@”, end=“ ”)
Else clause
• In Python, the else clause can be used with a while statement.
• The else block is gets executed whenever the condition of the while statement is
evaluated to false.
• But, if the while loop is terminated with break statement then else doesn't execute.
• Else statement can also be used with the for statement in Python
Break Statement:
The break statement is used to terminate the loop containing it, the control of the program
will come out of that loop.
Syntax:
Break
Output:
Current character: P
Current character: y
Current character: t
Continue statement
When the program encounters a continue statement, it will skip the statements which are
present after the continue statement inside the loop and proceed with the next iterations.
Loop does not terminate but continues on with the next iteration.
Syntax:
Continue
Example:
for char in ‘Python’:
if (char == ‘y’):
continue
print(“Current character: “, char)
Output:
Current character: P
Current character: t
Current character: h
Current character: o
Current character: n
Pass Statement:
Pass statement is python is a null operation, which is used when the statement is required
syntactically.
Syntax:
pass
Example:
for char in ‘Python’:
if (char == ‘h’):
pass
print(“Current character: “, char)
Output:
Current character: P
Current character: y
Current character: t
Current character: h
Current character: o
Current character: n
Lists
• List is an ordered sequence of items. Values in the list are called elements / items.
• It can be written as a list of comma-separated items (values) between square
brackets[ ].
• Items in the lists can be of different data types.
Operations on list:
1. Indexing
2. Slicing
3. Concatenation
4. Repetitions
5. Updating
6. Membership
7. Comparison
>>> print(a[0:3])
Slicing [2, 3, 4]
>>> print(a[0:]) Printing a part of the list.
[2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> print(a[2])
4 Updating the list using
Updating >>> a[2]=100 index value.
>>> print(a)
[2, 3, 100, 5, 6, 7, 8, 9, 10]
>>> a=[2,3,4,5,6,7,8,9,10]
>>> 5 in a
Membership True Returns True if element is
>>> 100 in a present in list. Otherwise
False returns false.
>>> 2 not in a
False
>>> a=[2,3,4,5,6,7,8,9,10]
>>>b=[2,3,4] Returns True if all elements
Comparison >>> a==b in both elements are same.
False Otherwise returns false
>>> a!=b
True
List slices:
List slicing is an operation that extracts a subset of elements from an list and packages
them as another list.
Syntax:
Listname[start:stop]
Listname[start:stop:steps]
default start value is 0
default stop value is n-1
[:] this will print the entire list
[2:2] this will create a empty slice
>>> a=[9,8,7,6,5,4]
a[0:3] >>> a[0:3] Printing a part of a list
from 0 to 2.
[9, 8, 7]
List methods:
List loops:
1. For loop
2. While loop
3. Infinite loop List using For Loop:
• The for loop in Python is used to iterate over a sequence (list, tuple, string) or other
iterable objects.
• Iterating over a sequence is called traversal.
• Loop continues until we reach the last item in the sequence.
• The body of for loop is separated from the rest of the code using indentation.
Syntax:
for val in sequence:
a=[10,20,30,40,50] 1
for i in a: 2
print(i) 3
4
5
a=[10,20,30,40,50] 0
for i in range(0,len(a),1): 1
print(i) 2
3
4
a=[10,20,30,40,50] 10
for i in range(0,len(a),1): 20
print(a[i]) 30
40
50
a=[1,2,3,4,5] i=0 15
sum=0 while i<len(a):
sum=sum+a[i]
i=i+1
print(sum)
Mutability:
Lists are mutable. (can be changed)
Mutability is the ability for certain types of data to be changed without entirely recreating
it.
An item can be changed in a list by accessing it directly as part of the assignment statement.
Using the indexing operator (square brackets[ ]) on the left side of an assignment, one of
the list items can be updated.
Example description