Unit 2 - Python Programming
Unit 2 - Python Programming
CONTROL STATEMENTS:
1. Conditional statements
2. Iterative statements.
3. Transfer statements
Conditional statements
1. if statement
2. if-else
3. if-elif-else
4. nested if-else
If statement in Python
In control statements, The if statement is the simplest form. It takes a condition and
evaluates to either True or False.
If the condition is True, then the True block of code will be executed, and if the condition
is False, then the block of code is skipped, and The controller moves to the next line
Let’s see the example of the if statement. In this example, we will calculate the square of
a number if it greater than 5
Example
number = 6
if number > 5:
# Calculate square
print(number * number)
Output
36
If – else statement
The if-else statement checks the condition and executes the if block of code when the
condition is True, and if the condition is False, it will execute the else block of code.
if condition:
statement 1
else:
statement 2
If the condition is True, then statement 1 will be executed If the condition is False, statement 2
will be executed. See the following flowchart for more detail.
Example
if password == "PYnative@#29":
print("Correct password")
else:
print("Incorrect Password")
Output 1:
Output 2:
In Python, the if-elif-else condition statement has an elif blocks to chain multiple conditions
one after another. This is useful when you need to check multiple conditions.
With the help of if-elif-else we can make a tricky decision. The elif statement checks
multiple conditions one by one and if the condition fulfills, then executes that code.
if condition-1:
statement 1
elif condition-2:
stetement 2
elif condition-3:
stetement 3
...
else:
statement
Example
def user_check(choice):
if choice == 1:
print("Admin")
elif choice == 2:
print("Editor")
elif choice == 3:
print("Guest")
else:
print("Wrong entry")
user_check(1)
user_check(2)
user_check(3)
user_check(4)
Output:
Admin
Editor
Guest
Wrong entry
Nested if-else statement
Indentation is the only way to differentiate the level of nesting. The nested if-else is
useful when we want to make a series of decisions
if conditon_outer:
if condition_inner:
statement of inner if
else:
statement of inner else:
statement of outer if
else:
Outer else
statement outside if block
Output 1:
Output 2:
Instead of writing a block after the colon, we can write a statement immediately after
the colon.
Example
number = 56
if number > 0: print("positive")
else: print("negative")
Iterative statements
Python provides us the following two loop statement to perform some actions
repeatedly
1. for loop
2. while loop
Using for loop, we can iterate any sequence or iterable variable. The sequence can be
string, list, dictionary, set, or tuple.
Syntax of for loop:
Output
1
2
3
4
5
6
7
8
9
10
In a while-loop, every time the condition is checked at the beginning of the loop, and if
it is true, then the loop’s body gets executed. When the condition became False, the
controller comes out of the block.
Syntax of while-loop
while condition :
body of while loop
num = 10
sum = 0
i = 1
while i <= num:
sum = sum + i
i = i + 1
print("Sum of first 10 number is:", sum)
Run
Output
Transfer statements
In Python, transfer statements are used to alter the program’s way of execution in a
certain manner. For this purpose, we use three types of transfer statements.
1. break statement
2. continue statement
3. pass statements
Break Statement in Python
The break statement is used inside the loop to exit out of the loop. It is useful when we want to
terminate the loop as soon as the condition is fulfilled instead of doing the remaining iterations.
It reduces execution time. Whenever the controller encountered a break statement, it comes out
of that loop immediately
Let’s see how to break a for a loop when we found a number greater than 5.
Output
stop processing.
Let’s see how to skip a for a loop iteration if the number is 5 and continue executing the
body of the loop for other numbers.
Example of a continue statement
Run
Output
A pass statement is a Python null statement. When the interpreter finds a pass
statement in the program, it returns no operation. Nothing happens when
the pass statement is executed.
Example
Output
Where in other programming languages the indentation in code is for readability only, the
indentation in Python is very important.
Example
if 5 > 2:
print("Five is greater than two!")
Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
Example
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
You have to use the same number of spaces in the same block of code, otherwise Python
will give you an error:
Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")