Assignment 2:
Problem 1:
Write a Python program to find the sum of squares of even numbers in a list using a for loop
and if condition.
Example Input:
[1, 2, 3, 4, 5, 6]
Expected Output:
56 (i.e., 2² + 4² + 6² = 4 + 16 + 36)
Problem 2:
Define a function get_grade(score) that returns a grade:
A for 90+
B for 80–89
C for 70–79
D for 60–69
F otherwise
Use it to print grades for a list of scores.
Example Input:
[95, 82, 67, 58, 73]
Expected Output:
['A', 'B', 'D', 'F', 'C']
Problem 3:
Write a program to extract all prime numbers from a given list using filter() and a lambda
function.
Example Input:
[2, 3, 4, 5, 6, 7, 8, 9, 10]
Expected Output:
[2, 3, 5, 7]
Problem 4:
Write a Python program to convert a list of temperatures from Celsius to Fahrenheit using the
formula:
F = C * 9/5 + 32
Use map() and a lambda function.
Example Input:
[0, 10, 20, 30, 40]
Expected Output:
[32.0, 50.0, 68.0, 86.0, 104.0]
Assignment 2:
Problem 5:
Write a program that takes a string and returns a list of all non-vowel characters using list
comprehension.
Example Input:
"Python Programming"
Expected Output:
['P', 'y', 't', 'h', 'n', ' ', 'P', 'r', 'g', 'r', 'm', 'm', 'n', 'g']
Problem 6:
Write a function that accepts a sentence and returns a list of word lengths using map().
Example Input:
"Python is powerful"
Expected Output:
[6, 2, 8]
Problem 7:
From a list of strings, filter only the palindromes using filter() and lambda.
Example Input:
["madam", "racecar", "apple", "hello", "noon"]
Expected Output:
["madam", "racecar", "noon"]
Problem 8:
Write a list comprehension to return the square of all odd numbers from a list.
Example Input:
[1, 2, 3, 4, 5, 6]
Expected Output:
[1, 9, 25]
Problem 9:
Create a function calculate(a, b, op) that performs:
Addition if op == '+'
Subtraction if op == '-'
Multiplication if op == '*'
Division if op == '/'
Assignment 2:
Example Input:
calculate(10, 5, '*')
Expected Output:
50
Problem 10:
Given a list with duplicate elements, write a program to remove duplicates and return a sorted
list of squares of the unique elements using list comprehension.
Example Input:
[4, 2, 2, 3, 4, 1]
Expected Output:
[1, 4, 9, 16]