Professional Documents
Culture Documents
Coding Revision
Coding Revision
Coding Revision
Week 1 to 4
Question 1:
Write a Python program to find the smallest and largest of three
numbers.
Sample Input: Enter the three numbers: 1,2,3
Sample Output: Largest:3 Smallest:1
Solution:
#this program will find the smallest and largest number of 3 numbers
entered
Question 2:
Write a python program that will print the following:
1 3 5 7 9 11
10 8 6 4 2 0
(Hint: After 11 the number gets subtracted by 1 then it gets
subtracted by 2)
Solution 1:
Firstnumberset = 1
#for loop, to run 6 numbers adding 2 each time
for eachPass in range(6):
print(Firstnumberset, end=" ")
Firstnumberset = Firstnumberset + 2
Question 3:
Write a python program that will accept 10 numbers from the user and
find the number of odd and even numbers.
Sample Input: 22, 11, 6, 99, 100
Sample Output: There are 2 odd numbers and 3 odd numbers
Solution:
even=0
odd=0
Solution 1:
Days28=['February']
Days30=['April', 'June', 'September', 'November']
Days31=['January', 'March', 'May', 'July', 'August', 'October',
'December']
if month in (Days28):
print(month, "has 28 Days excluding leap years.")
if month in (Days30):
print(month, "has 30 Days.")
if month in (Days31):
print(month, "has 31 Days.")
else:
print("Please input a valid month.")
Solution 2:
x=str(input("Please enter the month name: "))
if x=="February":
print("Number of days: 28/29 days")
elif x in ("April","June","September","November"):
print("Number of days: 30")
elif x in ("January","March","May","July","August","October",
"December"):
print("Number of days: 31")
else:
print("Please type the correct month name")
Question 5:
Write a program that takes input from the user and when the user press
enter to quit the program gives the sum of all even numbers and the
sum of all odd numbers.
Sample Input:
Enter a number 2
Enter a number 4
Enter a number 3
Enter a number 5
Enter a number (enter)
Output
Sum of even number: 6
Sum of Odd number: 8
Solution 1:
#sums the even and odd numbers
#Create variables
Evennumber=0
Oddnumber=0
Userinput=0 # User input
Question 6:
Write a program to check whether the user input number is a prime
number or not.
Sample:
Input
Enter number: 8
Output
Number 8 is not prime number.
Solution:
n = int(input("Please enter a whole number: "))
if n ==1: # is the number is one, exit the script
print("1 is not a prime number.")
else:
for i in range (2,n):
if n%i == 0: #check if divisible with a remainder
print(n, "is not a prime number.") # no remainder
break
elif i == n-1: #always a remainder, not divisible
print(n, "is a prime number.")
Question 7:
Write a program to print the numbers in the following format:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Solution:
a=[]
for i in range(1,6):
a.append(i)
for j in range(len(a)):
print(a[j], end=" ")
print('\n')
Question 8:
Write a program that accepts a sentence and calculate the number of
letters and digits. Suppose the following input is supplied to the
program:
hello class! 123
Then, the output should be:
LETTERS 10
DIGITS 3
Solution:
sentence=input("Enter any sentence:")
letters=numbers=0
for x in sentence:
chvalue=ord(x)
if (chvalue>=65 and chvalue<=90)or(chvalue>=97 and chvalue<=122):
letters+=1
elif chvalue>=49 and chvalue<=57:
numbers+=1
print("LETTERS",letters)
print("DIGITS",numbers)