0% found this document useful (0 votes)
22 views7 pages

PYTHON TOUR 2 Type C

The document contains multiple questions and their solutions related to Python programming. It covers topics like validating phone numbers, calculating word and character counts, adding lists, rotating lists, generating Fibonacci series, working with dictionaries and sorting dictionaries by keys and values.

Uploaded by

Jeffi Joshuva
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)
22 views7 pages

PYTHON TOUR 2 Type C

The document contains multiple questions and their solutions related to Python programming. It covers topics like validating phone numbers, calculating word and character counts, adding lists, rotating lists, generating Fibonacci series, working with dictionaries and sorting dictionaries by keys and values.

Uploaded by

Jeffi Joshuva
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/ 7

Q.

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.
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 valid number ")
else :
print("It is not valid number ")
else :
print("It is not valid number ")
else :
print("It is not valid number ")
else :
print("It is not valid number ")
else :
print("It is not valid number ")
else :
print("It is not valid number ")
Output :-
Enter your Phone number :- 017-555-1212
It is valid number
>>>
Enter your Phone number :- 654-54633-513
It is not valid number
>>>
Enter your Phone number :- 313-456-3113
It is valid number
>>>
Enter your Phone number :- 56-5643-25423
It is not valid number
>>>
Enter your Phone number :- 46-3113-55313
It is not valid number
>>>
Q. 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):
(i) Number of words
(ii) Numbers of character (including white-space and punctuation )
(iii) Percentage of character that are alphanumeric.
sen = input("Enter a sentence = ")
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)
Output :-
Enter a sentence = ? Welcome to our website Path Walla : )
Number of word is 9
Number of characters 39
Percentage of alpha numeric = 71.7948717948718
>>>
Enter a sentence = #Python is my Love?
Number of word is 4
Number of characters 19
Percentage of alpha numeric = 73.68421052631578
>>>
Q. Write a program that takes any two list l and m of the same size
and adds their elements together to form a new list n whose element
are sum of the corresponding elements in l and m.
l = eval(input("Enter first list = "))
m = eval(input("Enter second list = "))
n=[]
for i in range(len(l)):
n=n+[l[i]+m[i]]
print("New list = ",n)
Output :-
Enter first list = [1,2,3,4,5,6,7,8,9]
Enter second list = [1,2,3,4,5,6,7,8,9]
New list = [2, 4, 6, 8, 10, 12, 14, 16, 18]
>>>
Enter first list = [9,8,7,6,5,4,3,2,1]
Enter second list = [1,2,3,4,5,6,7,8,9]
New list = [10, 10, 10, 10, 10, 10, 10, 10, 10]
>>>
Enter first list = [89,64,37,58,41,39]
Enter second list = [58,96,74,38,21,98]
New list = [147, 160, 111, 96, 62, 137]
>>>
Q. Write a program rotates the elements of a list show that the
element at the first index moves to the second index, the element i n
second index moves to the third index, etc, and the element in the
last index moves to the first index.
lst = eval(input("enter the list = "))
print ("New list =", [lst[ -1] ] + lst[0 : -1] )
Output :-
Enter the list :- [3, 5, 6, 9, 10, 12, 15, 18, 20, 21]
New list = [21, 3, 5, 6, 9, 10, 12, 15, 18, 20]
>>>
Enter the list :- ["Path","Walla","Portal","Express"]
New list = ['Express', 'Path', 'Walla', 'Portal']
>>>
Enter the list :- ["C++","Python","Java","CSS"]
New list = ['CSS', 'C++', 'Python', 'Java']
>>>
Q. 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)
Output :-
[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42,
45, 48, 50, 51, 54, 55, 57, 60, 63, 65, 66, 69, 70, 72, 75, 78, 80, 81, 84,
85, 87, 90, 93, 95, 96, 99]
>>>
Q. Define two variables first and second show that first = "jimmy"
and second = "johny". Write a short python code segment that
swaps the values assigned to these two variable and print the
results.
Answer :-
first = "jimmy "
second= "johny"
first , second = second , first
print("first = ", first )
print("second = " , second )
Output :-
first = johny
second = jimmy
>>>
Q. Write python that create a tuple storing first 9 term of Fibonacci
series.
fib = (0,1,1)
for i in range(6):
fib = fib + ( fib [ i +1] + fib [ i + 2 ] ,)
print(fib)
Output :-
(0, 1, 1, 2, 3, 5, 8, 13, 21)
>>>
Q. 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 month with 31 days.
(d) Print out the (key - value) pair sorted by the number of the days
in each month.
(i)
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 ])
(ii)
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}
lst = list ( month . keys() )
lst.sort()
print( lst )
(iii)
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}
print( "Month which have 31 days !!-- ")
for i in month :
if month [ i ] == 31 :
print( i )
(iv)
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}
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)
Output :-
(i)
Enter the month name in short form :- feb
Number of days in feb = 28
>>>
Enter the month name in short form :- aug
Number of days in aug = 31
>>>
(ii)
['april', 'aug', 'dec', 'feb', 'jan', 'july', 'june', 'march', 'may', 'nov', 'oct',
'sept']
>>>
(iii)
Month which have 31 days !!--
jan
march
may
july
aug
oct
dec
>>>
(iv)
Month according to number of days ---
feb
april
june
sept
nov
jan
march
may
july
aug
oct
dec
>>>
Q. 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=eval(input("Enter First Dictionary :- "))


