Teachers and Examiners (CBSESkillEduction) collaborated to create the Flow of Control in Python Class 11 Notes. All the important Information are taken from the NCERT Textbook Computer Science (083) class 11.
Flow of Control in Python Class 11 Notes
Selection Statement (Conditional Statement)
Conditions are tested by selection statements, This type of statement depending on the condition and generate the result based on condition. If a condition is met in this sentence, a true block is run; else, a false block is. example of selection statement is If…Else statement and Switch Statement.
If Statement having three types –
- If – Else Statement
- Elif Statement
- Nested If Statement
If – Else Statement
If statement called if..else statement allows us to write two alternative paths and the control condition determines which path gets executed. The syntax for if..else statement is as follows.
The syntax of if statement is:
if condition:
statement(s)
else:
statement(s)
Q. Write a program to accept person age from the user and check weather person is eligible for vote or not.
age = int(input(“Enter your age: “)) # Taking input from the user
if age >= 18: # Checking weather age is grater then 18 or not
print(“Eligible to vote”) #Printing the comment if age is grater then 18
else:
print(“Not eligible to vote”) #Printing the comment if age is grater then 18
Note – The int() code is used to convert string values to integer values since input codes only accept input in the form of character (Strings).
Flow of Control in Python Class 11 Notes
Q. Program to print the positive difference of two numbers.
#Program 6-2
#Program to print the positive difference of two numbers
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
if num1 > num2:
diff = num1 – num2
else:
diff = num2 – num1
print(“The difference of”,num1,”and”,num2,”is”,diff)
Output:
Enter first number: 5
Enter second number: 6
The difference of 5 and 6 is 1
Flow of Control in Python Class 11 Notes
Elif Statement
You can build a chain of if statements using the elif statement. Before the end of the elif chain is reached or one of the if expressions is true, the if statements are evaluated one at a time. If there is no true expression at the end of the elif chain, then else statement will be execuited.
The syntax for a selection structure using elif is as shown below.
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
Q. Check whether a number is positive, negative, or zero.
number = int(input(“Enter a number: “)
if number > 0:
print(“Number is positive”)
elif number < 0:
print(“Number is negative”)
else:
print(“Number is zero”)
Flow of Control in Python Class 11 Notes
Q. Display the appropriate message as per the colour of signal at the road crossing.
signal = input(“Enter the colour: “)
if signal == “red” or signal == “RED”:
print(“STOP”)
elif signal == “orange” or signal == “ORANGE”:
print(“Be Slow”)
elif signal == “green” or signal == “GREEN”:
print(“Go!”)
Q. Write a program to create a simple calculator performing only four basic operations.
#Program to create a four function calculator
result = 0
val1 = float(input(“Enter value 1: “))
val2 = float(input(“Enter value 2: “))
op = input(“Enter any one of the operator (+,-,*,/): “)
if op == “+”:
result = val1 + val2
elif op == “-“:
if val1 > val2:
result = val1 – val2
else:
result = val2 – val1
elif op == “*”:
result = val1 * val2
elif op == “/”:
if val2 == 0:
print(“Error! Division by zero is not allowed. Program terminated”)
else:
result = val1/val2
else:
print(“Wrong input,program terminated”)
print(“The result is “,result)
Output:
Enter value 1: 84
Enter value 2: 4
Enter any one of the operator (+,-,*,/): /
The result is 21.0
Flow of Control in Python Class 11 Notes
Indentation
The statements included within a block are typically enclosed in curly brackets in programming languages. Python, however, uses indentation for both block and nested block structures. Indentation is the practise of placing leading whitespace (spaces and tabs) at the start of a sentence.
Q. Program to find the larger of the two pre-specified numbers.
#Program 6-4
#Program to find larger of the two numbers
num1 = 5
num2 = 6
if num1 > num2: #Block1
print(“first number is larger”)
print(“Bye”)
else: #Block2
print(“second number is larger”)
print(“Bye Bye”)
Output:
second number is larger
Bye Bye
Flow of Control in Python Class 11 Notes
Repetition
Iteration is another word for this kind of repetition. A programme can repeat a certain collection of statements by using looping constructs.
Looping – The ability to repeatedly run a group of statements in a programme based on a condition is provided by looping constructs. While a certain logical condition is true, the statements in a loop are repeated again.
There are two type of looping –
- Entry Control Loop – The loop which check the condition first and then execuite the body of loop is known as entry control loop. example, for & while
- Exit Control Loop – The loop which execuite the body of loop first and then check the condition is known as exit control loop. example do-while
For Loop
The for statement allows you to specify how many times a statement or compound statement should be repeated. A for statement’s body is executed one or more times until an optional condition is met.
Syntax of the For Loop
for <control-variable> in <sequence/ items in range>:
<statements inside body of the loop>
Q. Program to print the characters in the string ‘PYTHON’ using for loop.
#Program 6-6
#Print the characters in word PYTHON using for loop
for letter in ‘PYTHON’:
print(letter)
Output:
P
Y
T
H
O
N
Q. Program to print the numbers in a given sequence using for loop.
#Program 6-7
#Print the given sequence of numbers using for loop
count = [10,20,30,40,50]
for num in count:
print(num)
Output:
10
20
30
40
50
Q. Program to print even numbers in a given sequence using for loop.
#Program 6-8
#Print even numbers in the given sequence
numbers = [1,2,3,4,5,6,7,8,9,10]
for num in numbers:
if (num % 2) == 0:
print(num,’is an even Number’)
Output:
2 is an even Number
4 is an even Number
6 is an even Number
8 is an even Number
10 is an even Number
Flow of Control in Python Class 11 Notes
The Range() Function
The range() is a built-in function in Python. Syntax of range() function is:
range([start], stop[, step])
It is used to generate a list of integers with a difference equal to the specified step value that runs from the given start value to the specified stop value (excluding the stop value). The function range() is often used in for loops for generating a sequence of numbers.
#start and step not specified
>>> list(range(10)-)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#default step value is 1
>>> list(range(2, 10))
[2, 3, 4, 5, 6, 7, 8, 9]
#step value is 5
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
#step value is -1. Hence, decreasing
#sequence is generated
>>> range(0, -9, -1)
[0, -1, -2, -3, -4, -5, -6, -7, -8]
Q. Program to print the multiples of 10 for numbers in a given range.
#Program 6-9
#Print multiples of 10 for numbers in a given range
for num in range(5):
if num > 0:
print(num * 10)
Output:
10
20
30
40
Flow of Control in Python Class 11 Notes
The ‘While’ Loop
While a loop’s control condition is true, a block of code is continuously run using the while statement. The while loop’s control condition is carried out before any statements inside the loop are performed. The loop continues as long as the control condition is true after each iteration. The statements in the body of the loop are not performed while this condition is false, and instead, control is passed to the statement that follows the while loop’s body. The body is not even once run if the while loop’s initial condition is false.
Syntax of while Loop
while test_condition:
body of while
Q. Program to print first 5 natural numbers using while loop.
#Program 6-10
#Print first 5 natural numbers using while loop
count = 1
while count <= 5:
print(count)
count += 1
Output:
1
2
3
4
5
Q. Program to find the factors of a whole number using while loop.
#Program 6-11
#Find the factors of a number using while loop
num = int(input(“Enter a number to find its factor: “))
print (1, end=’ ‘) #1 is a factor of every number
factor = 2
while factor <= num/2 :
if num % factor == 0:
#the optional parameter end of print function specifies the delimeter
#blank space(‘ ‘) to print next value on same line
print(factor, end=’ ‘)
factor += 1
print (num, end=’ ‘) #every number is a factor of itself
Output:
Enter a number to find its factors : 6 1 2 3 6
Flow of Control in Python Class 11 Notes
Break and Continue Statement
Using looping techniques, programmers can efficiently repeat tasks. When a specific condition is met, there are times when we may want to end a loop (coming out of the loop indefinitely) or skip a few of its statements before proceeding. Use of break and continue statements, respectively, can satisfy these criteria. These statements are provided by Python as a tool to provide the programmer additional control over how a programme is executed.
Break Statement
The break statement modifies the normal course of execution by ending the current loop and continuing with the statement that follows it.
Q. Program to demonstrate use of break statement.
#Program 6-12
#Program to demonstrate the use of break statement in loop
num = 0
for num in range(10):
num = num + 1
if num == 8:
break
print(‘Num has value ‘ + str(num))
print(‘Encountered break!! Out of loop’)
Output:
Num has value 1
Num has value 2
Num has value 3
Num has value 4
Num has value 5
Num has value 6
Num has value 7
Encountered break!! Out of loop
Q. Find the sum of all the positive numbers entered by the user. As soon as the user enters a neagtive number, stop taking in any further input from the user and display the sum .
#Program 6-13
#Find the sum of all the positive numbers entered by the user
#till the user enters a negative number.
entry = 0
sum1 = 0
print(“Enter numbers to find their sum, negative number ends the
loop:”)
while True:
#int() typecasts string to integer
entry = int(input())
if (entry < 0):
break
sum1 += entry
print(“Sum =”, sum1)
Output:
Enter numbers to find their sum, negative number ends the loop:
3
4
5
-1
Sum = 12
Q. Program to check if the input number is prime or not.
#Program 6-14
#Write a Python program to check if a given number is prime or not.
num = int(input(“Enter the number to be checked: “))
flag = 0 #presume num is a prime number
if num > 1 :
for i in range(2, int(num / 2)):
if (num % i == 0):
flag = 1 #num is a not prime number
break #no need to check any further
if flag == 1:
print(num , “is not a prime number”)
else:
print(num , “is a prime number”)
else :
print(“Entered number is <= 1, execute again!”)
Output 1:
Enter the number to be checked: 20
20 is not a prime number
Output 2:
Enter the number to check: 19
19 is a prime number
Output 3:
Enter the number to check: 2
2 is a prime number
Output 4:
Enter the number to check: 1
Entered number is <= 1, execute again!
Flow of Control in Python Class 11 Notes
Continue Statement
When a continue statement is found, the control goes to the beginning of the loop for the following iteration instead of executing any leftover statements in the loop’s body for the current iteration. The loop is restarted if the condition that initiated it is still true; else, control is passed to the statement that comes before the loop.
Q. Program to demonstrate the use of continue statement.
#Program 6-15
#Prints values from 0 to 6 except 3
num = 0
for num in range(6):
num = num + 1
if num == 3:
continue
print(‘Num has value ‘ + str(num))
print(‘End of loop’)
Output:
Num has value 1
Num has value 2
Num has value 4
Num has value 5
Num has value 6
End of loop
Flow of Control in Python Class 11 Notes
Nested Loops
A loop may contain another loop inside it. A loop inside another loop is called a nested loop. Python does not impose any restriction on how many loops can be nested inside a loop or on the levels of nesting. Any type of loop (for/while) may be nested within another loop (for/while).
Q. Program to demonstrate working of nested for loops.
#Program 6-16
#Demonstrate working of nested for loops
for var1 in range(3):
print( “Iteration ” + str(var1 + 1) + ” of outer loop”)
for var2 in range(2): #nested loop
print(var2 + 1)
print(“Out of inner loop”)
print(“Out of outer loop”)
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
Q. Program to print the pattern for a number input by the user.
#Program 6-17
#Program to print the pattern for a number input by the user
#The output pattern to be generated is
#1
#1 2
#1 2 3
#1 2 3 4
#1 2 3 4 5
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:
Enter a number to generate its pattern = 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Q. Program to find prime numbers between 2 to 50 using nested for loops.
#Program 6-18
#Use of nested loops to find the prime numbers between 2 to 50
num = 2
for i in range(2, 50):
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:
2 is a prime number
3 is a prime number
5 is a prime number
7 is a prime number
11 is a prime number
13 is a prime number
17 is a prime number
19 is a prime number
23 is a prime number
29 is a prime number
31 is a prime number
37 is a prime number
41 is a prime number
43 is a prime number
47 is a prime number
Bye Bye!!
Flow of Control in Python Class 11 Notes
Q. Write a program to calculate the factorial of a given number.
#Program 6-19
#The following program uses a for loop nested inside an if..else
#block 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
Computer Science Class 11 Notes
- Unit 1 : Basic Computer Organisation
- Unit 1 : Encoding Schemes and Number System
- Unit 2 : Introduction to problem solving
- Unit 2 : Getting Started with Python
- Unit 2 : Conditional statement and Iterative statements in Python
- Unit 2 : Function in Python
- Unit 2 : String in Python
- Unit 2 : Lists in Python
- Unit 2 : Tuples in Python
- Unit 2 : Dictionary in Python
- Unit 3 : Society, Law and Ethics
Computer Science Class 11 MCQ
- Unit 1 : Basic Computer Organisation
- Unit 1 : Encoding Schemes and Number System
- Unit 2 : Introduction to problem solving
- Unit 2 : Getting Started with Python
- Unit 2 : Conditional statement and Iterative statements in Python
- Unit 2 : Function in Python
- Unit 2 : String in Python
- Unit 2 : Lists in Python
- Unit 2 : Tuples in Python
- Unit 2 : Dictionary in Python
- Unit 3 : Society, Law and Ethics
Computer Science Class 11 NCERT Solutions
- Unit 1 : Basic Computer Organisation
- Unit 1 : Encoding Schemes and Number System
- Unit 2 : Introduction to problem solving
- Unit 2 : Getting Started with Python
- Unit 2 : Conditional statement and Iterative statements in Python
- Unit 2 : Function in Python
- Unit 2 : String in Python
- Unit 2 : Lists in Python
- Unit 2 : Tuples and Dictionary in Python
- Unit 3 : Society, Law and Ethics