Page 148 - Dig_CodeAI_V2.1_Class_8
P. 148
Python Critical Thinking
Information Literacy
D. Write a program to find sum of all elements in a list using functions.
E. Type the given program and view the output.
Code:
DAYS = [
("Sunday", 1),
("Monday", 2),
("Tuesday", 3),
("Wednesday", 4),
("Thursday", 5),
("Friday", 6),
("Saturday", 7)
]
for day, number in DAYS:
print(day, ":", number)
F. Type the given program to find maximum and minimum number in a list and view the output.
Code:
lst = []
num = int(input("How many numbers: "))
for n in range(num):
numbers = int(input("Enter number "))
lst.append(numbers)
print("Maximum element in the list is :", max(lst))
print("Minimum element in the list is :", min(lst))
G. Type the given program to enter a number from the keyboard and then it will print:
the sum of the digits of the number
the reverses the number
Code:
sum = 0
rev = 0
number=int(input("Enter a number: "))
n = number
while (number):
digit = number % 10
sum += digit
rev = rev * 10 + digit
number //= 10
print("Reverse of number is : ",rev)
print ("Sum of digits of the number is : ",sum)
146 DigiCode AI (Ver. 2.1)-VIII

