Control Statements1 4
Control Statements1 4
Control Statements1 4
Compound_Statement_Header :
indented_body containing multiple
simple or compound statement
Compound Statement has:
conditional
function
Decision Making Statement
1. if statements
Syntax:
if(condition):
statement
[statements]
e.g.
noofbooks = 2
if (noofbooks == 2):
print('You have ')
print(‘two books’)
print(‘outside of if statement’)
Output
You have two books
Note:To indicate a block of code in Python, you must indent each line
of the block by the same amount. In above e.g. both print statements
are part of if condition because of both are at same level indented but
not the third print statement.
Decision Making Statement
1. if statements
Using logical operator in if
statement
x=1
y=2
if(x==1 and y==2):
print(‘condition matcing the criteria')
Output :-
a=100
if not(a == 20):
print('a is not equal to 20')
Output :-
a is not equal to 20
Decision Making Statement
2. if-else Statements
If-else statement executes some code if the test expression is
true (nonzero) and some other code if the test expression is
false.
Decision Making Statement
2. if-else Statements
Syntax:
if(condition):
statements
else:
statements
e.g. a=10
if(a < 100):
print(‘less than 100')
else:
print(‘more than equal 100')
OUTPUT
less than 100
*Write a program in python to check that entered numer is even or odd
Decision Making Statement
3. Nested if-else statement
The nested if...else statement allows you to check for
multiple test expressions and execute different codes for
more than two conditions.
Decision Making Statement
3. Nested if-else statement
Syntax
If (condition):
statements elif
(condition):
statements
else:
statements
E.G.
num = float(input("Enter a number: "))
if num >= 0:
if num == 0: print("Zero")
else:
print("Positive number") else:
print("Negative number") OUTPUT
Enter a number: 5 Positive
number
* Write python program to find out largest of 3 numbers.
Program to sort 3 numbers in
ascending order
a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))
if a < b:
if b < c:
print (a, “<", b, "<", c)
else:
if a < c:
print (a, "<", c, "<", b)
else:
print (c, "<", a, "<", b)
else:
if c < b:
print (c, "<", b, "<", a)
else:
if c < a:
print (b, "<", c, "<", a)
else:
print (b, "<", a, "<", c)
Iteration Statements (Loops)
Iteration statements(loop) are used to execute a block of
statements as long as the condition is true.
Loops statements are used when we need to run same code
again and again.
1.While Loop
2.For Loop
2. For Loop
It is used to iterate over items of any sequence, such as a
list or a string.
Syntax
for val in sequence:
statements
e.g.
for i in range(3,5):
print(i)
Output
3
4
Iteration Statements (Loops)
Output
5
4
range() Function Parameters
start: Starting number of the sequence.
stop: Generate numbers up to, but not including this number.
step(Optional): Determines the increment between each numbers in the
sequence.
Iteration Statements (Loops)
Output
1
2
3
No Break
Iteration Statements (Loops)
2. For Loop continue
Nested For Loop
e.g.
for i in range(1,3):
for j in range(1,11):
k=i*j
print (k, end=' ')
print()
Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
Iteration Statements (Loops)
2. For Loop continue
Nested For Loop
e.g.
for i in range(1,3):
for j in range(1,11):
k=i*j
print (k, end=' ')
print()
Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
Nested For Loop(Example-1):Pattern
for i in range(0,5):
for j in range(0,i+1):
print (‘*’, end=' ')
print()
Output
*
**
** *
****
*****
Nested For Loop(Example-2)
n=5
K = 2*n-2 #no of spaces
for i in range(1,n): #no of rows
for j in range(0,n): #spaces
print(end=“”)
k=k-2
for j in range(0,i+1):
print (‘*’, end=' ')
print(‘\r’)
Output
*
**
** *
****
*****
Nested For Loop(Example-3)
n=5
K = 2*n-2 #no of spaces
for i in range(1,n): #no of rows
for j in range(0,n): #spaces
print(end=“”)
k=k-2
for j in range(0,i+1):
print (‘*’, end=' ')
print(‘\r’)
Output:
*
**
** *
****
*****
Nested For Loop(Example-4)
k=1
for i in range(0, 5):
for j in range(0, i+1):
print(k, end=" ")
k=k+1
print()
Output
1
22
333
4444
55555
Jump Statements – break & continue