Module -1:
Q1 - Implement a code which prompts user for Celsius temperature, convert the temperature to Fahrenheit, and
print out the converted temperature by handling the exception.
try:
Ctemp = int(input("Enter the temperature in Celsius: "))
Ftemp = (Ctemp * 9/5) + 32
print("The temperature in Farenheit is " + str(Ftemp) + " F")
except ValueError:
print("Enter the temperature in number format")
Q2 - Write a python program to calculate the area of circle, rectangular and triangle. Print the results.
import math
shape = input("Enter the shape whose area you want to find: ")
[Link]()
area = 0
if shape == 'circle':
radius = int(input("Enter the radius of the circle: "))
area = 3.14 * radius * radius
print("The area of circle is " + str(area))
elif shape == 'triangle':
s1 = int(input("Enter the first side of the triangle: "))
s2 = int(input("Enter the second side of the triangle: "))
s3 = int(input("Enter the third side of the triangle: "))
s = (s1+s2+s3)/2
sqrA = s *(s-s1)*(s-s2)*(s-s3)
area = [Link](sqrA)
print("The area of triangle is " + str(area))
elif shape == 'rectangle':
length = int(input("Enter the length of the rectangle: "))
breadth = int(input("Enter the breadth of the rectangle: "))
area = length*breadth
print("The area of rectangle is " + str(area))
Q3 - Write a program using function to find out the given string is palindrome or not.
def palindrome(string):
rev_str = string[::-1]
if string == rev_str:
print(string + " is a palindrome")
else:
print(string + " isnt a palindrome")
string = input("Enter the string to check for palindrome: ")
palindrome(string)
Q4 - Write a python program to create a function called collatz() which reads as parameter named number. If the
number is even it should print and return number//2 and if the number is odd then it should print and return
3*number+1. The function should keep calling on that number until the function returns a value 1.
def collatz(number):
if number % 2 == 0:
print(number//2)
return number//2
else:
print(3*number+1)
return 3*number + 1
num = int(input("Enter a number: "))
while True:
num = collatz(num)
if num == 1:
break
Q5 - Write a Python program to check whether a given number is even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print(str(num) + " is even")
else:
print(str(num) + " is odd")
Q6 - Define a Python function with suitable parameters to generate prime numbers between two integer values.
Write a Python program which accepts two integer values m and n (note: m>0, n>0 and m < n) as inputs and pass
these values to the function. Suitable error messages should be displayed if the conditions for input values are not
followed.
def is_prime(m,n):
if m<=0 or n<=0:
print("One of the values entered is zero")
elif m>n:
print(str(m) + " isnt lesser than " + str(n))
for i in range(m,n+1):
flag = 0
if i == 1:
continue
x = int(i/2)
for j in range(2,x+1):
if i%j == 0:
flag = 1
break
if flag == 0:
print(str(i) + " is prime \n")
m = int(input("Enter the first number: "))
n = int(input("Enter the second number: "))
is_prime(m,n)
Q7 - Define a Python function with suitable parameters to generate first N Fibonacci numbers. The first two
Fibonacci numbers are 0 and 1 and the Fibonacci sequence is defined as a function F as Fn = Fn-1 + Fn-2. Write a
Python program which accepts a value for N (where N >0) as input and pass this value to the function. Display suitable
error message if the condition for input value is not followed.
def fibonacci(N):
f1 = 0
f2 = 1
i = 0
if N == 1:
print('0')
elif N == 2:
print('0 1')
else:
print('0 1',end = " ")
while i<N:
f3 = f1 + f2
print(f3 ,end = " ")
f1 = f2
f2 = f3
i = i + 1
N = int(input("Enter the range for the series: "))
if N == 0:
print("Enter a number greater than 0")
else:
fibonacci(N)
Q8 - Write a function that computes and returns addition, subtraction, multiplication, division of two integers. Take
input from user.
def calc(num1,num2,operator):
if operator == '+':
return num1+num2
elif operator == '-':
return num1-num2
elif operator == '*':
return num1*num2
elif operator == '/':
return num1/num2
else:
return "Invalid operator specified"
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
op = input("Enter the operator to use: ")
print("The result is " + str(calc(num1,num2,op)))
Module -2:
Q1 - For a given list num=[45,22,14,65,97,72], write a python program to replace all the integers divisible by 3 with
“ppp” and all integers divisible by 5 with “qqq” and replace all the integers divisible by both 3 and 5 with “pppqqq”
and display the output.
num=[45,22,14,65,97,72]
for i in range(len(num)):
if num[i] % 3 == 0 and num[i] % 5 == 0:
num[i] = 'pppqqq'
elif num[i] % 3 == 0:
num[i] = 'ppp'
elif num[i] % 5 == 0:
num[i] = 'qqq'
print(num)
Q2 - Write a Python program that accepts a sentence and find the number of words, digits, uppercase letters and
lowercase letters.
string = input("Enter the string: ")
wcount = dcount = ulcount = llcount = 0
for i in string:
if [Link]():
wcount +=1
if [Link]():
dcount +=1
if [Link]():
ulcount +=1
if [Link]():
llcount +=1
print("The number of words = " + str(wcount))
print("The number of digits = " + str(dcount))
print("The number of uppercase letters = " + str(ulcount))
print("The number of lowercase letters = " + str(llcount))
Q3 - Create a function to print out a blank tic-tac-toe board
board = {0:' ',1:' ',2:' ',3:' ',4:' ',5:' ',6:' ',7:' ',8:' '}
def print_board():
print(board[0]+'|'+board[1]+'|'+board[2])
print('-+-+-')
print(board[3]+'|'+board[4]+'|'+board[5])
print('-+-+-')
print(board[6]+'|'+board[7]+'|'+board[8])
print_board()
print("\n")
Q4 - Develop a program to accept a sentence from the user and display the longest word of that sentence along with
its length
string = input("Enter a sentence: ")
list = [Link]()
max_pos = 0
max = len(list[0])
i = 1
while i<len(list):
if max < len(list[i]):
max = len(list[i])
max_pos = i
i = i + 1
print("Word with maximum length is - " + list[max_pos] + " : " + str(max))
Q5 - Write a python program to check whether a given string is palindrome or not
def palindrome(string):
rev_str = string[::-1]
if string == rev_str:
print(string + " is a palindrome")
else:
print(string + " isnt a palindrome")
string = input("Enter the string to check for palindrome: ")
palindrome(string)
Q6 - Write a python program to display a given substring in main string
string = input("Enter the string: ")
print(string)
while True:
sindex = int(input("Enter the start index for substring: "))
if sindex<0 or sindex>len(string):
print("\nEnter proper start index\n")
continue
eindex = int(input("Enter the end index for substring: "))
if eindex<0 or eindex>len(string):
print("\nEnter proper ending index\n")
continue
else:
break
substring = string[sindex:eindex+1]
print("The substring is " + substring)
Q7 - Write a python program to accept N numbers from user. Find sum of all even numbers and product of all odd
numbers in entered list.
N = int(input("Enter the largest range value: "))
list = []
for i in range(N):
num = int(input("Enter number " + str(i+1) + " : "))
list = list + [num]
sum = 0
product = 1
for i in list:
if i%2 == 0:
sum = sum + i
else:
product = product * i
print("The sum of even numbers is " + str(sum))
print("The product of all odd numbers is " + str(product))