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

Python Tour 2 Type C Programming Practice

The document contains three programming exercises in Python. The first program validates a phone number format, the second analyzes sentences for word count, character count, and alphanumeric percentage, and the third adds corresponding elements of two lists to create a new list. Each exercise includes example code and comments explaining the functionality.

Uploaded by

aman11211369y
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)
13 views3 pages

Python Tour 2 Type C Programming Practice

The document contains three programming exercises in Python. The first program validates a phone number format, the second analyzes sentences for word count, character count, and alphanumeric percentage, and the third adds corresponding elements of two lists to create a new list. Each exercise includes example code and comments explaining the functionality.

Uploaded by

aman11211369y
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/ 3

Python Tour 2 - Type C: Programming Practice/Knowledge Based Questions

Question 1:

Write a program that prompts the user to enter a phone number consisting of exactly 10 digits and two

dashes (-).

The dashes must appear after the area code (first 3 digits) and after the next 3 digits.

Example: 017-555-1212.

Display whether the phone number entered is valid format or not.

# Program to validate the phone number format

phNo = input("Enter the phone number: ") # Prompt user for phone number

length = len(phNo) # Calculate length of input

# Check all conditions for valid phone number

if length == 12 and phNo[3] == "-" and phNo[7] == "-" and phNo[:3].isdigit() and phNo[4:7].isdigit() and

phNo[8:].isdigit():

print("Valid Phone Number")

else:

print("Invalid Phone Number")

-------------------------------------------------------------------

Question 2:

Write a program that prompts the user to type some sentences followed by "Enter".

It should then print the original sentences and display the following statistics:

- Number of words

- Number of characters (including spaces and punctuation)

- Percentage of characters that are alphanumeric

# Program to analyze a sentence

str = input("Enter a few sentences: ") # Take sentence input

length = len(str) # Total characters including space


spaceCount = 0 # Count of spaces

alnumCount = 0 # Count of alphanumeric characters

# Loop through each character

for ch in str:

if ch.isspace(): # If character is space

spaceCount += 1

elif ch.isalnum(): # If character is alphanumeric

alnumCount += 1

# Calculate percentage

alnumPercent = alnumCount / length * 100

print("Original Sentences:")

print(str)

print("Number of words =", (spaceCount + 1)) # Words are space+1

print("Number of characters =", length)

print("Alphanumeric Percentage =", alnumPercent)

-------------------------------------------------------------------

Question 3:

Write a program that takes two lists L and M of the same size and adds their elements together to form a new

list N.

# Program to add corresponding elements of two lists

print("Enter two lists of same size")

L = eval(input("Enter first list (L): ")) # Input first list

M = eval(input("Enter second list (M): ")) # Input second list

N = [] # Initialize empty list for result

# Loop through each element by index

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

print("List N:")

print(N)

-------------------------------------------------------------------

You might also like