Unit 2
Unit 2
Q1. What is decision making statements? Write down the different decision
making statements.
Ans.
Python provides various types of conditional statements:
Statement Description
Nested Statements You can use one if or else statement inside another if or else
if statements(s)
Python If-Else Statement
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t. But if we want to do something
else if the condition is false, we can use the else statement with if statement to
execute a block of code when the if condition is false.
Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Python if-elif-else Ladder
Here, a user can decide among multiple options. The if statements are executed from
the top down. As soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the ladder is bypassed. If none of the
conditions is true, then the final else statement will be executed.
Syntax:
if (condition):
statement
elif (condition):
statement
else:
statement
Ans:
if(n>0):
print("Number is positive")
else:
print("Number is negative")
if(n&1==0):
print(“even”)
else:
print(“odd”)
Q3. Program to take in the marks of 5 subjects and display the grade.
Ans:
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg>=90):
print("Grade: A")
elif(avg>=80&avg<90):
print("Grade: B")
elif(avg>=70&avg<80):
print("Grade: C")
elif(avg>=60&avg<70):
print("Grade: D")
else:
print("Grade: F")
Ans:
else:
if(n>>31==0):
print(“positive”)
else:
print(“negative”)
Loops in python
Generally Statements are executed sequentially, but there sometimes occur such cases
where programmers need to execute a block of code several times. The control structures of
programming languages allow us to execute a statement or block of statements repeatedly.
Loops are a sequence of instructions that does a specific set of instructions or tasks based
on some conditions and continue the tasks until it reaches certain conditions.
Following diagram illustrates a loop statement .
Python language provides the different types of loop –
Syntax:
while expression:
statement(s)
Output –
Hello
Hello
Hello
The else clause is only executed when your while condition becomes false. If you
break out of the loop, or if an exception is raised, it won’t be executed.
Syntax of While Loop with else statement:
while condition:
# execute these statements
else:
# execute these statements
count = 0
while (count < 3):
count = count + 1
print("Hello Geek")
else:
print("In Else Block")
Output
Hello Geek
Hello Geek
Hello Geek
In Else Block
The for loop has the ability to iterate over the items of any
sequence, such as a list or a string.
Syntax:
for iterator_var in sequence:
statements(s)
Example –
s = "apple"
for i in s:
print(i)
Output –
a
p
p
l
e
Example-
for x in range(6):
print(x)
Example-
Example-
for x in range(2, 30, 3):
print(x)
Example with List, Tuple, string, and dictionary iteration using For Loops
in Python
We can use for loop to iterate lists, tuples, strings and dictionaries in Python.
1. Example with List
Output-
languages = ["C", "JAVA", "PYTHON"] C
for x in languages:
JAVA
print(x)
PYTHON
for x in thisdict:
print(x)
I
4. Example with string
N
country=”INDIA”
for i in country: D
print(i)
I
A
Using else statement with for loop in Python
We can also combine else statement with for loop like in while loop. If else statement
is used with for loop , then else statement will executed when the loop has finished
iterating the given sequence .
Example-
C
languages = ["C", "JAVA", "PYTHON"]
for x in range(len(languages)): JAVA
print(languages[x]) PYTHON
else:
print(“Inside else block”) Inside else block
AAA
BB
C
ANS -
n=3
for i in range(n,0,-1):
for j in range(0,i,1):
print(chr(65+n-i),end=" ")
print()
Loop control statements change execution from their normal sequence. When
execution leaves a scope, all automatic objects that were created in that scope
are destroyed. Python supports the following control statements.
Break statement –
● break statement in Python is used to bring the control out of the loop when
some external condition is triggered.
● break statement is put inside the loop body (generally after if condition). It
terminates the current loop, i.e., the loop in which it appears, and resumes
execution at the next statement immediately after the end of that loop.
● If the break statement is inside a nested loop, the break will terminate the
innermost loop.
Example-
s = 'programming' # example1
for letter in s: p
print(letter)
if letter == 'o' or letter == 'm': r
break out of for loop
print("Out of for loop")
var =10 10
while(var>0): 9
print(var) 8
var=var-1 7
if var ==5: 6
break Out of while loop
print(“out of while loop)
Continue Statement
Example :
P
1. for var in "PYTHON": T
H
if var == "Y": O
continue N
print(var)
0
1
2
3
2. i=0 4
6
while i < 8: 7
if i == 5:
i += 1
continue
print(i)
i += 1
FlowChart
Pass Statement
● It is used when a statement is required
syntactically but you don't want any command or
code to execute .
● Pass is also used for empty control statements,
functions and classes.
Example - 2
4
for num in range(1,10): 6
8
if num%2 != 0:
pass
else:
print(num)
Note- Empty code is not allowed in loops , functions , if statements
, class definitions or in function definitions .
a = 33
b = 22
if b > a :
a = 33
b = 22
if b > a :
Pass.
Output - no error
Difference between Pass and
Comments
Pass Comments
Pass statement Comments are used
allow program to to enhance the
execute , nothing readability of the
happens but you code.
avoid getting an
error when empty
code is not allowed
.
Python interpreter Python interpreter
does not ignored ignored all the
the pass statement comments during
during the the execution of the
execution of the program.
program
The continue statement is used to skip an iteration of a for The break statement is used to terminate the
Python.
The continue statement can lead to an infinite loop when The break statement never causes an infinite
We cannot use the continue statement outside a for loop or a We cannot use the break statement outside a
The continue statement can be executed multiple times in a The break statement can be executed only once
for loop or while loop in Python. inside a loop. After this, the execution of the