0% found this document useful (0 votes)
11 views3 pages

Revision Chapter II XII

Uploaded by

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

Revision Chapter II XII

Uploaded by

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

Revision chapter-ii

1. Write a program that prompts for a phone number of 10 digits and two dashes, with dashes
after the area code and the next three numbers. For example, 017-555-1212 is a legal input.
Display if the phone number entered is valid format or not and display if the phone number
is valid or not (i.e., contains just the digits and dash at specific places).
p = input(‘Enter Phone Number : ‘)
val = False
# length must be 12
if len(p) == 12 and p[3] == ‘-‘ and p[7] == ‘-‘:
if (p[:3]+p[4:7]+p[8:]).isdigit():
val = True

if val:
print(p, ‘is valid’)
else:
print(p, ‘is invalid’)

2.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].
L = list(map(int, input(‘Enter list of integers : ‘).split()))
M = list(map(int, input(‘Enter list of integers : ‘).split()))
if len(L) = len(M):
N = [L[i]+M[i] for i in range(len(L))]
print(N)
3. Write a program that should prompt the user to type some sentence(s) followed by “enter”. It
should then print the original sentence(s) and the following statistics relating to the sentence(s) :
s = input(‘Enter a sentence : ‘)
number_of_words = 1
number_of_characters = len(s)
al_num = 0
for i in s:
if i.isalnum():
al_num += 1
if i == ‘ ‘: # there is a space means there is another word
number_of_words += 1
print(‘number of words are’, number_of_words)
print(‘number_of_characters are’, number_of_characters)
print(‘percentage of characters that are alphanumric is’, al_num*100/len(s), ‘%’)
4. 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 = input().split() # input().split() returns the list
l = l[len(l)-1:] + l[:-1]
print(l)
5.Write a short Python code segment that prints the longest word in a list of words.
6.Write a program that creates a list of all the integers less than 100 that are multiples of 3 or 5.

7.Define two variables first and second so that first = “Jimmy” and second = “Johny”. Write a short
Python code segment that swaps the values assigned to these two variables and prints the results.
first = ‘Jimmy’
second = ‘Johny’
print(first, second)
first, second = second, first
print(first, second)
9. Create a dictionary whose keys are month names, and whose values are the number of days in
the corresponding months.

10. Write a function called addDict(dict1, dict2) which computes the union of two dictionaries. It
should return a new dictionary, with all the items in both its arguments (assumed to be dictionaries).
If the same key appears in both arguments, feel free to pick a value from either.

def addDict(dic1, dic2):


new_dic = {}
for i in dic1.keys():
new_dic[i] = dic1[i]

for i in dic2.keys():

dic2 in the new_dic


new_dic[i] = dic2[i]
return new_dic

dic1 = {‘a’:1, ‘b’:2, ‘c’:3}


dic2 = {‘c’:4, ‘d’:5, ‘e’:6}
print(addDict(dic1, dic2))

You might also like