Python
Programming
Introduction to Python
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 1
Decision Making
&
Flow Control
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 2
Agenda
In this session, you will learn about:
• Control Structures
• If-elif-else statements
• Nested if-else
• For loop
• While loop
• Range
• Break
• Continue
• Pass
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 3
Control Structures
Conditions & Blocks
True False
Condition
Condition
False • Condition
o It is a Boolean expression.
True
Statement Statement o If the outcome of a condition
1 2 Statement is True, the statements
belong to that block will be
executed.
o If the condition outcome is
False, the next block should
/ be executed.
CONDITIONAL
• Blocks:
o Indentation defines start of
block
o Blocks can contain other
ü Break ü IF LOOPs: blocks
ü Continue ü IF – ELSE ü FOR o When indentation ends, block
ends
ü Pass ü IF – ELIF – ELSE ü WHILE If x>0:
ü Nested IF print(“It is a +ve no.”)
y=7
If y%5==0:
print(“Multiple of 5”)
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 4
If Statement
if (condition):
Statement
Flowchart of If Statement
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 5
If-Else Statement
if (condition):
Statement 1
else: Flowchart of If-Else Statement
Statement 2
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 6
If-Elif-Else Statement
if (condition1):
Statement 1 Flowchart of If—Elif-Else Statement
elif (condition2):
Statement 2
else:
Statement 3
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 7
Nested If-Else Statement
● Nesting means using an if statement within another if statement
● If the first ‘if’ condition is satisfied then the program executes the
commands within that ‘if’
if (condition1):
if (condition2):
Statement 1
else: Flowchart of Nested If-Else Statement
Statement 2
else:
if (condition3):
Statement 3
else:
Statement 4
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 8
Range() Function
range(start, stop, step_size)
● A sequence of numbers can be
generated using range() function.
● If only one parameter is given, that
implies the stop. By default, start is “0”
and step_size is “1”.
● range(10) à Start = 0, stop < 10,
step_size = 1.
● range(1, 10) à Start = 1, stop < 10
step_size = 1.
● range(1, 10, 2) à Start = 1, stop < 10
step_size = 2.
● To decrement, step_size must be a
negative value and stop < start.
● range(10, 1, -2) à starts from 10,
decrements at each step with -2 and
stop before 1.
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 9
While Loop
while (condition):
Statement
Program to
print 1st 10
natural
numbers
(i.e., 0 - 9
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 10
Infinite Loop
● An infinite loop runs infinitely after execution.
● For example, the below code provides infinite output where the while loop is
true for every iteration.
● This can be stopped either by clicking “stop” button or pressing “Ctrl + C”.
● Infinite loops should be avoided.
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 11
Do While Loop
do{
// statement or
// set of
statements
}
while(condition)
• Do while can run any
statement until the
condition statement
becomes false
specified in the loop.
• In do while loop, the
statement runs at
least once no matter
whether the condition
is false or true.
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 12
For Loop
● The ‘for loop’ is used to iterate over the items of a
for i in sequence: sequence object like list, tuple, string and other
Statement iterating objects
● The iteration continues until we reach the last item
in the sequence object
Program to
print 1st 10
natural
numbers
(i.e., 0 - 9
Iteration is a general term for taking each item from a sequence one after another
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 13
Break Statement
● Break allows you to exit a loop when an external
for i in sequence: condition is met.
if (condition): ● Normal program execution resumes at the next
break statement.
● A break statement with both for loops and while loops.
while (condition1):
● In a nested loop, break will stop execution of the
if (condition2):
innermost loop.
break
● The flow of the program resumes at the next line of code
immediately after the block.
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 14
Continue Statement
● Continue forces to execute the next iteration of the loop
● However, it skips the rest of the code inside the loop for that current iteration
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 15
Pass Statement
● Pass is a null statement to avoid errors in blank loops, function definitions,
class definitions, or in if statements.
● Instead of pass, if a comment or a blank is used, an IndentationError error
message will be thrown.
● The difference between pass and comment is that a comment is ignored by
the interpreter whereas a pass is not ignored.
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 16
Assignment
1. Write a Python program to reverse a string, e.g.,
input à "Hello, World!”, output à “!dlroW ,olleH”.
2. Write a Python program to reverse a string where each words position
should be same, e.g.,
input à "Hello, World!”, output à “,olleH !dlroW”.
3. Write a program that counts from 1 to 100, printing each number.
However, for multiples of 3, instead of printing the number, the program
should print "Fizz". For multiples of 5, the program should print "Buzz".
For numbers that are multiples of both 3 and 5, the program should print
"FizzBuzz". The loop should exit after printing the number 100.
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 17
Thank you
Dr. Susama Bagchi, Assistant Professor, CSE, CUIET 18