Share with others

Table of Content

# Assignment of Python Loops

## ASSIGNMENT SET – 1

Time: 30 min                                                                                   M.M. – 20

Instructions:

• All Questions are compulsory
• Q1 to Q4 carry 1 mark
• Q5 to Q9 carry 2 marks
• Q10 to Q11 carry 3 marks

Q1. Write the output of the following:

```          for i in (1,3,5):
print(i)```

Q2. How many times the following loop execute?

```i=5
while i > 0:
print(i)
i=i+1
print("Loop End")```

Q3. Identify the error in the following statement and rectify it.

For i in range(5):

Q4. How many times “Hello” will print in the following code:

```for i in range(-10,5,1):
print("Hello")```

Q5. Write a program to print first 10 even numbers.

Q6. Convert the following code to while loop:

```for i in range(10,20,5):
print(i)```

Q7. Write the output of the following code :

```a=18
b=0
while a>b:
a=a-2
b=b+4
print(a)```

Q8. Write the output of the following code :

```                                 for i in range(10, 4, -2):
print("@"*i)```

Q9. Accept a number from the user and display it’s table in the following format:

If number input is 7 then output should be

7 * 1 = 7

7 * 2 = 14

Display this till

7 * 10 = 70

Q10. Write a program to check whether a number accepted from user is prime or not.

Q11. Write the code to display the following pattern

1

1   2

1   2   3

1   2   3   4

1   2   3   4   5

## ASSIGNMENT PYTHON LOOPS SET – 1 (Solutions)

Ans1. Output will be

1

3

5

Ans2. Loop will execute Infinite Times

Ans3. Correct code is :

for i in range(5):

Ans4. Hello will print 15 times.

Ans5. Code to print first 10 even numbers is:

```for i in range(2,22,2):
print (i)```

Ans6. Code in while loop is:

```i=10
while(i<20):
print(i)
i+=5```

Ans7. Output is:

16

14

12

Ans8. Output is:

```@@@@@@@@@@
@@@@@@@@
@@@@@@```

Ans9. Code is as follows:

```num=int(input("Enter any number"))
for i in range(1,11):
print(num,"*",i,"=",num*i)```

Ans10.

```num=int(input("Enter any number"))
f=0
if num==1 or num==0:
f=1
for i in range(2,num):
if num%i==0:
f=1
if f==1:
print("Number is not prime")
else:
print("Number is prime")
```

Ans11.

```for i in range(1,6) :
for j in range(1,i+1):
print(j,end=" ")
print()```

# Assignment of Python Loops

## ASSIGNMENT SET – 2

Time: 30 min                                                                                   M.M. – 20

Instructions:

• All Questions are compulsory
• Q1 to Q4 carry 1 mark
• Q5 to Q9 carry 2 marks
• Q10 to Q11 carry 3 marks

Q1. Repetition of statement in program is called____________ (Iteration/Selection)

Q2. Write a statement in for loop to print the following pattern:

1       5       9       14     19

Q3. Write the output of the following:

```for i in "csiphub":
print(i,end='-')```

Q4. Convert the following loop into while loop:

```for j in range(5,9):
print(j)
```

Q5. What type of error is returned by the following statement?

```for i in 123:
print(i)```

Q6. Write the code in while loop which print “A” infinite times.

Q7. Write a program to accept a number from the user and display it’s factorial.

Q8. Write a program to print first 10 natural numbers in reverse order.

Q9. Write a program to accept a number from the user and display the sum of the digits.

Q10. Write a program to print the following pattern:

A

A  B

A  B  C

A  B  C  D

Q11. Write a program to accept a number from the user and display the reverse number.

## ASSIGNMENT PYTHON LOOPS SET – 2 (Solutions)

Ans1. Iteration

Ans2. for i in range(1,20,4):

print(i)

Ans3. c-s-i-p-h-u-b-

Ans4.

```j=5
while(j<9):
print(j)
j=j+1```

Ans5. TypeError: ‘int’ object is not iterable

Ans6.

```j=1
while(j>0):
print("A")

#Any other code can also work```

Ans7.

```num=int(input("Enter any number"))
f=1
for i in range(1,num+1):
f=f*i
print("Factorial is",f)```

Ans8.

```for i in range(10,0,-1):
print(i)```

Ans9.

```num=int(input("Enter any number"))
s=0
while(num):
r=num%10
s=s+r
num=num//10
print("Sum of digits is",s)```

Ans10.

```k=65
for i in range(1,5):
for j in range(i):
print(chr(k),end=" ")
k=k+1
print()
k=65```

Ans11.

```num=int(input("Enter any number"))
rn=0
while(num):
r=num%10
rn=rn*10+r
num=num//10
print("Reverse number is",rn)```

# Assignment of Python Loops

## ASSIGNMENT SET – 3

Time: 30 min                                                                                   M.M. – 20

Instructions:

• All Questions are compulsory
• Q1 to Q4 carry 1 mark
• Q5 to Q9 carry 2 marks
• Q10 to Q11 carry 3 marks

Q1. Out of while and for loop which is preferable when you don’t know the number of times the loop should run?

Q2. While loop will execute only when the condition given is true.(T/F)

Q3. Write the output of the following:

for i in (“String”):

if i==’t’:

break

print(ord(i))

Q4. Write a for loop which start from 7, stop at 13 and step is 2.

Q5. Write the output of the following:

for i in range(5):

for j in range(5):

break

print(“Hello”)

print(“Hello”)

Q6. What is pass statement in python?

Q7. Write a program to print “Hello” n times using while loop. (Accept n from user)

Q8. Accept any number from user and display sum of the square of each digit(if number is 134, then answer is : 1 + 9 + 16 =26)

Q9. Find errors in the following code and correct it by rewriting the code and underline the corrections.

For k in [1,2,3,4,5):

if k==3:

break:

else:

Print(k)

Q10. Write a program to print Fibonacci Series (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …) till n terms. (Accept n from the user)

Q11. Write a program to accept a number from the user and check whether it is palindrome or not.

## ASSIGNMENT PYTHON LOOPS SET – 3 (Solutions)

Ans1. while loop

Ans2. True

Ans3. 83

Ans4. for i in range(7,14,2):

Ans5.

```Hello
Hello
Hello
Hello
Hello
Hello```

Ans6. The pass statement is null operation. Nothing happens when it execute.

Ans7.

```num=int(input("Enter any number"))
while(num):
print("Hello")
num=num-1```

Ans8.

```nt = int(input("Enter number of term"))
s=0
while (nt):
r=nt%10
s=s+r*r
nt=nt//10
print("Sum of square of digits is ",s)```

Ans9. Corrected code is:

```for k in [1,2,3,4,5]:
if k==3:
break
else:
print(k)```

Ans10

```nt = int(input("Enter number of term"))
ft=0
st=1
tt=ft+st
print(ft)
print(st)
for i in range(nt-2):
print(tt)
ft=st
st=tt
tt=ft+st```

Ans11.

```num=int(input("Enter any number"))
ornum=num
rn=0
while(num):
r=num%10
rn=rn*10+r
num=num//10
if ornum==rn:
print("Number is Palindrome")
else:
print("Number is not a Palindrome")
```