Which of the following is not used as loop in Python?
- for loop
- while loop
- do-while loop
- All of them
Answer
do-while loop
Reason — Python does not support do-while loop as a built-in looping construct. Only for loop and while loop are used to perform iterative tasks in Python.
Which of the following statement is a fixed iterative loop?
- for
- while
- if
- if-else
Answer
for
Reason — The for loop is the best example of a fixed iterative loop because the number of iterations is known and fixed in advance using the range() function or any other iterable.
How is the range() function typically used in a for loop?
- range(start, end)
- range(end)
- range(start, end, step)
- range(step)
Answer
range(start, end, step)
Reason — The range() function can be used with one, two or three parameters. The complete form is range(start, end, step) where start is the initial value, end is the stop value and step is the update value.
Which of the following loop does not execute even once, if the condition is false in the beginning?
- infinite
- while
- for
- none
Answer
while
Reason — The while loop checks the condition before executing the loop body. If the condition is false at the very beginning, the control will not enter the loop block and hence the loop will not execute even once.
Which of the following statements is most appropriate to illustrate the difference between a for loop and a while loop?
- A for loop is used for fixed iterations, while a while loop is used for conditional statements.
- A for loop is more concise than a while loop.
- There is no difference between for loop and while loop.
- They are not interchangeable.
Answer
A for loop is used for fixed iterations, while a while loop is used for conditional statements.
Reason — The for loop is a fixed iterative loop where the number of iterations are known in advance. The while loop is an unfixed iterative loop which executes based on a conditional expression and continues until the condition becomes false.
Which of the following statement holds valid with Python for loop?
- initial value is an optional
- stop value is mandatory
- step value is an optional
- none
Answer
stop value is mandatory
Reason — The for loop is a fixed iterative loop where the number of iterations is determined using the range() function. In range(), the initial value and step value are optional because their default values are 0 and 1 respectively, but the stop value is mandatory as it specifies the ending limit of the loop.
Which of the following loop does not need to check the condition before the execution begin?
- User controlled loop
- for
- while loop
- all
Answer
for
Reason — The for loop works with a predefined sequence or iterable, so the number of iterations is fixed in advance unlike the while loop which depends on a condition.
The purpose of the range function in a for loop is to ...............
- create a list of numbers
- define the start and end of the loop
- generate a sequence of numbers
- specify the step size of the loop
Answer
generate a sequence of numbers
Reason — The range() function is a built-in function that generates a sequence of integer numbers based on the initial value, stop value and update parameters. This sequence is then used by the for loop for iteration.
To find the sum of whole numbers up to 10, a loop runs:
- once
- ten times
- eleven times
- any number of times
Answer
eleven times
Reason — Whole numbers start from 0. Therefore, the whole numbers up to 10 are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, which makes a total of 11 numbers. Hence, the loop runs eleven times.
Using ............... loop, an infinite loop can be created.
- Fixed iterative loop
- while
- for
- all
Answer
while
Reason — An infinite loop can be created with the help of the while loop by giving an ever-true condition. Since the condition will never become false, the loop will keep on executing infinitely.
A loop statement is given as:
for i in range(10):For how many times will the given loop statement be executed?
- none
- 9 times
- 10 times
- infinite
Answer
10 times
Reason — The function range(10) generates a sequence of numbers from 0 to 9, which contains 10 values. Hence, the loop will execute 10 times.
When a code allows to use one loop inside another loop, known as ...............
- finite loop
- loop in loop
- nested loop
- infinite loop
Answer
nested loop
Reason — A loop written inside another loop is called a nested loop. In this type of loop, the inner loop executes completely for every iteration of the outer loop.
Predict the output of the following snippet:
m, n = 2, 15
for i in range(1, 5):
m = m + 1
n = n - 1
print("m =", m)
print("n =", n)Answer
m = 6
n = 11
Let’s break down the code line by line:
1. m, n = 2, 15: This statement uses multiple assignment, where m is assigned 2 and n is assigned 15 in a single statement.
2. for i in range(1, 5):: The for loop runs for the values 1, 2, 3, 4. Therefore, the loop executes 4 times.
3. Iterations
- First Iteration: m = 3, n = 14
- Second Iteration: m = 4, n = 13
- Third Iteration: m = 5, n = 12
- Fourth Iteration: m = 6, n = 11
4. print("m =", m): Displays m = 6
5. print("n =", n): Displays n = 11
6. Therefore, the final output is:
m = 6
n = 11
Predict the output of the following snippet:
k = 1
i = 2
while(i < 6):
k = k * i
i = i + 1
print(k)Answer
120
Let’s break down the code line by line:
1. k = 1: The variable k is assigned the value 1.
2. i = 2: The variable i is assigned the value 2.
3. while(i < 6):: The while loop iterates as long as i < 6.
4. The iterations are as follows:
- i = 2: k = 1 * 2 = 2, then i becomes 3
- i = 3: k = 2 * 3 = 6, then i becomes 4
- i = 4: k = 6 * 4 = 24, then i becomes 5
- i = 5: k = 24 * 5 = 120, then i becomes 6
5. When i = 6, the condition becomes false and the loop terminates. Hence, the final value of k = 120 is printed.
Predict the output of the following snippet:
p = 100
for i in range(1, 5):
p = p - 1
print(p, end=' ')Answer
99 98 97 96
Let’s break down the code line by line:
1. p = 100: The variable p is assigned the value 100.
2. for i in range(1, 5):: The for loop runs for the values 1, 2, 3, 4. Therefore, the loop executes 4 times.
3. Iterations of the loop:
- First Iteration: p = 100 - 1 = 99
- Second Iteration: p = 99 - 1 = 98
- Third Iteration: p = 98 - 1 = 97
- Fourth Iteration: p = 97 - 1 = 96
4. print(p, end=' '): Displays the value of p after each iteration in the same line separated by spaces.
5. Therefore, the final output is: 99 98 97 96
Predict the output of the following snippet:
p = -99
while(p < 0):
p = p + 9
print(p, end=' ')Answer
-90 -81 -72 -63 -54 -45 -36 -27 -18 -9 0
Let’s break down the code line by line:
1. p = -99: The variable p is assigned the value -99.
2. while(p < 0):: The while loop executes as long as the value of p is less than 0.
3. In each iteration, the statement p = p + 9 increases the value of p by 9 and print(p, end=' ') displays the updated value in the same line separated by spaces.
4. The values of p become -90, -81, -72, -63, -54, -45, -36, -27, -18, -9 and 0.
5. When p becomes 0, the condition p < 0 becomes False and the loop terminates.
6. Therefore, the output is -90 -81 -72 -63 -54 -45 -36 -27 -18 -9 0.
Predict the output of the following snippet:
for i in range(4):
for j in range(i, -1, -1):
print(j, end=' ')
print()Answer
0
1 0
2 1 0
3 2 1 0
The outer loop runs from 0 to 3. For each value of i, the inner loop starts from i and decreases up to 0 using the step value -1. Therefore, when i = 0, the output is 0; when i = 1, the output is 1 0; when i = 2, the output is 2 1 0; and when i = 3, the output is 3 2 1 0. The print() statement moves the output to the next line after each iteration of the outer loop.
Predict the output of the following snippet:
for x in range(1, 4):
for y in range(1, 3):
print(x*y, end=' ')
print()Answer
1 2
2 4
3 6
The outer loop runs with values 1, 2, and 3. For each value of x, the inner loop runs with values 1 and 2. During each iteration, the product of x*y is printed. Thus, when x = 1, the outputs are 1 and 2; when x = 2, the outputs are 2 and 4; and when x = 3, the outputs are 3 and 6. The print() statement moves the cursor to the next line after each row is completed.
Assertion (A): Loop is a repetitive structure in which a statement or a set of statements are executed until the desired number of iterations is over.
Reason (R): The for loop is used to repeat the execution of a block of statements for a fixed number of times.
Based on the above assertion and reasoning, pick an appropriate statement from the options given below:
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true and R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
- Both A and R are false.
Answer
Both A and R are true and R is not the correct explanation of A.
Reason — A loop is a repetitive structure used to execute statements repeatedly until the required number of iterations are over. The for loop is one type of loop that is used when the number of iterations is fixed. Therefore, both statements are true, but the second statement does not explain the first statement.
Assertion (A): The range() function is a built-in function which is used with the for loop.
Reason (R): This function doesn't require any parameter. It is used in for loop to generate the series such as even and odd numbers, positive and negative numbers, etc.
Based on the above assertion and reasoning, pick an appropriate statement from the options given below:
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true and R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
- Both A and R are false.
Answer
A is true but R is false.
Reason — The range() function is a built-in function used with the for loop to generate a sequence of numbers. In the range() function, the start value and step value are optional because their default values are 0 and 1 respectively. However, the stop value is mandatory. Therefore, the first statement is true but the second statement is false.
A Python program that takes a list of integers and tests the user's ability to:
- write a code in Python.
- use for loops for iteration to iterate over a list.
- apply conditional statements to check divisibility.
- print the results based on the condition.
A list of integers [10, 15, 24, 30, 20, 45, 25, 60] is provided.
The program counts and prints all numbers from the list that are divisible by 3 as well as 5.
The expected output:
The numbers divisible by 3 as well as 5 in the list are:
15
30
45
60
Number of elements divisible by 3 as well as 5: 4
An outline of the program is given below to make the task easier.
Now, fill in the blanks to complete the code. The program is given below:
# a program to display numbers divisible by 3 and 5 in the list
c = 0
My_List = [10, 15, 24, 30, 20, 45, 25, 60]
print("Given List:", My_List)
print("The numbers divisible by 3 as well as 5 in the list are:")
for ........... in My_List: # line 1
if(num%3 == 0 ......... num%5 == 0): # line 2
print(num)
c = ............... # line 3
print("Number of elements divisible by 3 as well as 5: ", c)Answer
(a) Line 1: num
(b) Line 2: and
(c) Line 3: c+1
The complete program is given below:
# a program to display numbers divisible by 3 and 5 in the list
c = 0
My_List = [10, 15, 24, 30, 20, 45, 25, 60]
print("Given List:", My_List)
print("The numbers divisible by 3 as well as 5 in the list are:")
for num in My_List: # line 1
if(num%3 == 0 and num%5 == 0): # line 2
print(num)
c = c+1 # line 3
print("Number of elements divisible by 3 as well as 5: ", c)Write Python code to display the first ten terms of the following series:
1, 4, 9, 16, ...............
for i in range(1, 11):
print(i*i, end=" ")1 4 9 16 25 36 49 64 81 100
Write Python code to display the first ten terms of the following series:
1, 2, 4, 7, 11, ...............
n = 1
for i in range(1, 11):
print(n, end=" ")
n = n + i1 2 4 7 11 16 22 29 37 46
Write Python code to display the first ten terms of the following series:
2, 5, 10, 17, ...............
n = 2
for i in range(1, 11):
print(n, end=" ")
n = n + (2*i + 1)2 5 10 17 26 37 50 65 82 101
Design Python code to find and display the sum of the following series:
s = 1 + 4 + 9 + ............... + 400
s = 0
for i in range(1, 21):
s = s + i*i
print("Sum of the series =", s)Sum of the series = 2870
Design Python code to find and display the sum of the following series:
s = (1*2) + (2*3) + ............... + (19*20)
s = 0
for i in range(1, 20):
s = s + (i * (i + 1))
print("Sum of the series =", s)Sum of the series = 2660
Write Python code to find the sum of the given series:
1 + (1+2) + (1+2+3) + ............... + (1+2+3+...............+n)
n = int(input("Enter the value of n: "))
s = 0
for i in range(1, n+1):
t = 0
for j in range(1, i+1):
t = t + j
s = s + t
print("Sum of the series =", s)Enter the value of n: 6
Sum of the series = 56
Write Python code to find the sum of the given series:
1 + (1*2) + (1*2*3) + ............... + (1*2*3*...............*n)
n = int(input("Enter the value of n: "))
s = 0
p = 1
for i in range(1, n+1):
p = p * i
s = s + p
print("Sum of the series =", s)Enter the value of n: 5
Sum of the series = 153
Write Python code to find the sum of the given series:
S = 1! + 2! + 3! + 4! + ............... + n!
[Hint: Factorial of 5! = 5*4*3*2*1]
n = int(input("Enter the value of n: "))
s = 0
f = 1
for i in range(1, n+1):
f = f * i
s = s + f
print("Sum of the series =", s)Enter the value of n: 6
Sum of the series = 873
Write a program to find and display the sum of any ten natural numbers.
s = 0
print("Enter any ten natural numbers:")
for i in range(10):
n = int(input())
s = s + n
print("Sum of ten natural numbers =", s)Enter any ten natural numbers:
5
4
6
7
3
2
9
8
10
23
Sum of ten natural numbers = 77
Write a program to input any 20 numbers (including positive and negative). The program displays the sum of all positive numbers and negative numbers separately.
ps = 0
ns = 0
for i in range(20):
n = int(input("Enter a number: "))
if(n > 0):
ps = ps + n
elif(n < 0):
ns = ns + n
print("Sum of positive numbers =", ps)
print("Sum of negative numbers =", ns)Enter a number: 10
Enter a number: -5
Enter a number: 6
Enter a number: 9
Enter a number: -23
Enter a number: -4
Enter a number: -6
Enter a number: -11
Enter a number: 22
Enter a number: 33
Enter a number: 44
Enter a number: 10
Enter a number: 12
Enter a number: 6
Enter a number: 7
Enter a number: 8
Enter a number: -9
Enter a number: -2
Enter a number: -3
Enter a number: 19
Sum of positive numbers = 186
Sum of negative numbers = -63
Write a program to enter a number and display the sum of its digits.
Sample Input: 642
Output: 12 (6+4+2 = 12)
n = int(input("Enter a number: "))
s = 0
while(n != 0):
d = n % 10
s = s + d
n = n // 10
print("Sum of digits =", s)Enter a number: 642
Sum of digits = 12
Write a Python code to check and display whether a number input by the user is a composite number or not.
[A number is said to composite, if it has one or more factors excluding 1 and the number itself. For example: 4, 6, 8, 9, ...............]
c = 0
n = int(input("Enter a number: "))
for a in range(1, n+1):
if n % a == 0:
c = c + 1
if c > 2:
print(n, "is a composite number")
else:
print(n, "is not a composite number")Enter a number: 9
9 is a composite number
Enter a number: 13
13 is not a composite number
Write a program to input 10 different numbers. Display the greatest and the smallest numbers among them.
n = int(input("Enter a number: "))
greatest = n
smallest = n
for i in range(1, 10):
n = int(input("Enter a number: "))
if(n > greatest):
greatest = n
if(n < smallest):
smallest = n
print("Greatest number =", greatest)
print("Smallest number =", smallest)Enter a number: 12
Enter a number: 56
Enter a number: 45
Enter a number: 78
Enter a number: 19
Enter a number: 20
Enter a number: 30
Enter a number: 41
Enter a number: 87
Enter a number: 90
Greatest number = 90
Smallest number = 12
Write a program to input a number. Check and display whether it is a Niven Number or not. (A number is said to be Niven when it is divisible by the sum of its digits).
Sample Input: 126
Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.
n = int(input("Enter a number: "))
p = n
s = 0
while n > 0:
d = n % 10
s = s + d
n = n // 10
if p % s == 0:
print(p, "is a Niven Number")
else:
print(p, "is not a Niven Number")Enter a number: 126
126 is a Niven Number
Enter a number: 167
167 is not a Niven Number
Write a program to input a number and display the new number after reversing the digits.
Sample Input: 467
Sample Output: 764
n = int(input("Enter a number: "))
r = 0
while n > 0:
d = n % 10
r = r * 10 + d
n = n // 10
print("Reversed number:", r)Enter a number: 467
Reversed number: 764
Write a Python code to display the Mathematical Tables from 5 to 10. Each Mathematical Table should run for 10 iterations as shown in the given format:
Sample Output: Table of 5
5*1 = 5
5*2 = 10
...............
...............
5*10 = 50
for i in range(5, 11):
print("Table of", i)
for j in range(1, 11):
print(i, "*", j, "=", i*j)
print()Table of 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Table of 6
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
Table of 7
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
Table of 8
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
Table of 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
Table of 10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
Write a Python code to accept any 20 numbers and display only those numbers which are prime.
[Hint: A number is said to be prime, if it is divisible by 1 and the number itself.]
for i in range(1, 21):
n = int(input("Enter a number: "))
if n > 1:
c = 0
for j in range(1, n + 1):
if n % j == 0:
c = c + 1
if c == 2:
print(n, "is a prime number")Enter a number: 4
Enter a number: 5
5 is a prime number
Enter a number: 6
Enter a number: 7
7 is a prime number
Enter a number: 8
Enter a number: 9
Enter a number: 2
2 is a prime number
Enter a number: 3
3 is a prime number
Enter a number: 12
Enter a number: 14
Enter a number: 15
Enter a number: 16
Enter a number: 78
Enter a number: 91
Enter a number: 20
Enter a number: 17
17 is a prime number
Enter a number: 39
Enter a number: 41
41 is a prime number
Enter a number: 19
19 is a prime number
Enter a number: 29
29 is a prime number
A number is said to be 'Happy number' if eventual sum of the square of its digits results in 1. Write a Python code to input a number and check whether it is a Happy Number or not.
Sample Input: 31
Sample Output: 31 = 32 + 12 = 10 = 12 + 0 = 1
Hence, it is a Happy Number
n = int(input("Enter a number: "))
p = n
while n > 9:
s = 0
while n > 0:
d = n % 10
s = s + d*d
n = n // 10
n = s
if n == 1:
print(p, "is a Happy Number")
else:
print(p, "is not a Happy Number")Enter a number: 31
31 is a Happy Number
Enter a number: 12
12 is not a Happy Number
Write Python code to display the following pattern:
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
for i in range(1, 6):
for j in range(5, i - 1, -1):
print(j, end=" ")
print()5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Write Python code to display the following pattern:
1 3 5 7 9
1 3 5 7
1 3 5
1 3
1
for i in range(5, 0, -1):
for j in range(1, 2 * i, 2):
print(j, end=" ")
print()1 3 5 7 9
1 3 5 7
1 3 5
1 3
1
Write Python code to display the following pattern:
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
for i in range(1, 6):
for j in range(5, 5 - i, -1):
print(j, end=" ")
print()5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Write Python code to display the following pattern:
1
3 5
5 7 9
7 9 11 13
9 11 13 15 17
for i in range(1, 6):
n = 2 * i - 1
for j in range(1, i + 1):
print(n, end=" ")
n = n + 2
print()1
3 5
5 7 9
7 9 11 13
9 11 13 15 17
Write Python code to display the following pattern:
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
for i in range(1, 6):
for j in range(i, 6):
print(j, end=" ")
print()1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
Write Python code to display the following pattern:
9
9 7
9 7 5
9 7 5 3
9 7 5 3 1
for i in range(1, 6):
for j in range(9, 9 - (2 * i), -2):
print(j, end=" ")
print()9
9 7
9 7 5
9 7 5 3
9 7 5 3 1
What is a loop?
Answer
A loop is a repetitive control structure in which a statement or a set of statements are executed repeatedly until the given condition is false or the desired number of iterations are over.
Explain for loop with an example.
Answer
The for loop is a fixed iterative looping construct in Python. In this looping system, the number of iterations is fixed. The loop starts with a given value of the control variable and executes the block of statements by updating the control variable unless the last limit of the loop is reached. This loop is preferred when the user is aware of the number of times the process is to be repeated.
Syntax:
for <variable> in range(<parameters>):
<statement or a set of statements>
Example:
for i in range(1, 6):
print(i, end=' ')Output: 1 2 3 4 5
This code displays the numbers from 1 to 5 using a for loop.
What is significance of range() function in a for loop.
Answer
The range() function is a built-in function in Python which is used with the for loop to generate a sequence of integer numbers. It plays a significant role in defining the iteration limit of the for loop.
The range() function takes three parameters — initial value, stop value and update. The for loop iterates over the sequence generated by range() and executes the loop body for each value in the sequence. This makes the for loop simple, concise and easy to control.
What are the parameters needed to create a for loop?
Answer
The parameters needed to create a for loop are passed through the range() function. These are:
- Initial value — It is an optional parameter that defines the starting value of the loop. The default value is 0.
- Stop value — It is a mandatory parameter that indicates the end condition of the loop. The loop will execute up to stop value - 1.
- Update — It is an optional parameter used to increase or decrease the value of the control variable. The default update value is 1.
Write down the general format of:
(a) for loop
(b) while loop
Answer
(a) General format of for loop:
for <variable> in range(<parameters>):
<statement or a set of statements>
(b) General format of while loop:
while <conditional expression>:
statement 1
statement 2
...............
<update expression>
What is an infinite loop? Give an example.
Answer
An infinite loop or an endless loop is a looping construct in which the number of iterations will never come to an end. The set of statements within the loop block keeps executing infinitely because the given condition never becomes false.
Example:
a = 5
while(a > 0):
print(a*a)
a = a + 1In the above code, the value of 'a' will always be positive. Hence, the condition (a > 0) will never become false, resulting in never-ending iterations of the loop. Press Ctrl+C keys together to break the running of such an endless loop.
State one similarity and one difference between while and for loops.
Answer
Similarity: Both for loop and while loop are used to repeat the execution of a block of statements until a particular condition is satisfied.
Difference: The for loop is a fixed iterative loop in which the number of iterations is known in advance, whereas the while loop is an unfixed iterative loop in which the user may not be aware of the exact number of iterations that will take place during execution.
Distinguish between step loop and continuous loops.
Answer
The differences between step loop and continuous loop are:
| Continuous Loop | Step Loop |
|---|---|
| The control variable is updated (increased or decreased) only by 1 (one) after each iteration. | The control variable is updated by a given value (other than one) after each iteration. |
| The loop terminates when the control variable reaches the last limit. | The loop terminates when the value of the control variable reaches or exceeds the last limit of the range. |
Example: for n in range(1, 11): | Example: for n in range(1, 20, 2): |
What do you mean by a nested loop?
Answer
A nested loop is defined as a loop programmed within another loop. It is used in such a way that the inner loop repeats for a given number of times for each iteration of the outer loop.
When do you need to use a nested loop?
Answer
A nested loop is used when a loop has to be programmed within another loop to perform a specific task. In a nested loop, the inner loop repeats for a number of times for each iteration of the outer loop. Nested loops are widely used in pattern printing.