Python Programming (Unit-2)
Python Programming (Unit-2)
TECHNOLOGY, BANDA
“PYTHON PROGRAMMING”
BCC-402
UNIT – II
Python Program Flow Control Conditional blocks: if, else and else
if (elif), Simple for loops in python, for loop using ranges, string, list
and dictionaries. Use of while loops in python, Loop manipulation
using pass, continue, break and else. Programming using Python
conditional and loop blocks.
Compiled by
Abhishek Tiwari
(Assistant Professor)
Control Flow Statements
Python control flow. Control flow is the order in which individual statements, instructions, or
function calls are executed or evaluated. The control flow of a Python program is regulated by
conditional statements, loops, and function calls.
Python if statement
The if statement is the most simple decision-making statement. It is used to decide whether a
certain statement or block of statements will be executed or not.
if condition:
# Statements to execute if
# condition is true
Here, the condition after evaluation will be either true or false. if the statement accepts
boolean values – if the value is true then it will execute the block of statements below it
otherwise not.
As we know, Python uses indentation to identify a block. So the block under the Python if
statements will be identified as shown in the below example:
if condition:
statement1
statement2
# Here if the condition is true, if block
# will consider only statement1 to be inside
# its block.
if (i > 15):
print("10 is less than 15")
print("I am Not in if")
Output:
I am Not in if
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 the if statement Python to execute a block of code
when the Python if condition is false.
i = 20
if (i < 15):
print("i is smaller than 15")
print("i'm in if Block")
else:
print("i is greater than 15")
print("i'm in else Block")
print("i'm not in if and not in else Block")
Output:
i is greater than 15
i'm in else Block
i'm not in if and not in else Block
# Explicit function
def digitSum(n):
dsum = 0
for ele in str(n):
dsum += int(ele)
return dsum
# Initializing list
List = [367, 111, 562, 945, 6726, 873]
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
i = 10
if (i == 10):
# First if statement
if (i < 15):
print("i is smaller than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print("i is smaller than 12 too")
else:
print("i is greater than 15")
Output:
i is smaller than 15
i is smaller than 12 too
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
i = 20
if (i == 10):
print("i is 10")
elif (i == 15):
print("i is 15")
elif (i == 20):
print("i is 20")
else:
print("i is not present")
Output:
i is 20
Syntax:
if condition: statement
We can run a single statement or set of statements repeatedly using a loop command.
The following sorts of loops are available in the Python programming language.
Name of
Sr.No. Loop Type & Description
the loop
While loop Repeats a statement or group of statements while a given condition is TRUE.
1 It tests the condition before executing the loop body.
For loop This type of loop executes a code block multiple times and abbreviates the
2 code that manages the loop variable.
Output:
The list of squares is [16, 4, 36, 49, 9, 25, 64, 100, 36, 1, 81, 4]
Only if the execution is complete does the else statement comes into play. It won't be executed
if we exit the loop or if an error is thrown.
Code
# Initiating a loop
for s in a string:
# giving a condition in if block
if s == "o":
print("If block")
# if condition is not satisfied then else block will be executed
else:
print(s)
Output:
P
y
t
h
If block
n
L
If block
If block
p
Code
# Python program to show how to use else statement with for loop
# Creating a sequence
tuple_ = (3, 4, 6, 8, 9, 2, 3, 8, 9, 7)
Output:
3
9
3
9
7
These are the odd numbers present in the tuple
With the help of the range() function, we may produce a series of numbers. range(10) will
produce values between 0 and 9. (10 numbers).
We can give specific start, stop, and step size values in the manner range(start, stop, step size).
If the step size is not specified, it defaults to 1.
Since it doesn't create every value it "contains" after we construct it, the range object can be
characterized as being "slow." It does provide in, len, and __getitem__ actions, but it is not an
iterator.
Code
# Python program to show the working of range() function
print(range(15))
print(list(range(15)))
print(list(range(4, 9)))
Output:
range(0, 15)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[4, 5, 6, 7, 8]
[5, 9, 13, 17, 21]
To iterate through a sequence of items, we can apply the range() method in for loops. We can
use indexing to iterate through the given sequence by combining it with an iterable's len()
function. Here's an illustration.
Code
# Python program to iterate over a sequence with the help of indexing
Output:
PYTHON
LOOPS
SEQUENCE
CONDITION
RANGE
While Loop
While loops are used in Python to iterate until a specified condition is met. However, the
statement in the program that follows the while loop is executed once the condition changes to
false.
while <condition>:
{ code block }
All the coding statements that follow a structural command define a code block. These
statements are intended with the same number of spaces. Python groups statements together
with indentation.
Code
Output:
Python Loops
Python Loops
Python Loops
Python Loops
Code
#Python program to show how to use else statement with the while loop
counter = 0
# Iterating through the while loop
while (counter < 10):
counter = counter + 3
print("Python Loops") # Executed untile condition is met
# Once the condition of while loop gives False this statement will be executed
else:
print("Code block inside the else statement")
Output:
Python Loops
Python Loops
Python Loops
Python Loops
Code block inside the else statement
Code
Continue Statement
Code
Output:
Current Letter: P
Current Letter: y
Current Letter: h
Current Letter: n
Current Letter:
Current Letter: L
Current Letter: s
Break Statement
It stops the execution of the loop when the break statement is reached.
Code
Output:
Current Letter: P
Current Letter: y
Current Letter: t
Current Letter: h
Current Letter: o
Current Letter: n
Current Letter:
Pass Statement
Pass statements are used to create empty loops. Pass statement is also employed for classes,
functions, and empty control statements.
Code
Output:
Last Letter: s
age = 18
if else statement
age = 16