Python Programming Test Paper
Upper-Medium Level | Duration: 1.5 Hours (90 minutes)
(Using String Methods, Slicing & Built-in Functions)
INSTRUCTIONS
• Attempt all 10 questions
• Use string methods, slicing, and built-in functions
• Do NOT use explicit for/while loops
• Questions 1-5 are worth 6 marks each
• Questions 6-10 are worth 8 marks each
• Total: 60 marks
Section A: String Operations & Conditionals (6 marks each)
Question 1: String Slicing and Case Conversion
Write a Python program that takes a string as input and performs operations based on its
length: (i) If length > 10, print the first 5 characters in uppercase, the last 5 characters in
lowercase, and the middle character separately; (ii) If length is between 5 and 10 (inclusive),
reverse the string using slicing and check if it's a palindrome using if-elif-else; (iii) If length < 5,
concatenate the string with itself twice using the * operator and print it. Finally, check if the string
contains only alphabetic characters.
Question 2: List Slicing and Element Access
Given a list of numbers, write a program that: (i) If the list has more than 5 elements, extract and
print the first 3, last 3, and every 2nd element using slicing; (ii) If the list has exactly 5 elements,
print the middle element and swap the first and last elements to create a new list; (iii) If the list
has fewer than 5 elements, find the maximum and minimum values using min() and max()
functions, and calculate their difference. Also determine if the list is in ascending or descending
order using comparison.
Question 3: Tuple Unpacking and String Methods
Given a tuple containing a person's information (name, email, age), write a program that: (i)
Unpacks the tuple into separate variables; (ii) Uses string methods to validate email format
(must contain exactly one '@' and at least one '.'); (iii) Checks if the age is a valid number; (iv) If
email is valid, extract the username (part before '@') and domain (part after '@') using string
slicing and the split() method; (v) Use if-elif-else to determine if the person is eligible for a senior
discount (age >= 60).
Question 4: String Methods and Conditional Analysis
Write a program that takes a string (sentence or word) and: (i) Finds the count of uppercase
letters, lowercase letters, digits, and special characters using count(), isalpha(), isdigit()
methods; (ii) Based on the type of characters present, classify the string as 'Alphanumeric',
'Pure Text', or 'Special Characters Present' using if-elif-else; (iii) Use find() and rfind() methods
to locate the first and last occurrence of a specific character; (iv) Use replace() to change all
spaces to hyphens and check if the result is a valid Python identifier using isidentifier() method.
Question 5: List Methods and Tuple Creation
Given a list of distinct numbers, write a program that: (i) Uses list methods to find the index of
the maximum and minimum elements; (ii) Creates a copy of the list and removes the maximum
element from the copy; (iii) Uses sum() and len() functions to calculate the average; (iv) Based
on the average value, classify all elements as 'Below Average', 'Equal to Average', or 'Above
Average' using conditional statements; (v) Create a tuple from the sorted list (using sorted()
function) and another tuple with reversed order; (vi) Compare the two tuples and display which
has greater total sum.
Section B: Advanced String & List Operations (8 marks each)
Question 6: Email and String Validation
Write a program that validates multiple email addresses provided as a tuple: (i) Check if each
email contains exactly one '@' symbol using count() method; (ii) Check if the domain part (after
'@') contains at least one '.' using find() method; (iii) Extract username and domain parts using
split() and string slicing; (iv) Validate that the username contains only alphanumeric characters
and underscores using string methods; (v) Use if-elif-else statements to create a report showing
'Valid Email', 'Invalid Format', 'Missing Domain', etc.; (vi) Create a new tuple with only valid
emails and display statistics about valid vs invalid emails.
Question 7: List Manipulation Using Built-in Functions
Given a list of mixed numbers (positive, negative, zero), write a program that: (i) Uses sum()
function to calculate total sum; (ii) Uses max() and min() to find extremes; (iii) Uses len() to find
the list size; (iv) Calculates average and uses it as a threshold to create a new list with elements
converted to tuples containing (value, 'positive'/'negative'/'zero') based on conditional checks;
(v) Create another list using list slicing to get alternating elements; (vi) Compare the original list
length with sliced list length and display analysis; (vii) Use sorted() function to create ascending
and descending versions and identify if the original list matches either.
Question 8: String Pattern Analysis and Transformation
Write a program that analyzes a given string: (i) Use split() method to break the string into words
(use whitespace as delimiter); (ii) Find the longest word by comparing string lengths (without
using max with key parameter); (iii) Check if the string is a palindrome using string slicing
(ignore case and spaces); (iv) Use replace() to swap vowels and consonants patterns; (v) Use
count() method to find frequency of specific characters; (vi) Based on character analysis
(uppercase, lowercase, digit presence), classify the string using if-elif-else as 'Mixed Case with
Numbers', 'Upper Case Only', etc.; (vii) Create a reversed version using slicing and compare
with original.
Question 9: Tuple and List Comparison with Conditionals
Given a tuple of numbers and a list of numbers, write a program that: (i) Compares their lengths
using len() function; (ii) Converts the tuple to a list and compares element-by-element with the
original list using slicing; (iii) Uses sum() to compare total values and create tuples like
(list_sum, 'Greater'/'Lesser'/'Equal'); (iv) Creates new structures: a tuple from sorted list
elements and a list from reversed tuple elements; (v) Checks if any element exists in both
structures using the 'in' operator; (vi) Calculate average for both structures and categorize
values relative to average; (vii) Display comprehensive comparison results using multiple if-elif-
else conditions.
Question 10: Complex Data Validation System
Build a student record validation system using tuples and lists: (i) Each record is a tuple
(student_id, name, email, scores_as_string); (ii) Validate student_id to ensure it's numeric using
string methods (isdigit()); (iii) Validate name contains only alphabetic characters and spaces
using isalpha(); (iv) Validate email format using count(), find(), and string slicing techniques; (v)
Parse scores string using split() to extract individual scores and use sum() and len() to calculate
average; (vi) Use if-elif-else to assign grades: 'A' for avg >= 85, 'B' for >= 75, 'C' for >= 65, etc.;
(vii) Create output tuples with (student_name, grade) and sort using tuple comparison; (viii)
Generate a comprehensive report showing valid records, invalid records with reasons, and
statistics.
Total Questions Total Marks
10 60
Time Limit Difficulty Level
90 minutes Upper-Medium
KEY TECHNIQUES TO USE (Without Loops):
• String Slicing: str[start:end:step], str[::-1] for reversal
• String Methods: split(), join(), replace(), find(), count(), upper(), lower(), strip()
• Tuple/List Unpacking: a, b, c = (1, 2, 3) or a, *rest = [1, 2, 3, 4]
• Built-in Functions: len(), min(), max(), sum(), sorted(), reversed()
• List/String Methods: index(), count(), append(), remove(), copy(), clear()
• Conditional Logic: if-elif-else statements for decision making
• Membership Testing: in operator to check element existence
• String Validation: isalpha(), isdigit(), isalnum(), isidentifier()