0% found this document useful (0 votes)
9 views9 pages

Chapter 1 Type C

The document contains a series of programming exercises for Class XII Computer Science focusing on Python. Each exercise includes a problem statement followed by a sample solution in Python code, covering various topics such as conditional statements, loops, functions, and data structures. The exercises aim to reinforce programming skills and understanding of Python syntax and logic.

Uploaded by

jayvi9919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Chapter 1 Type C

The document contains a series of programming exercises for Class XII Computer Science focusing on Python. Each exercise includes a problem statement followed by a sample solution in Python code, covering various topics such as conditional statements, loops, functions, and data structures. The exercises aim to reinforce programming skills and understanding of Python syntax and logic.

Uploaded by

jayvi9919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Class XII

Computer Science
Chapter 1 Python Revision Tour
Type C – Programming Practice

Q.1 Write a program to print one of the words negative, zero or positive. according to
whether variable x is less than 0, 0, or greater than 0 respectively.
Ans:
x = int (input ("enter the number "))
if x > 0:
print ("Positive number ")
elif x == 0 :
print ("Zero number ")
else :
print ("Negative number ")
Q.2 Write a program that return True if the input number is an even number, False
otherwise .
Ans:
x = int (input ("enter the number "))
if x%2 == 0:
print ("true ")
else :
print (" false ")
Q.3 Write a python program that calculates and prints the number of seconds in a year .
Ans:
print ( "For Non Leap year = " ,60*60*24*365, " , For Leap year = ", 60*60*24*366 )

Q.4 Write a python program that accepts two integers from the user and print a message
saying if first number is divisible by second number or if it is not.
Ans:
first = int(input("enter the first number "))
second = int(input("enter the second number "))
if first % second == 0 :
print ("first number is divisible by second number ")
else :
print ("first number is not divisible by second number ")

1
Q.5 Write a program that asks the user the day number in a year in the range 2 to 365 and
asks the first day of the year — Sunday or Monday or Tuesday etc. Then the program
should display the day on the day-number that has been input.

dayNames = ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY",


"SATURDAY", "SUNDAY"]

dayNum = int(input("Enter day number: "))


firstDay = input("First day of year: ")

if dayNum < 2 or dayNum > 365:


print("Invalid Input")
else:
startDay = dayNames.index(str.upper(firstDay))
currDay = dayNum % 7 + startDay - 1

if currDay >= 7:
currDay = currDay - 7

print("Day on day number", dayNum, ":", dayNames[currDay])

Q.6 One foot equals 12 inches. Write a function that accept a length written in feet as an
argument and returns this length written in inches. Write a second function that asks
the user for a number of feet and return this value. Write a third function that accept a
number of inches and display this to the screen. Use these three functions to write a
program that asks the user for a number of feet and tells them the corresponding
number of inches.
Ans:
def inch(feet): #1
return feet*12
def ask(): #2
ft=int(input("enter number in feet "))
return ft
def inch2(inch):
print(inch)
inch2(inch(ask()))

2
Q.7 Write a program that reads an integer N from the keyboards computes and displays
the sum of numbers from N to (2*N) if N is nonnegative. If N is a negative number,
then it’s the sum of the numbers from (2*N) to N. the starting and ending points are
included in the sum.
Ans:
sum = 0
n = int(input("enter the number "))
if n > 0 :
for i in range (n,n*2+1):
sum += i
else :
for i in range (2*n, n+1):
sum += i
print (sum)

Q.8 Write a program that reads a date as an integers in the format MMDDYYYY. The
program will call a function that prints print out the date in the format <month name>
<day>,<year>. Apr 30, 2021
Ans:
mon = ["january", "february","march" , "april", "may" , "june" , "july" , "august ",
"september ", "october ", "november" , 'december ']
a = int (input("enter the date = " ))
month = a // 1000000
date = (a % 1000000) // 10000
year = a % 10000
print (mon [ month - 1 ] , date ," , ", year )

Q.8 Write a program that prints a table on two columns – table that helps converting miles
into kilometers.
Ans:
print("Miles | Kilometer")
print("---------------------------")

for j in range (7) :


print(10**j , end= "\t" )
print(" | " , end= "\t")
print( 1.6093 * 10** j )
3
Q.9 Write a program that reads two times in military format and prints the number of hours
and minutes between the two times.
Ans:

first = int (input("please enter the first time = "))


sec = int (input ("please enter the second time = " ))

a = sec - first

