Question 1 — Functions
a. Write a Python function named add_numbers(a, b) that returns the sum of the two numbers.
b. Modify the function so that it prints the result instead of returning it.
c. Add a default value of 10 to parameter b and call the function with only one argument.
d. Update the function to return three values: sum, difference, and product.
e. Write a separate function that calls add_numbers() inside it and prints the returned values.
Question 2 — Lambda Expressions
a. Create a lambda function that squares a number.
b. Use a lambda to find the larger of two numbers.
c. Use lambda inside sorted() to sort a list of tuples by the second element.
d. Use map() with a lambda to add 5 to each item in a list.
e. Use filter() with lambda to return only even numbers from a list.
Question 3 — String and List Methods
a. Convert the string "hello world" to uppercase using a method.
b. Count how many times the letter "a" appears in "banana".
c. Remove the last item from a list using two different list methods.
d. Split the string "John,Doe,25" into a list.
e. Join the list ["Python", "is", "fun"] into a space-separated string.
Question 4 — Combined Functions + Methods + Lambda
a. Write a function that takes a list of strings and returns the list in uppercase.
b. Inside the function, use map() with a lambda to multiply each number in a list by 3.
c. Create a function that takes a sentence and returns the number of words.
d. Create a lambda that checks if a string starts with "A" (case insensitive).
e. Write a function that takes a list of dictionaries and returns them sorted by age using lambda.
PYTHON PRACTICAL QUESTIONS
1. Functions
a. Write a function add_two_numbers(a, b) that returns the sum of its two parameters.
b. Modify the function so it prints whether the sum is even or odd.
c. Call the function using user input (two numbers entered from the keyboard).
d. Add error-handling so the function does not crash if the user enters a non-number.
2. Lists and Loop Operations
a. Given a list nums = [3, 7, 2, 9, 4], write a loop to print each element.
b. Write a loop that creates a new list containing only even numbers from nums.
c. Write code to find the largest number in nums without using max().
d. Write a function find_index(lst, value) that returns the index of value or -1 if not found.
3. If-Statements and Conditions
a. Ask the user for their age. If age < 13, print “Child”, 13–19 print “Teen”, otherwise “Adult”.
b. Write code that checks if a number is positive, negative, or zero.
c. Write a function grade_mark(score) that prints A, B, C, D, or F based on score ranges.
d. Modify grade_mark(score) to validate that score is between 0 and 100; otherwise print an
error.
4. Combining Loops and Functions
a. Write a function sum_list(lst) that returns the sum of all numbers in a list.
b. Use a loop to count how many elements in a list are greater than 10.
c. In sum_list(lst), add code that prints each number as it’s being added.
d. Write a main program that asks the user to enter 5 numbers, stores them in a list, and passes it
to sum_list.
5. Practical Array + Logic Challenge
a. Write a loop that removes duplicate numbers from a list and keeps only unique values.
b. Write code to reverse a list manually (without using reverse() or slicing).
c. Write a function search(lst, target) that returns True if the target exists in the list.
d. Modify the function so it also counts and returns how many times the target appears.
WISCONSIN INTERNATIONAL UNIVERSITY COLLEGE
INTERACTIVE PROGRAMMING WITH PYTHON
QUIZ 3
Q1. Combining Default Parameters and Keyword Arguments
Write a function format_text(text, uppercase=False, repeat=1) that:
a. Returns the text converted to uppercase only if uppercase=True.
b. Repeats the final text repeat number of times.
c. Call the function in three different ways:
i. using keyword arguments only
ii. using positional arguments only
iii. mixing positional and keyword arguments
Q2. Working With args and Conditional Logic
Write a function even_sum(*nums) that:
a. Accepts any quantity of numbers.
b. Returns the sum of only the even numbers provided.
c. Returns 0 if no even numbers are found.
d. Demonstrate calls where:
i. all arguments are even.
ii. a mix of even and odd
iii. no arguments are passed.
Q3. Using kwargs to Build a Flexible Object
Write a function create_user(username, **details) that:
a. Requires a positional argument username.
b. Accepts any number of keyword arguments to store as user details.
c. Automatically adds a key "active": True to the dictionary.
d. Returns a dictionary containing all user data.
e. Call the function with at least four keyword arguments and print the result.
Q4. List Arguments + Positional-Only Parameters
Write a function max_diff(values, /) that:
a. Accepts only a positional argument values, which must be a list of numbers.
b. Returns the difference between the largest and smallest values in the list.
c. Write two test calls:
i. one valid positional call
ii. one invalid keyword call that should raise a TypeError
Q5. Mixing All Argument Types Together
Create a function analyze(*nums, scale=1, **options) that:
a. Uses *nums to accept any number of numeric inputs.
b. Multiplies all numbers by the scale keyword argument.
c. If details=True is passed in **options, return a dictionary containing:
i. the scaled numbers
ii. the count of numbers
iii. their sum
d. Otherwise, just return the scaled numbers as a list.
e. Demonstrate the function with details=True and details=False.
CLASS ACTIVITIES ON PYTHON PROGRAMMING
ITERATIONS
1. Area of a Rectangle:
Question: Write a Python program to calculate the area of a rectangle. Ask the user to input
the length and width of FIVE rectangles.
Expected Output: The program should display the area of the rectangle based on the user's
input.
2. Currency Conversion (USD):
Question: Create a Python program to convert currency from USD to five different
currencies. Ask the user to input the amount in USD and the current exchange rate. Assume
the exchange rate is constant for simplicity.
Expected Output: Display the equivalent amount in local currencies.
3. Simple Calculator:
Question: Develop a Python program for a simple calculator. Prompt the user to enter two
numbers and an operation (addition, subtraction, multiplication, or division) in a loop.
Expected Output: Perform the operation chosen by the user and display the result.
4. Area of a Circle:
Question: Write a Python script to compute the FIVE areas of a circle. Request the user to
input the radius of the circle.
Expected Output: Show the calculated area of the circles.
5. Comparison Operators Quiz:
Question: Create a Python quiz to test knowledge of comparison operators. Ask the user
questions like "What is the area of a rectangle with length 5 and width 8?" or "What is the
result of 10 > 5?" Ensure the quiz covers concepts related to area calculation, currency
conversion, calculator operations, and comparison operators.
Expected Output: Provide feedback on the correctness of the user's answers and their
performance in the quiz.
6. Temperature Checker
Using loops, develop a simple python application to determine the average
temperature of 5 patients in a hospital.