Answer the following questions.
1-Write a for loop that will iterate from 0 to 20. For each iteration, it
will check if the current number is even or odd, and report that to the
screen (e.g. "2 is even").
for num in range (1 ,21) :
if num %2== 0:
print(num,'is even')
else:
print(num,'is odd'
2. Write a Python program to print odd numbers from 1 to 1000, in two
ways.
Solution:
first way:
for num in range (1 ,1001) :
if num % 2 == 1:
print (num)
Second way:
for num in range (1 ,1001) :
if num % 2 == 0:
continue
print (num)
1
3. Write a Python program to calculate area or circumference of
a circle.
Solution:
u = input (" Enter A for area or C for circumference of a circle : ")
r = float ( input (" Enter the radius of a circle : "))
pi = 3.14
if(u == "A"):
area = pi * r * r
print ("The area of a circle is: ",area
) elif (u == "C"):
circ = 2 * pi * r
print ("The circumference of a circle is: ",circ )
4. Write a Python program to multiply all numbers in the given list,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
Solution:
mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
prod = 1
for i in mylist :
prod = prod * i
print ( prod )
2
5. Write a Python program to get the sum of even numbers from 1
to 100.
Solution:
sum = 0
for i in range (1 ,11):
if(i % 2 == 0):
sum = sum +
i print (sum)
6. Write a Python program to find the sum of squares of each elements
of the list using for loop.
numbers = [3, 5, 23, 6, 5, 1, 2, 9, 8]
sum= 0
for num in numbers:
sum= sum+ num ** 2
print("The sum of squares is: ", sum)
7. Create a user login page that prompts for password.
user_name=input('Enter user name')
user_password=int(input('enter your password'))
if user_name=="Ahmed" and user_password==1410:
print('welcom')
else:
print('wrong')
3
8. Create a user login page that prompts for password and it allow four attempts
for incorrect.
tries=4
mainpassword='Ahmed'
user_pass=input('write your pass')
while user_pass!=mainpassword:
tries=tries-1
print('wrong pass')
user_pass=input('write your pass')
if tries==0:
print('wrong and you finished your tries')
break
else:
print('correct pass')
9. Printing Multiplication Table Using Python Nested For Loops
for i in range (1,13):
for j in range (1,13):
product=i*j
print(i,'*',j,'=',product)
10. Write a program to print multiples of 5 ranging 1 to 100
for num in range (1 ,101) :
if num %5== 0:
print(num)
4
11. Loop through the range(1,20) :
(a)Continue to the next iteration if i is 10
(b)Exit the loop when i is 3
(a)
for x in range (1,20):
if x== 10:
continue
print(x)
(b)
for x in range (1,20):
print(x)
if x== 10:
break
12. Create a mixed list (containing string, number and a list). Print the
list
13. Create a nested list. Print the list.
14. Create a list containing 5 numbers then Add ‘Ahmed’ in index 3 to
this list . print the list
5
15. Implement a program that requests the student's degree in the course and determines
the student's grade whether it is A, B, C, or F
16. The student is considered successful in the course if he attends more than 70% of
the classes and obtains more than 40 marks on the final exam. Determine whether
the student is successful or not.