Python Ch-3_Notes
Python Ch-3_Notes
Syntax:
if expression:
Statements(s)
Example:
num = int(input("enter the number:"))
if num%2 == 0:
print("The Given number is an even number")
O/P:
enter the number: 10
The Given number is an even number
Explain if…else statement with suitable example SUMMER-2022
2. if-else
The if-else statement is similar to if statement except the fact that, it also provides the block
of the code for the false case of the condition to be checked.
If the condition provided in the if statement is false, then the else statement will be executed.
Syntax:
if (condition):
#block of statements
else:
#another block of statements (else-block)
Example:
num = int(input("enter the number:"))
if (num%2 == 0):
print("The Given number is an even number")
else:
print("The Given number is odd number")
O/P:
enter the number: 10
The Given number is an even number
Explain nested if statements by giving syntax, flowchart and example. WINTER –
2021,WINTER-2022
3. Nested if
There may be a situation when you want to check for another condition after a condition
resolves to true. In such a situation, you can use the nested if construct.
Here, key point of the while loop is that the loop might not ever run. When the condition is
tested and the result is false, the loop body will be skipped and the first statement after the
while loop will be executed.
Example
count = 0
while (count < 9):
print ('The count is:', count)
count = count + 1
print "Good bye!"
While Loop with else statement
Python supports to have an else statement associated with a loop statement.
If the else statement is used with a while loop, the else statement is executed when the
condition becomes false.
Example:
count = 0
while count < 5:
print (count, " is less than 5")
count = count + 1
else:
print (count, " is not less than 5")
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 6
Python Programming (UNIT-3) | 4311601
O/P:
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
Explain for loop by giving flowchart and example. WINTER – 2021, WINTER-2022
2. for Loop
Python For loop is used for sequential traversal i.e. it is used for iterating over an iterable
like String, Tuple, List, Set, or Dictionary.
The process of traversing a sequence is known as iteration.
Syntax:
for value in sequence:
# code block
Example:
list = ["geeks", "for", "geeks"]
for i in list:
print(i)
O/P:
geeks
for
geeks
Using else Statement with for Loop
Example:
list = ["geeks", "for", "geeks"]
for (i in list):
print(i)
else:
print(“exit”)
O/P:
geeks
2*1=2
2*2=4
2*3=6
Explain break and continue statement with suitable example. SUMMER-2022,
WINTER-2022
break Statement
The break is a keyword in python which is used to bring the program control out of the loop.
The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks
the inner loop first and then proceeds to outer loops.
In other words, we can say that break is used to abort the current execution of the program
and the control goes to the next line after the loop.
The break is commonly used in the cases where we need to break the loop for a given
condition.
Syntax:
break;
Example:
my_str = "python"
**********
“Do not give up, the beginning is always hardest!”