You are on page 1of 29

Python Program Flow

Control
Program Control Statement or Control Structures
• Normally, the program execution starts with the first statement of
program and ends with the last statement of program.
• With the help of control statement or control structure, we can change
the execution order.
• Control structures specify the statement to be executed and the order of
execution of statements.
• There are three kinds of control structures.
1. Sequential Statements
2. Selection or Branching or Conditional Statements
3. Iteration or Looping Statements
1.Sequential Statements : instructions are executed in linear order.
2. Selection or Branching or Conditional Statements : it asks a true / false
question and then selects the next instruction based on the answer.
3. Iteration or Looping Statements : it repeats the execution of a block of
instructions.
Control Structures in Flowchart

Sequence Selection Iteration


Sequential
Python if Statement Syntax

if (test expression):
statement(s)
Python if Statement Flowchart

num = float(input("Enter a number: "))


if num > 0:
print("Positive number")
GETTING VALUE FROM USER
num=input() # use of input function to read value from user
print(num)
>>> 5
5
Python if...else

if (test expression):
Body of if
else: Python if…else Flowchart
Body of else

num = float(input("Enter a number: "))


if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
1. WAP to find the largest of two numbers.
2. WAP to check whether given number is even or odd.
3. WAP to check whether the two given numbers are equal or not.
4. WAP to find the largest among three numbers
5. WAP to checks whether a number given by the user is zero, positive or
negative.
Assigning multiple values to variables

a,b,c = 1,2,"john"
Python if...elif...else Flowchart of if...elif...else
if (test expression):
Body of if
elif (test expression):
Body of elif
elif (test expression):
Body of elif
else:
Body of else
EXAMPLE 1 EXAMPLE 2

num = float(input("Enter a number: ")) num = int(input("Enter a number: "))


if num == 0:
if num > 0:
print("zero")
print("Positive number") elif num == 1:
elif num == 0: print("one")
print("Zero") elif num == 2:
else: print("two")
elif num == 3:
print("Negative number")
print("three")
elif num == 4:
print("four")
else:
print("Number is", num)
Python Nested if statements

num = float(input("Enter a number: "))


if (num >= 0):
if (num == 0):
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Kinds of loops

Flow Diagram
1. while loop
2. for loop
3. for…...else
4. while……else

