Chapter 2 – Python Revision Tour – II
Type C: Programming Practice / Knowledge-Based Questions
Question – 1: Phone Number Format Validation
Answer:
#Que - 1 : Phone Number Format Validaion
phoneNo = input("Enter Phone Number xxx-xxx-xxxx format : ")
if phoneNo.count('-') == 2 and len(phoneNo) == 12 \
and phoneNo[3] == '-' and phoneNo[7] == '-' and \
phoneNo[0:3].isdigit() and phoneNo[4:7].isdigit()\
and phoneNo[8:].isdigit():
print("You entered a VALID phone number", phoneNo)
else:
print("Phone number is INVALID", phoneNo)
Output:
Enter Phone Number xxx-xxx-xxxx format : 123-456-7890
You entered a VALID phone number 123-456-7890
Enter Phone Number xxx-xxx-xxxx format : 12-3655-4598
Phone number is INVALID 12-3655-4598
Question – 2: Statistics related to Sentence
Answer:
#Que - 2 : Enter Sentence and Print statistics relating to sentence.
sentence = input("Enter a sentence :")
words = len(sentence.split())
nochars = len(sentence)
count = 0
for char in sentence:
if char.isalnum():
count +=1
alnumper = (count / nochars) * 100
print("Number of Words : ", words)
print("Number of Characters : ", nochars)
print("Percentage of characters that are alpha numeric :", alnumper)
Output:
Enter a sentence:Your roll number is 10. You scored 25 out of 30.
Number of Words: 11
Number of Characters: 48
Percentage of characters that are alpha numeric: 75.0
Question – 3: Sum of two List element-wise
Answer:
#Que 3 - Sum of two lists element wise
L = [3, 1, 4] #List 1
M = [1, 5, 9] #List 2
N = [] #Empty List
for i in range(3):
N.append(L[i] + M[i]) #Adding element and appending
print("Sum of two Lists :", N)
Output:
Sum of two Lists : [4, 6, 13]
Question – 4: Rotate the element of the list i.e. one position right shift
Answer:
#Que 4 - Rotate the element of List i.e. Right Shift
L = [5, 3, 2, 1, 9]
print("List Before Shifting")
print(L)
temp = L[len(L)-1] #last element
for i in range(len(L)-2,-1,-1):
L[i+1] = L[i] #Copy current element to next position
L[0] = temp #Copy the last element at 0 position
print("List After Shifting")
print(L)
Output:
List Before Shifting
[5, 3, 2, 1, 9]
List After Shifting
[9, 5, 3, 2, 1]
Question – 5: Print the longest word in a list of words.
Answer:
#Que 5 - Print longest words in a list of words.
string = input("Enter a sentence : ")
wordList = string.split()
print("List of words are =>", wordList, sep='\n')
long_word = ''
for word in wordList:
if len(word) > len(long_word):
long_word = word
print("The longest word in the List is => ", long_word)
Output:
Enter a sentence: Hello my dear students, you are doing excellent in your study.
Original List :
[‘Hello’, ‘my’, ‘dear’, ‘students,’, ‘you’, ‘are’, ‘doing’, ‘execellent’, ‘in’, ‘your’, ‘study.’]
The longest word in the List is => excellent
Question – 6: Lists of all the integers less than 100 that are multiples of 3 or 5.
Answer:
#Que 6 - Lists of all the integers less than 100 that are multiples of 3 or 5.
L = []
for i in range(1, 100):
if i % 3 == 0 or i % 5 == 0:
L.append(i)
print("List are => ", L, sep = '\n')
Output:
List are =>
[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]