Learn Python for & while
- Categories Python, Python loops, Python
In this post we will cover loops in Python. You can watch our videos on forloops in Python – Click Here and while loop in Python – Click here
Q. 1 Why we used for loop? Give example of same?
Ans.
- A for loop is used for iterating over a sequence (that is either a list, a string, a tuple, a dictionary etc.)
- The else keyword in a for loop specifies a block of code to be executed when the loop is finished.
- Example:
S = “code”
for x in S:
print(x)
- here we have a string so x will take on each letter one by one and loop will execute 4 times and print the value of X.
Q.2 What is else in for loop? Give an example of for-else?
Ans.
- The else keyword in a for loop specifies a block of code to be executed when the loop is finished
- The else block will NOT be executed if the loop is stopped by a break
- Example:
for x in range(6):
print(x)
else:
print(“else statement”)
Q. 3 Which are two types of else clauses in Python?
Ans. The two types of python else clauses are:
else in an if statement– the else clause of an if statement is executed when the condition of the if statement results into false
else in a loop statement-The else clause of a loop is executed when the loop is terminating normally. i.e. when its test- condition has gone false for a while loop or when the for loop has executed for the last value in sequence
Q. 4 Is there any limit on number of statements that can appear in a for block.
Ans. No
Q. 5 Write a short note on range function.
Ans.
- range() function is used to generate a sequence of numbers.
- It takes in 3 arguments out of which 2 are optional.
- Syntax: range(start, stop, step) takes three arguments (Default: Start 0, Step 1)
- Some important point on range function is that it works only with integers values
- All three arguments can be positive or negative and The step value must not be zero.
Q. 6 What is while loop?
Ans.
With the while loop we can execute a set of statements as long as a condition is true.
With the else statement we can run a block of code once when the condition no longer is true:
E.g.
i = 1
while i < 6:
print(i)
i += 1
Q. 7 What is difference between break and continue?
Ans.
In Python, break and continue statements can alter the flow of a normal loop.
Loops iterate over a block of code until the test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking test expression.
The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.
Q.8 What is pass in Python?
Ans. In python pass statement does nothing. It is just an indication to interpreter to move to next executable statement.
Q. 9 Write the output of the following Python code :
for i in range(1, 5):
for j in range(1, 5):
print(i * j, end= ” “)
print()
Ans.
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Q.10 Find and write the output of the following python code:
a=['Cat','Dog','cat','Dog']
def manip(f1):
animal={}
for index in f1:
if index in animal:
animal [index] += 1
else:
animal[index] =1
return(animal)
ret = manip(a)
print (ret)
print (len(ret))
Ans.
{‘Cat’: 1, ‘Dog’: 2, ‘cat’: 1}
3
Q. 11 Rewrite the following code in python after removing all syntax error(s). Underline each correction done in the code.
i = 0
while i < 10
if i % 2 = 0:
print(i,"is even")
elif:
print (i,"is odd")
i = i + 1
Ans.
i = 0
while i < 10:
if i % 2 == 0:
print(i,"is even")
else:
print (i,"is odd")
i = i + 1
Q. 12 What is the output of this program?
for i in range(1,11):
if(i==3):
print("hello", end=' ')
continue
if(i==8):
break
if(i==5):
pass
else:
print(i, end=' ');
Ans. 1 2 hello 4 6 7
Q. 13 Write a program that uses exactly four for loops to print the sequence of letters bellow.
A A A A A A A B B B B B C C C C E E E
Ans.
for i in range(7):
print(‘A’, end= ” “)
for j in range(5):
print(‘B’, end= ” “)
for k in range(4):
print(‘C’, end= ” “)
for L in range(3):
print(‘E’, end= ” “)
Q. 14 Rewrite the following Python code after removing all syntax errors. Underline each correction done in the code.
for p in range (3)
for q in range (3)
Print(p*q, end=" ")
else:
print("Outer loop ends")
Ans.
for p in range (3):
for q in range (3):
print(p*q, end=" ")
else:
print("Outer loop ends")
Q. 15 Write the output of the following Python code :
for a in [1, 2, 3]:
for b in [4, 5, 6]:
print(a, b)
Ans.
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
Q.16 Write the output of the following Python code :
for i in range (4):
for j in range (5):
if i+1 == j or j + i == 4:
print ("+", end= ' ')
else:
print("o", end= ' ')
print()
Ans.
+ + o
+ + o
+ + o
+ + o
Q. 17 Write a program to input any number and to print all the factors of that number.
Ans.
n=int(input("Enter the number"))
for i in range(2,n):
if n%i == 0:
print (i,"is a factor of",n)
Q.18 Write a program to print the pyramid.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Ans.
for i in range(1,6):
for j in range(1,i+1):
print(i,end=" ")
print ()
Q.19 Write a program to find and display the prime number between N numbers
Ans.
N=int(input("Enter the number"))
for a in range (2, N):
Prime = 1
for i in range (2, a):
if a%i == 0 :
Prime = 0
break
if Prime == 1:
print (a,"is prime")
Q. 20 Program to find the sum of all numbers stored in a list.
Ans.
numbers = [6, 9, 2, 1, 4, 5, 3, 7, 10]
sum = 0
for val in numbers:
sum = sum+val
print("The sum is", sum)
Q. 21 Rewrite the following Python code after removing all syntax errors. Underline each correction done in the code.
digits = 3, 7, 1, 6
for i in digits:
print(i)
else:
print(No items left.)
Ans.
digits = [3, 7, 1, 6]
for i in digits:
print(i)
else:
print("No items left.")
Q. 22 Write a program to find the sum of the series: s= (1) + (1+2) + (1+2+3) +(1+2+3+4..n) .
Ans.
sum =0
n= int(input("Enter the value of n: "))
for i in range(1, n+2):
term=0
for j in range(1,i):
term += j
print("Term",(i-1), ":",term)
sum+= term
print("Sum of",n,"term is", sum)
Q. 23 Rewrite following code fragment using for loop
i=1
while i < 20:
if i % 4 == 2:
print (i,"mod",4,"= 2")
i = i + 1
Ans.
for i in range(1,20,1):
if i % 4 == 2:
print(i,"mod", 4, "= 2")