# Complete Python Practice Set with Answers ## **1. Variables and Naming Conventions** **Questions** ### 1. Which of the following are valid Python variable names? ``` a) my_var b) 2var c) _name d) var-name e) Var123 ``` ### 2. Create 3 variables to store your **name**, **age**, and **grade**. Print them in one line. ### 3. Create a constant `PI = 3.14159` and print it. **Answers** ```python # Valid names: my_var, _name, Var123 name = "Alice" age = 20 grade = "A" print(name, age, grade) PI = 3.14159 print(PI) ``` --- ## **2. Data Types** **Questions** ### 4. Assign values: 25, 3.14, True, "Python" to variables and print their types. ### 5. Swap values of `a = 5` and `b = 10` without using a temporary variable. ### **Answers** ```python x = 25 y = 3.14 z = True s = "Python" print(type(x)) # int print(type(y)) # float print(type(z)) # bool print(type(s)) # str a, b = 5, 10 a, b = b, a print(a, b) # 10 5 ``` --- ## **3. Type Casting** ### **Questions** ### 6. Convert `"456"` to int and add 10. ### 7. Convert float `9.87` to int. ### 8. Take a number input from user, convert to float, multiply by 3. **Answers** ```python print(int("456") + 10) # 466 print(int(9.87)) # 9 x = float(input("Enter number: ")) print(x * 3) ``` --- ## **4. Strings and String Slicing** ### **Questions** ### 9. `text = "PythonProgramming"`. Print: * First 6 characters * Last 6 characters * Characters from index 2 to 8 * Every 2nd character * Reverse the string ### 10. Take first and last name input and print full name in uppercase. ### 11. Use f-string to print `"Hello, <name>! You are <age> years old."` ### **Answers** ```python text = "PythonProgramming" print(text[:6]) # Python print(text[-6:]) # mming print(text[2:9]) # thonPro print(text[::2]) # Pto rgamn print(text[::-1]) # gnimmargorPmhtyP first = input("First name: ") last = input("Last name: ") print((first + " " + last).upper()) name = "Alice" age = 20 print(f"Hello, {name}! You are {age} years old.") ``` --- ## **5. Lists** ### **Questions** ### 12. Create a list of 5 numbers. ### 13. Add a number. ### 14. Remove the third number. ### 15. Print list reversed. ### 16. Find max and min. ### 17. Count occurrences of a number. ### 18. Sort ascending and descending. ### **Answers** ```python numbers = [10, 20, 30, 40, 50] numbers.append(60) numbers.pop(2) print(numbers[::-1]) print(max(numbers), min(numbers)) print(numbers.count(20)) numbers.sort() print(numbers) numbers.sort(reverse=True) print(numbers) ``` --- ## **6. Tuples** **Questions** ### 19. Create a tuple of 5 numbers. ### 20. Try changing the second number. What happens? ### 21. Print length and max value. ### **Answers** ```python t = (1, 2, 3, 4, 5) # t[1] = 10 # Error: tuples are immutable print(len(t)) print(max(t)) ``` --- ## **7. Sets** ### **Questions** ### 22. Create a set of 5 colors. ### 23. Add a new color. ### 24. Remove a color. ### 25. Try adding duplicate. ### **Answers** ```python colors = {"red", "blue", "green", "yellow", "black"} colors.add("white") colors.remove("blue") colors.add("red") # no effect print(colors) ``` --- ## **8. Dictionaries and CRUD with Input** **Questions** ### 26. Create a dictionary: `{"name": ..., "age": ..., "grade": ...}` ### 27. Add a `"school"` key. ### 28. Update `"grade"` to `"A+"`. ### 29. Delete `"age"` key. ### 30. Print keys, values, items. ### 31. Check if `"name"` exists. ### 32. Merge `{"hobby": "Reading"}`. ### 33. Take **input from user** for student name, age, grade, add to dictionary. ### **Answers** ```python student = {"name": "Alice", "age": 20, "grade": "A"} student["school"] = "XYZ School" student["grade"] = "A+" del student["age"] print(student.keys()) print(student.values()) print(student.items()) print("name" in student) student.update({"hobby":"Reading"}) print(student) # Input from user name = input("Name: ") age = int(input("Age: ")) grade = input("Grade: ") student = {"name": name, "age": age, "grade": grade} print(student) ``` --- ## **9. If-Else** **Questions** ### 34. Number input → Positive, Negative, Zero ### 35. Check even/odd ### 36. Score input → Grade A,B,C,F ### **Answers** ```python num = int(input("Enter number: ")) if num > 0: print("Positive") elif num < 0: print("Negative") else: print("Zero") if num % 2 == 0: print("Even") else: print("Odd") score = int(input("Enter score: ")) if score >= 90: print("A") elif score >= 75: print("B") elif score >= 50: print("C") else: print("F") ``` --- ## **10. Loops (Extra)** ### **Questions** ### 37. Print 1–10 (for loop) ### 38. Print even numbers 1–20 (while loop) ### 39. Sum first 20 natural numbers ### 40. Multiplication table of a number ### 41. Print all items of a list using a loop ### 42. Nested loop: print a 5×5 star pattern ### 43. Loop through dictionary items and print key-value pairs ### 44. Take multiple student names as input (5 names) and store in a list using loop ### **Answers** ```python # For loop for i in range(1,11): print(i) # While loop i = 2 while i <= 20: print(i) i += 2 # Sum total = sum(range(1,21)) print(total) # Multiplication table n = int(input("Enter number: ")) for i in range(1,11): print(f"{n} * {i} = {n*i}") # List loop lst = [10,20,30] for item in lst: print(item) # Nested loop (star pattern) for i in range(5): for j in range(5): print("*", end=" ") print() # Dictionary loop student = {"name":"Alice","grade":"A"} for key, value in student.items(): print(key, ":", value) # Input multiple names names = [] for i in range(5): name = input(f"Enter name {i+1}: ") names.append(name) print(names) ``` --- ## **11. Functions** **Questions** ### 45. `greet(name)` prints greeting ### 46. `is_even(num)` → True/False ### 47. `factorial(n)` returns factorial ### 48. `reverse_string(s)` → reversed string ### 49. `sum_of_list(lst)` → sum ### 50. `max_min(lst)` → max and min ### 51. `count_vowels(s)` → number of vowels ### 52. `is_prime(n)` → True/False ### 53. `find_occurrences(lst,x)` → number of times x occurs ### **Answers** ```python def greet(name): print(f"Hello, {name}!") def is_even(num): return num%2==0 def reverse_string(s): return s[::-1] def sum_of_list(lst): return sum(lst) def max_min(lst): return max(lst), min(lst) def count_vowels(s): return sum(1 for c in s if c in "aeiouAEIOU") def is_prime(n): if n<2: return False for i in range(2,int(n**0.5)+1): if n%i==0: return False return True def find_occurrences(lst,x): return lst.count(x) ``` --- ## **12. Mini Projects** **Shopping List Manager** ```python shopping_list=[] for i in range(5): shopping_list.append(input("Enter item: ").upper()) print(shopping_list) ``` **Simple Calculator** ```python def calculate(a,b,op): if op=="+": return a+b elif op=="-": return a-b elif op=="*": return a*b elif op=="/": return a/b else: return "Invalid operation" a=float(input("Enter first number: ")) b=float(input("Enter second number: ")) op=input("Enter operation (+,-,*,/): ") print("Result:", calculate(a,b,op)) ``` ---