print (a // 100, "hours " , a % 100, "min")

Q.10 Write a program that prompts for a phone number of 10 digit and two dashes, with
dashes after the area code and the next three number. Display if the phone number
enter is valid format or not and display if the phone number is valid or not.
Ans: 956-521-1111

num = input("enter your phone number = ")


if len(num)== 12 :
if num[3]== "-" :
if num[4:7].isdigit() :
if num [7]== "-":
if num[ 8 : 13 ].isdigit() :
if num[ : 3 ].isdigit() :
print("it is vaild number ")
else :
print("it is not vaild number ")
else :
print("it is not vaild number ")
else :
print("it is not vaild number ")
else :
print("it is not vaild number ")
else :
print("it is not vaild number ")
else :
print("it is not vaild number ")

4
Q.11 Write a program that should prompts the user to type some sentence (s) followed by
“enter ”. It should then print the original sentence (s) and the following statistics
relating to sentence (s):
o I = number of words
o II = numbers of character (including white-space and punctuation )
o III = percentage of character that are alphanumeric.
sen = input("enter a sentance = ")
count = 1
alp = 0
for j in sen :
if j == " " :
count += 1
elif j.isalnum() :
alp += 1
print("number of word is ",count)
print("number of characters ",len(sen))
print("percentage of alpha numeric = ", (alp / len(sen)) * 100)

Q.12 Create a dictionary whose keys are month name and whose values are number of days
in the corresponding month:
(a) ask the user to enter the month name and use the dictionary to tell how many days are in month
(b) print out all of the keys in alphabetical order.
(c) print out all of the months with 31 days.
(d) print out the (key - value) pair sorted by the number of the days in each month.

month = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 , "july" : 31 ,


"aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}
mon = input("enter the month name in short form = ")
print("number of days in ",mon,"=",month [ mon ])
print(“Keys(Month) in alphabetical order:”)
lst = list ( month . keys() )
lst.sort()
print( lst )
print( "month which have 31 days --- ")
for i in month :
if month [ i ] == 31 :

5
print( i )
print("month according to number of days ---")
print("feb")
for i in month :
if month [ i ] == 30 :
print(i)
for i in month :
if month [ i ] == 31 :
print( i )

Q.14 Write a short python code segment that swaps the values assigned to these two
variable and print the results.

first = "jimmy "


second = " johny "
first , second = second , first
print ("first = ", first , "second = " , second )

Q.15 Write a program that creates a list of all integers less then 100 that are multiples of 3 or
5.
lst = [ ]
for i in range (1,100):
if i % 3 == 0 or i % 5 == 0 :
lst += [ i ]
print (lst)

Q.16 Write a short python code segment that prints the longest word in the list of the word.
def longestword(a):
max1=len(a[0])
temp=a[0]

for i in a:
if(len(i)>max1):
max1=len(i)
temp=i
print("Longest word:",max1,temp)

a=["Hello","World","This","Is","Python"]
6
longestword(a)

Q.17 Write a program that rotates the elements of a list so that the element at the first
index moves to the second index, the element in the second index moves to the third
index, etc., and the element in the last index moves to the first index.

l = eval(input("Enter the list: "))


print("Original List")
print(l)
l = l[-1:] + l[:-1]
print("Rotated List")
print(l)
Q.18 Write a program that takes any two lists L and M of the same size and adds their
elements together to form a new list N whose elements are sums of the corresponding
elements in L and M. For instance, if L = [3, 1, 4] and M = [1, 5, 9], then N should
equal [4,6,13].
print("Enter two lists of same size")
L = eval(input("Enter first list(L): "))
M = eval(input("Enter second list(M): "))
N = []

for i in range(len(L)):
N.append(L[i] + M[i])

print("List N:")
print(N)

Q.19 Write a Python program that creates a tuple storing first 9 terms of Fibonacci series.

lst = [0,1]
a=0
b=1
c=0

for i in range(7):

7
c=a+b
a=b
b=c
lst.append(c)

tup = tuple(lst)

print("9 terms of Fibonacci series are:", tup)

Q.19 Write a function called addDict(dict 1,dict 2 ) which computes the union of
to dictionaries. it should return a new dictionary, with all the items in both its
argument. if the same key appear in both arguments, feel free to pick a value
from either.

def adddict(dict1,dict2) :
dic = {}
for i in dict1 :
dic [ i ] = dict1 [i]
for j in dict2 :
dic [ j ] = dict2 [ j ]
return dic

dict1=input("Enter First Dictionary :- ")


dict2=input("Enter Second Dictionary :- ")

print(adddict(dict1,dict2))

Q.20 Write a program to sort a dictionary’s keys using bubble sort and produce the sorted
keys as a list.

dic = eval (input("Enter a dictionary : "))


key = list(dic.keys())

for j in range (len(key)):


for i in range ( len ( key ) - 1) :
if key [ i ] > key [ i + 1 ] :
8
key [ i ] , key [ i + 1 ] = key [ i + 1 ] , key [ i ]

print(key)

Q.21 Write a program to sort a dictionary’s keys using bubble sort and produce the sorted
keys as a list.

dic = eval (input("Enter a dictionary : "))


key = list(dic.keys())
for j in range (len(key)):
for i in range ( len ( key ) - 1) :
if key [ i ] > key [ i + 1 ] :
key [ i ] , key [ i + 1 ] = key [ i + 1 ] , key [ i ]

print(key)

You might also like