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

Python Practice Worksheet 3

Uploaded by

ashvita2008
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)
17 views3 pages

Python Practice Worksheet 3

Uploaded by

ashvita2008
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 Practice Worksheet – Term 1 Practice Set 3

Q1. Write a Python program to input marks of 4 subjects,


calculate percentage and decide pass/fail (pass if percentage ≥
40).
marks = []
for i in range(4):
m = int(input(f"Enter marks of subject {i+1}: "))
marks.append(m)

total = sum(marks)
perc = total / 4

print("Total:", total)
print("Percentage:", perc)

if perc >= 40:


print("Result: Pass")
else:
print("Result: Fail")

Q2. Write a Python program to print the squares of numbers from


1 to 12 using while loop.
i = 1
while i <= 12:
print(i, "squared is", i*i)
i += 1

Q3. Explain any three dictionary functions with examples.


students = {"Amit": 85, "Riya": 90, "Karan": 78}

# keys() - returns all keys


print("Keys:", students.keys())

# values() - returns all values


print("Values:", students.values())

# get() - returns value of a key


print("Marks of Riya:", students.get("Riya"))

Q4. Write Python code to create a list of city names. Perform: (i)
display all cities, (ii) display number of cities, (iii) display cities in
alphabetical order.
cities = ["Delhi", "Mumbai", "Kolkata", "Chennai"]
print("Cities:", cities)
print("Number of cities:", len(cities))
cities.sort()
print("Alphabetical order:", cities)

Q5. Write a Python program to create a dictionary of 3 employees


with names as keys and salaries as values. Display (i) all
key-value pairs, (ii) employee with lowest salary.
employees = {"John": 35000, "Sara": 42000, "Raj": 30000}
print("All employees:", employees)
lowest = min(employees, key=employees.get)
print("Lowest salary:", lowest, "with", employees[lowest])

Q6. Consider list = [10, 25, 40, 55, 70]. Write Python code to: (i)
display numbers divisible by 5, (ii) display sum of squares, (iii)
sort list, (iv) display first 2 elements.
nums = [10, 25, 40, 55, 70]
div5 = [n for n in nums if n % 5 == 0]
print("Divisible by 5:", div5)
squares_sum = sum([n*n for n in nums])
print("Sum of squares:", squares_sum)
nums.sort()
print("Sorted:", nums)
print("First 2 elements:", nums[:2])

Q7. Write a Python program to input 6 numbers and display: (i)


product of numbers, (ii) count of even numbers, (iii) largest
number, (iv) smallest number, (v) list in reverse order.
nums = []
for i in range(6):
n = int(input("Enter number: "))
nums.append(n)
print("Numbers:", nums)
product = 1
for n in nums:
product *= n
print("Product:", product)
even_count = len([n for n in nums if n % 2 == 0])
print("Even count:", even_count)
print("Largest:", max(nums))
print("Smallest:", min(nums))
print("Reverse:", list(reversed(nums)))

Q8. Write a program to create a dictionary of subjects with subject


names as keys and credits as values. Display (i) all subjects, (ii)
subject with maximum credits, (iii) average credits.
subjects = {"Maths": 4, "Science": 3, "English": 2}
print("Subjects:", subjects)
max_sub = max(subjects, key=subjects.get)
print("Maximum credits subject:", max_sub, "with", subjects[max_sub])
avg_credits = sum(subjects.values()) / len(subjects)
print("Average credits:", avg_credits)

Q9. A shop gives discounts based on amount spent: ≥5000 →


20%, 2000–4999 → 15%, 1000–1999 → 10%, <1000 → 5%. Write a
program to input amount and print discount received.
amt = int(input("Enter purchase amount: "))
if amt >= 5000:
disc = 0.20 * amt
elif amt >= 2000:
disc = 0.15 * amt
elif amt >= 1000:
disc = 0.10 * amt
else:
disc = 0.05 * amt
print("Discount received: ■", disc)

You might also like