1. What is Flow of control in python?
The order of execution of the statements in a program is known as Flow of control.
It can be implemented using control structures.
2. What are the flow controls/ control structure used in python?
There are three types of control structures used in python.
i. Sequential Flow
ii. Selection/Conditional Flow
iii. Looping /repetition/iteration
3. Write a note on sequential flow. Give an example.
The sequential execution of codes or statements take place one after another from
beginning to end.
It is not possible to exclude any statement from the execution
It consumes more time and leads to more complexity in larger programs.
Example:
a=int (input (“Enter the value of a:”))
b=int (input (“Enter the value of b:”))
c=a+b
print(c)
4. What is Selection/conditional Flow?
Block of statements need to be included or excluded as per the requirement of the
program. This can be done by using selection statements.
The execution of block of statements take place based on the specified condition.
(True/False).
5. What are the various selection statements used in python?
➢ Simple if statement.
➢ If else statement.
➢ If elif…else statement.
6. Explain simple if statement with syntax and an example.
It is a simple decision control instruction.
If the given condition is True, then all the indented statement(s) are executed,
Otherwise, control goes out of if condition.
Syntax:
if condition:
statement(s)
Example:
n=int (input (“Enter the number”))
if n>0:
print("Positive number")
7. Explain if...else statement with syntax and an example.
It is an extension/variant of if statement.
Here, the alternate block for the if statement will be available.
It executes either of the block depends on the condition given.
Syntax:
if condition:
statement(s)
else:
Statement(s)
Example:
n=int (input (“Enter the number:”))
if n>0:
print (“Positive number”)
else:
print (“Negative number”)
8. Explain if…elif...else statement with syntax and example.
➢ It is used to check multiple conditional in the program.
➢ Number of elif is dependent on the number of conditions to be checked.
➢ If the first condition is false, then the next condition is checked, and so on.
➢ If one of the conditions is True, then the corresponding indented block executes,
and if statement terminates.
➢ If no conditions is True, then the else block executes.
Syntax:
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
Example:
n=int (input ("Enter the number:"))
if n>0:
print ("Positive number")
elif n==0:
print("ZERO")
else:
print ("Negative number")
9. What is Indentation? How important it is in python programming? Explain.
Indentation is very important concept in python programming, because the
interpreter checks indentation levels very strictly and throws up syntax error if it is
not correct.
In most programming languages, the block of statements is put inside the curly
brackets, but python uses indentation.
Indentation use a single tab space for each level.
Before beginning of indentation, (:) symbol is used.
Python uses uniform indentation to mark block of statements.
10. What is Iteration/Repetition/looping in python?
It is an ability to repeat the set of statements based on certain condition given.
The repetition can be done zero or more number of times depending on requirement
of the program.
This is also called iteration/looping.
This can be implemented by setting initial value, step value/condition and stop
value followed by block of statement(s).
The block of statement(s) under the looping will execute repeatedly until the
condition becomes false.
If the stop value is not mentioned in the loop, it becomes infinite loop.
11. Name two looping constructs used in python.
for loop
while loop
12. Explain the Range () in python.
➢ The range () is a built-in function in python.
➢ It is used to create a list containing a sequence of integers from the given start
value up to stop value (excluding stop value), with a difference of the given step value.
Syntax:
range([start],stop[,step])
Here, Start, stop and step are parameters
Start and step are optional.
Default value of starting value is 0 and step value is 1.
All parameters value should be in integer.
Eg.
for i in range(1,10)
13. Explain for loop with syntax and example.
For loop is used to iterate over a range of values or a sequence.
The values can be either numeric or any other data types like string, list etc.
In every step of iteration, the control variable checks whether each of the values
in the range have been traversed or not.
When all the items in the range are checked, loop will be terminated and control
goes the statement present after the for loop.
To use the for loop, it is known in advance that the number of times the loop
will execute.
Syntax:
for<control_variable> in <sequence/items in range>:
<statement(s) inside the loop>
Example:
for i in range (1,10):
print(i)
Flowchart:
14. Explain while loop with syntax and an example.
➢ It is used to execute a block of statements repeatedly until a given condition is True.
➢ If the condition becomes False, control terminates from the while loop and execute
the next statements.
➢ Here, the condition is tested before the execution of statements present inside the
loop.
➢ It is also called pretested loop.
Syntax:
while test_condition:
statement/s
Example:
i=0
while i<=5:
print(i)
i+=1
Flowchart
Break statement Continue Statement
It is used to alter the normal flow of It is used to skip the current iteration and
execution, and terminates the current jumps to the next iteration in the loop
loop.
The control remains in the same loop.
The control transfers outside of the
The keyword ‘continue’ is used.
loop.
Eg:
The keyword ‘break’ is used.
for i in range(10):
Eg:
if i==5:
for i in range(10):
continue
if i==5:
print(i)
break
print(i)
Nested loop
A loop inside another loop is called a nested loop.
Example:
Output:
Iteration 1 of outer loop
1
2
Out of inner loop
Iteration 2 of outer loop
1
2
Out of inner loop
Iteration 3 of outer loop
1
2
Out of inner loop
Out of outer loop
1. Program to print the pattern for a number input by the user.
#1
#1 2
#1 23
#1 234
#1 2345
num = int(input("Enter a number to generate its pattern = "))
for i in range(1,num + 1):
for j in range(1,i + 1):
print(j, end = " ")
print()
Output:
2. Program to find prime numbers between 2 to 20 using nested for
loops.
num = 2
for i in range(2, 20):
j= 2
while ( j <= (i/2)):
if (i % j == 0): #factor found
break #break out of while loop
j += 1
if ( j > i/j) : #no factor found
print ( i, "is a prime number")
print ("Bye Bye!!")
Output:
3. Write a program to calculate the factorial of a given number.
num = int(input("Enter a number: "))
fact = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, num + 1):
fact = fact * i
print("factorial of ", num, " is ", fact)
Output:
Enter a number: 5
Factorial of 5 is 120
EXERCISE questions
1. Find the output of the following program segments:
(i)
a = 110
while a > 100:
print(a)
a -= 2
Output:
(ii)
for i in range(20,30,2): Output:
print(i)
(iii)
country = 'INDIA'
for i in country:
print (i)
Output:
(iv)
i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i=i+2
print (sum)
Output:
(v)
for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)
Output:
(vi)
var = 7
while var > 0:
print ('Current variable value: ', var)
var = var -1
if var == 3:
break
else:
if var == 6:
var = var -1
continue
print ("Good bye!")
Output:
2. Write a function that checks whether an input number is a palindrome or not.
[Note: A number or a string is called palindrome if it appears same when written in
reverse order also. For example, 12321 is a palindrome while 123421 is not a
palindrome]
rev = 0 Output-1:
n = int(input("Enter the number: ")) Enter the number: 123421
temp = n The number is not a palindrome.
while temp > 0:
digit = (temp % 10) Output-2:
rev = (rev * 10) + digit Enter the number: 456
temp = temp // 10 The number is not a palindrome.
if(n == rev):
print("The number is a palindrome.")
else:
print("The number is not a
palindrome.")
3. Write a function to print the table of a given number. The number has to be
entered by the user.
def print_table(num):
for i in range(1, 11):
print(num, "x", i, "=", num * i)
n = int(input("Enter a number: "))
print_table(n)
4. Write a program that prints minimum and maximum of five numbers entered by
the user.
numbers = []
for i in range(5):
num = int(input("Enter number: "))
[Link](num)
print("Minimum:", min(numbers))
print("Maximum:", max(numbers))
Output:
Enter number: 45
Enter number: 65
Enter number: 12
Enter number: 15
Enter number: 20
Minimum: 12
Maximum: 65
5. Write a program to check if the year entered by the user is a leap year or not.
Answer:
year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
print(year, "is a Leap Year")
else:
print(year, "is not a Leap Year")
Output:
Enter a year: 2025
2025 is not a Leap Year
Enter a year: 2024
2024 is a Leap Year
6. Write a program to generate the sequence: –5, 10, –15, 20, –25..... up to n, where
n is an integer input by the user.
Answer:
n = int(input("Enter the value of n: "))
for i in range(1, n+1):
if i % 2 == 0:
print(i * 5, end=" ")
else:
print(-i * 5, end=" ")
Output:
Enter the value of n: 5
-5 10 -15 20 -25 .
7. Write a program to find the sum of 1+ 1/8 + 1/27......1/n³, where n is the number
input by the user.
Answer:
n = int(input("Enter the value of n: "))
total = 0
for i in range(1, n+1):
total += 1/(i**3)
print("Sum =", total)
Output
Enter the value of n: 5
Sum = 1.185662037037037
8. Write a program to find the sum of digits of an integer number, input by the user.
Answer:
num = int(input("Enter a number: "))
s=0
while num > 0:
digit = num % 10
s += digit
num //= 10
print("Sum of digits:", s)
Output:
Enter a number: 45
Sum of digits:
9. Write a program to print the following patterns:
*
***
*****
***
*
n=3
for i in range(1, n+1):
print(" "*(n-i) + "*"*(2*i-1))
for i in range(n-1, 0, -1):
print(" "*(n-i) + "*"*(2*i-1))
(ii) Pattern
1
212
32123
4321234
543212345
Answer:
n=5
for i in range(1, n+1):
print(" "*(n-i), end="")
for j in range(i, 0, -1):
print(j, end=" ")
for j in range(2, i+1):
print(j, end=" ")
print()
(iii) Pattern
12345
1234
123
12
1
Answer:
n=5
for i in range(n, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print()
(iv) Pattern
*
**
* *
**
*
Answer:
n=3
for i in range(1, n+1):
print(" "*(n-i) + "*", end="")
if i > 1:
print(" "*(2*i-3) + "*")
else:
print()
for i in range(n-1, 0, -1):
print(" "*(n-i) + "*", end="")
if i > 1:
print(" "*(2*i-3) + "*")
else:
print()