• While Loop
Syntax
count = 0
while (expression): while (count < 9):
print ('The count is:', count)
statement(s) count = count + 1
print ("Good bye!“)
Nested while loop
while expression:
while expression: 1,5
i,j = 1,5 1,6
statement(s) #j = 5 1,7
while i < 6: 2,5
statement(s) j=5 2,6
while j < 8: 2,7
print(i, ",", j) 3,5
j=j+1
i=1 Output: i=i+1
3,6
3,7
j=5 1 , 5 4,5
4,6
1,6 4,7
while i < 4:
1,7 5,5
5,6
while j < 8: 5,7
print(i, ",", j)
j=j+1
i=i+1
Python – while loop with else block
• We can have a ‘else’ block associated with while loop.
• The ‘else’ block is optional.
• It executes only after the loop finished execution.

num = 10 Output:
while (num > 6): 10
print(num) 9
num = num-1 8
7
else:
loop is finished
print("loop is finished")
For Loop
for iterating_var in sequence:
statements
Flow Diagram
for let in 'Python': # First Example
Output
print ('Current Letter :', let)
Current Letter : P
fruits = ['banana', 'apple', 'mango']
Current Letter : y
for fru in fruits: # Second Example Current Letter : t
print ('Current fruit :', fru) Current Letter : h
Current Letter : o
print ("Good bye!“) Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango

Good bye!
numbers = [1, 2, 4, 6, 11, 20]

# variable to store the square of each num temporary


sq = 0 Output:
1
# iterating over the given list 4
16
for val in numbers:
36
# calculating square of each number 121
sq = val * val 400
# displaying the squares
print(sq)
• For loop with else block
• In Python we can have an optional ‘else’ block associated with the loop.
• The ‘else’ block executes only when the loop has completed all the
iterations.
Output:
Eg- for val in range(5):
print(val) 0
1
else:
2
print("The loop has completed execution") 3
Function range() 4
range(n): generates a set of whole numbers starting from 0 to The loop has
(n-1). completed execution
For example:
range(8) is equivalent to [0, 1, 2, 3, 4, 5, 6, 7]
Nested for loop for num1 in range(3):
for iterating_var in sequence: for num2 in range(10, 14):
print(num1, ",", num2)
for iterating_var in sequence:
statements(s) Output:
0 , 10
statements(s)
0 , 11
0 , 12
0 , 13
1 , 10
1 , 11
1 , 12
1 , 13
2 , 10
2 , 11
2 , 12
2 , 13
Control statements using pass, continue, break
• We might face a situation in which we need to exit a loop completely when
an external condition is triggered or there may also be a situation when you
want to skip a part of the loop and start next execution.
• Python provides break and continue statements to handle such situations
and to have good control on your loop.
1. The break Statement:
• The break statement in Python terminates the current loop and resumes
execution at the next statement, just like the traditional break found in C.
for letter in 'Python':
if letter == 'h':
break
print ('Current Letter :', letter)
OUTPUT
var = 10 Current Letter : P
while (var > 0): Current Letter : y
Current Letter : t
print ('Current variable value :', var)
Current variable value : 10
var = var -1 Current variable value : 9
if (var == 5): Current variable value : 8
break Current variable value : 7
Current variable value : 6
Good bye!
print ("Good bye!")
2.The continue Statement:
• The continue statement in Python returns the control to the beginning of the
while or for loop.
• The continue statement rejects all the remaining statements in the current
iteration of the loop and moves the control back to the top of the loop.
• The continue statement can be used in both while and for loops.
for letter in 'Python': OUTPUT
if letter == 'h': Current Letter : P
Current Letter : y
continue Current Letter : t
print ('Current Letter :', letter) Current Letter : o
Current Letter : n
Current variable value : 1
var = 0 Current variable value : 2
while var < 10: Current variable value : 3
Current variable value : 4
var = var +1 Current variable value : 6
if var == 5: Current variable value : 7
Current variable value : 8
continue Current variable value : 9
print ('Current variable value :', var) Current variable value : 10
print ("Good bye!") Good bye!
3. The pass Statement:
• The pass statement in Python is used when a statement is required syntactically
but you do not want any command or code to execute.
• The pass statement is a null operation; nothing happens when it executes.
• When the user does not know what code to write, So user simply places pass at
that line.
OUTPUT
Current Letter : P
for letter in 'Python': Current Letter : y
if letter == 'h': Current Letter : t
This is pass block
pass
Current Letter : h
print ('This is pass block') Current Letter : o
print ('Current Letter :', letter) Current Letter : n
Good bye!
https://www.geeksforgeeks.org/python-pass-statement/
print ("Good bye!")
Iteration or looping Statement
1. WAP to print numbers from 10 to 1 and their respective binary, octal and
hexadecimal representation
2. WAP to print multiplication table of a given number.
3. WAP to print first ‘n’ natural numbers and their sum.
4. Write a Python program to find the sum of digits of a given number.
5. Python Program to find the area of triangle
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
6. Python Program to find Total, Average, and Percentage of Five Subjects &
Grade card.
7. Write a Python program to find the sum of all odd numbers divisible by three
in a group of n numbers entered by the user.
8. Python Program to print n-th Fibonacci number
9. Python Program to print Palindrome numbers from 1 to 100
10.Python Program to Print all Prime Numbers in an Interval
11.Write a Python program to calculate the factorial of a given number
12. WAP to print the odd composite numbers between m and n, where m and n are
positive integers greater than 1. (Odd composite numbers are all the odd integers
that are not prime. 9, 15, 21, 25, 27, etc,)
13. WAP to find the reverse of a number and then check whether it is palindrome or
not. if(percentage >= 90):
14. WAP to check whether given number is Armstrong or not. print("A Grade")
Armstrong number are: elif(percentage >= 80):
/ print Armstrong number in range print("B Grade")
• 0
elif(percentage >= 70):
• 1 print("C Grade")
• 153 elif(percentage >= 60):
print("D Grade")
• 370
elif(percentage >= 40):
• 371 print("E Grade") else:
• 407 print("Fail”)
UNIVERSITY QUESTIONS PREVIOUS YEARS 2020,2021,2022
1. Write a Python program to find average of n numbers entered by the user. 3
Marks
2. Write a python program to check whether a number is positive or negative.3
Marks
3. Write a program to read a number and then calculate the sum of its digits 6
Marks
4. Write a Python program to find the sum of all prime numbers in a group of n
numbers entered by the user.10 Marks
5. Write a program to generate all prime numbers in a given range. 10 Marks
6. Write a Python program to find the sum of all odd numbers divisible by three
in a group of n numbers entered by the user. 9 Marks
7. Write a python program to display Fibonacci series up to ‘n’. 10 Marks
UNIVERSITY IMPORTANT THEORY QUESTIONS
1. Explain the use of break statement in Python with an example. 3 Marks 2021
2. Define the terms expression and statement with the help of an example. 4 Mks 2021
3. What is the use of continue statement in Python? 5 Marks 2021
4. What is ‘pass’ statement in python? Explain with example. 3 Marks 2022
5. Explain different decision making statements in python with examples?12 Mks 2022
6. What is the purpose of indentation in python? Explain with an example. 2 Mks 2022
7. Differentiate continue and break statements with examples 4 Marks 2022
8. What are keywords? Give examples. 3 Marks 2022 , 2020
9. When should we use nested if statements? Explain with an example 3 Marks 2020
10. List the rules to name an identifier in Python 4 Marks 2020
11. What are the different operators used in Python? Briefly explain it.8 Marks 2020
EXPRESSION
• An expression is a combination of operators and operands that is interpreted to produce some other value. In
any programming language, an expression is evaluated as per the precedence of its operators. So that if there
is more than one operator in an expression, their precedence decides which operation will be performed first.
• Constant Expressions: These are the expressions that have constant values only
x = 15 + 1.3
print(x)
• Arithmetic Expressions: An arithmetic expression is a combination of numeric values, operators, and
sometimes parenthesis. The result of this type of expression is also a numeric value.
+,-,/,*,//,%,** etc…
• Integral Expressions: These are the kind of expressions that produce only integer results after all
computations and type conversions.
# Integral Expressions
a = 13
b = 12.0
c = a + int(b)
print(c)
• Floating Expressions
• Relational Expressions
• Logical Expressions
Statement
• A Python statement is an instruction that the Python interpreter can execute.
There are different types of statements in Python language as Assignment
statements, Conditional statements, Looping statements, etc.

You might also like