dict2=eval(input("Enter Second Dictionary :- "))
print(adddict(dict1,dict2))
Output :-
Enter First Dictionary :- {"Path":5,"Walla":3,"Portal":4,"Express":7}
Enter Second Dictionary :- {"Python":9,"C++":2,"Java":7,"CSS":3}
{'Path': 5, 'Walla': 3, 'Portal': 4, 'Express': 7, 'Python': 9, 'C++': 2, 'Java': 7,
'CSS': 3}
>>>
Enter First Dictionary :-
{"Path":5,"Walla":3,"Portal":4,"Express":7,"Python":9,"C++":2}
Enter Second Dictionary :- {"Python":9,"C++":2,"Java":7,"CSS":3}
{'Path': 5, 'Walla': 3, 'Portal': 4, 'Express': 7, 'Python': 9, 'C++': 2, 'Java': 7,
'CSS': 3}
>>>
Enter First Dictionary :- {9:"Python",2:"C++",7:"Java",3:"CSS"}
Enter Second Dictionary :- {5:"Path",3:"Walla",4:"Portal",7:"Express"}
{9: 'Python', 2: 'C++', 7: 'Express', 3: 'Walla', 5: 'Path', 4: 'Portal'}
>>>
Q. 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 i in range ( len ( key ) - 1 ) :
if key [ i ] > key [ i + 1 ] :
key [ i ] , key [ i + 1 ] = key [ i + 1 ] , key [ i ]
print(key)
Output :-
Enter a dictionary : {9:"Python",2:"C++",7:"Java",3:"CSS"}
[2, 7, 3, 9]
>>>
Enter a dictionary : {5:"Path",3:"Walla",4:"Portal",7:"Express"}
[3, 4, 5, 7]
>>>
Q. Write a program to sort a dictionary’s value using bubble sort and
produce the sorted values as a list.
dic = eval (input("Enter a dictionary : "))
val = list(dic.values())
for i in range ( len ( val ) - 1 ) :
if val [ i ] > val [ i + 1 ] :
val [ i ] , val [ i + 1 ] = val [ i + 1 ] , val [ i ]
print(val)
Output :-
Enter a dictionary : {"Path":5,"Walla":3,"Portal":4,"Express":7}
[3, 4, 5, 7]
>>>
Enter a dictionary : {"Python":9,"C++":2,"Java":7,"CSS":3}
[2, 7, 3, 9]
>>>

You might also like