💡 9th Grade Other: Python Coding Worksheet Practice Questions
1
Solved Example
Easy Level
💡 Understanding Print Statements and Variables
Write a Python program that declares a variable called student_name and assigns it your name. Then, print a greeting message that includes your name.
Example Output:
Hello, [Your Name]! Welcome to Python.
Solution & Explanation
Here's how to write the Python code:
👉 Step 1: Declare the variable.
First, we create a variable named student_name and assign a string value (your name) to it. Remember to enclose string values in quotes.
student_name = "Alice"
👉 Step 2: Print the greeting message.
Use the print() function to display the message. We can combine a string literal with the variable using a comma or f-string for clear output.
print("Hello,", student_name + "! Welcome to Python.")
✅ Complete Code:
student_name = "Alice" print("Hello,", student_name + "! Welcome to Python.")
Explanation: The print() function is fundamental for displaying output. Variables like student_name store data that can be reused throughout your program. String concatenation (using +) joins strings together.
2
Solved Example
Easy Level
📌 Basic Arithmetic Operations
Write a Python program that calculates the sum and product of two numbers: num1 = 15 and num2 = 5. Print both results clearly.
Example Output:
Sum: 20 Product: 75
Solution & Explanation
Let's perform the arithmetic operations:
👉 Step 1: Define the numbers.
Assign the given values to variables.
num1 = 15 num2 = 5
👉 Step 2: Calculate the sum.
Use the addition operator +.
sum_result = num1 + num2
👉 Step 3: Calculate the product.
Use the multiplication operator *.
product_result = num1 * num2
👉 Step 4: Print the results.
Display the calculated sum and product using print().
Explanation: Python supports standard arithmetic operators. Variables store intermediate results, making your code readable and organized.
3
Solved Example
Medium Level
💡 User Input and Type Conversion
Write a Python program that asks the user for their age and then prints a message stating how old they are. Remember that input from the user is always a string!
Example Interaction:
Enter your age: 14 You are 14 years old.
Solution & Explanation
Here's how to get user input and use it:
👉 Step 1: Get input from the user.
Use the input() function to prompt the user and store their response in a variable.
age_str = input("Enter your age: ")
👉 Step 2: Convert the input to an integer.
Since input() always returns a string, we need to convert it to an integer if we want to perform numerical operations or just to store it as a number. In this case, it's good practice, even if not strictly needed for just printing.
age = int(age_str)
👉 Step 3: Print the message.
Combine a string literal with the age variable to form the final output.
print("You are", age, "years old.")
✅ Complete Code:
age_str = input("Enter your age: ") age = int(age_str) print("You are", age, "years old.")
Explanation: The input() function is crucial for creating interactive programs. The int() function converts a string to an integer, which is often necessary when dealing with numerical input from users.
4
Solved Example
Medium Level
📌 Conditional Statements (if-else)
Write a Python program that checks if a given number x = 10 is even or odd. Print a message indicating the result.
Example Output:
10 is an even number.
Solution & Explanation
Let's use an if-else statement to determine parity:
👉 Step 1: Define the number.
Assign the value to the variable x.
x = 10
👉 Step 2: Check for even or odd.
An even number is perfectly divisible by 2, meaning the remainder when divided by 2 is 0. We use the modulo operator % to find the remainder.
if x % 2 == 0: print(x, "is an even number.") else: print(x, "is an odd number.")
✅ Complete Code:
x = 10
if x % 2 == 0: print(x, "is an even number.") else: print(x, "is an odd number.")
Explanation: The if-else statement allows your program to make decisions. The modulo operator% returns the remainder of a division. If x % 2 is 0, the number is even; otherwise, it's odd.
5
Solved Example
Medium Level
💡 Combining Input, Arithmetic, and Conditionals
A student takes a quiz and scores a certain number of points out of 100. Write a Python program that asks the user for their quiz score. If the score is 60 or above, print "You passed the quiz!". Otherwise, print "You need to study more.".
Example Interaction 1:
Enter your quiz score (out of 100): 75 You passed the quiz!
Example Interaction 2:
Enter your quiz score (out of 100): 55 You need to study more.
Solution & Explanation
This problem combines several fundamental concepts:
👉 Step 1: Get the quiz score from the user.
Use input() and remember to convert the string input to an integer using int().
score_str = input("Enter your quiz score (out of 100): ") score = int(score_str)
👉 Step 2: Apply the conditional logic.
Use an if-else statement to check if the score meets the passing threshold (60 or above).
if score >= 60: print("You passed the quiz!") else: print("You need to study more.")
✅ Complete Code:
score_str = input("Enter your quiz score (out of 100): ") score = int(score_str)
if score >= 60: print("You passed the quiz!") else: print("You need to study more.")
Explanation: This example demonstrates how to create a simple interactive program that takes user input, processes it numerically, and then makes a decision based on that data. The >= operator checks for "greater than or equal to".
6
Solved Example
Medium Level
📌 Introduction to Loops (for loop with range)
Write a Python program that uses a for loop to print the numbers from 1 to 5, inclusive. Each number should be on a new line.
Example Output:
1 2 3 4 5
Solution & Explanation
Let's use a for loop to iterate through numbers:
👉 Step 1: Use a for loop with range().
The range() function generates a sequence of numbers. range(1, 6) will generate numbers starting from 1 up to (but not including) 6, which means 1, 2, 3, 4, 5.
for i in range(1, 6):
👉 Step 2: Print each number inside the loop.
The code inside the loop will execute for each number generated by range().
print(i)
✅ Complete Code:
for i in range(1, 6): print(i)
Explanation: The for loop is used for iteration, meaning it repeats a block of code multiple times. The range(start, stop) function is commonly used with for loops to generate a sequence of numbers. Remember that the stop value is exclusive.
7
Solved Example
Real World Example
🌍 Calculating Simple Interest
Imagine you put money into a savings account. The bank offers a simple annual interest rate. Write a Python program that calculates the simple interest earned on a principal amount. Ask the user for the principal amount, the annual interest rate (as a percentage), and the number of years.
The formula for simple interest is: \( \text{Simple Interest} = \text{Principal} \times \text{Rate} \times \text{Time} \)
Example Interaction:
Enter the principal amount: 1000 Enter the annual interest rate (e.g., 5 for 5%): 5 Enter the number of years: 2 The simple interest earned is: 100.0
Solution & Explanation
Let's calculate simple interest using user input:
👉 Step 1: Get input for Principal, Rate, and Time.
Remember to convert the inputs to appropriate numerical types (float for principal and rate, int for years). The rate entered as "5" for 5% needs to be converted to a decimal (0.05) for the calculation.
principal = float(input("Enter the principal amount: ")) rate_percent = float(input("Enter the annual interest rate (e.g., 5 for 5%): ")) time_years = int(input("Enter the number of years: "))
👉 Step 2: Convert rate from percentage to decimal.
Divide the percentage rate by 100.
rate_decimal = rate_percent / 100
👉 Step 3: Calculate the simple interest.
Apply the formula: \( \text{Interest} = \text{Principal} \times \text{Rate} \times \text{Time} \)
simple_interest = principal rate_decimal time_years
👉 Step 4: Print the result.
Display the calculated simple interest.
print("The simple interest earned is: ", simple_interest)
✅ Complete Code:
principal = float(input("Enter the principal amount: ")) rate_percent = float(input("Enter the annual interest rate (e.g., 5 for 5%): ")) time_years = int(input("Enter the number of years: "))
rate_decimal = rate_percent / 100
simple_interest = principal rate_decimal time_years
print("The simple interest earned is: ", simple_interest)
Explanation: This program shows how Python can be used for financial calculations. It highlights the importance of correct type conversion (e.g., using float() for monetary values and rates) and applying mathematical formulas within code.
8
Solved Example
Real World Example
🛒 Discount Eligibility Check
A store offers a 10% discount to customers who spend more than 50. Write a Python program that asks the user for their total purchase amount. Then, tell them if they are eligible for the discount.
Example Interaction 1:
Enter your total purchase amount: 65 Congratulations! You are eligible for a 10% discount.
Example Interaction 2:
Enter your total purchase amount: 45 Sorry, you are not eligible for a discount at this time.
Solution & Explanation
Let's simulate a discount eligibility check:
👉 Step 1: Get the purchase amount from the user.
Use input() and convert to a float to handle potential decimal values.
purchase_amount = float(input("Enter your total purchase amount: "))
👉 Step 2: Check the discount condition.
Use an if-else statement to compare the purchase amount with the discount threshold (50).
if purchase_amount > 50: print("Congratulations! You are eligible for a 10% discount.") else: print("Sorry, you are not eligible for a discount at this time.")
✅ Complete Code:
purchase_amount = float(input("Enter your total purchase amount: "))
if purchase_amount > 50: print("Congratulations! You are eligible for a 10% discount.") else: print("Sorry, you are not eligible for a discount at this time.")
Explanation: This program demonstrates how conditional logic is used in everyday scenarios, like determining eligibility for promotions. The > operator checks if one value is strictly greater than another.
9th Grade Other: Python Coding Worksheet Practice Questions
Example 1:
💡 Understanding Print Statements and Variables
Write a Python program that declares a variable called student_name and assigns it your name. Then, print a greeting message that includes your name.
Example Output:
Hello, [Your Name]! Welcome to Python.
Solution:
Here's how to write the Python code:
👉 Step 1: Declare the variable.
First, we create a variable named student_name and assign a string value (your name) to it. Remember to enclose string values in quotes.
student_name = "Alice"
👉 Step 2: Print the greeting message.
Use the print() function to display the message. We can combine a string literal with the variable using a comma or f-string for clear output.
print("Hello,", student_name + "! Welcome to Python.")
✅ Complete Code:
student_name = "Alice" print("Hello,", student_name + "! Welcome to Python.")
Explanation: The print() function is fundamental for displaying output. Variables like student_name store data that can be reused throughout your program. String concatenation (using +) joins strings together.
Example 2:
📌 Basic Arithmetic Operations
Write a Python program that calculates the sum and product of two numbers: num1 = 15 and num2 = 5. Print both results clearly.
Example Output:
Sum: 20 Product: 75
Solution:
Let's perform the arithmetic operations:
👉 Step 1: Define the numbers.
Assign the given values to variables.
num1 = 15 num2 = 5
👉 Step 2: Calculate the sum.
Use the addition operator +.
sum_result = num1 + num2
👉 Step 3: Calculate the product.
Use the multiplication operator *.
product_result = num1 * num2
👉 Step 4: Print the results.
Display the calculated sum and product using print().
Explanation: Python supports standard arithmetic operators. Variables store intermediate results, making your code readable and organized.
Example 3:
💡 User Input and Type Conversion
Write a Python program that asks the user for their age and then prints a message stating how old they are. Remember that input from the user is always a string!
Example Interaction:
Enter your age: 14 You are 14 years old.
Solution:
Here's how to get user input and use it:
👉 Step 1: Get input from the user.
Use the input() function to prompt the user and store their response in a variable.
age_str = input("Enter your age: ")
👉 Step 2: Convert the input to an integer.
Since input() always returns a string, we need to convert it to an integer if we want to perform numerical operations or just to store it as a number. In this case, it's good practice, even if not strictly needed for just printing.
age = int(age_str)
👉 Step 3: Print the message.
Combine a string literal with the age variable to form the final output.
print("You are", age, "years old.")
✅ Complete Code:
age_str = input("Enter your age: ") age = int(age_str) print("You are", age, "years old.")
Explanation: The input() function is crucial for creating interactive programs. The int() function converts a string to an integer, which is often necessary when dealing with numerical input from users.
Example 4:
📌 Conditional Statements (if-else)
Write a Python program that checks if a given number x = 10 is even or odd. Print a message indicating the result.
Example Output:
10 is an even number.
Solution:
Let's use an if-else statement to determine parity:
👉 Step 1: Define the number.
Assign the value to the variable x.
x = 10
👉 Step 2: Check for even or odd.
An even number is perfectly divisible by 2, meaning the remainder when divided by 2 is 0. We use the modulo operator % to find the remainder.
if x % 2 == 0: print(x, "is an even number.") else: print(x, "is an odd number.")
✅ Complete Code:
x = 10
if x % 2 == 0: print(x, "is an even number.") else: print(x, "is an odd number.")
Explanation: The if-else statement allows your program to make decisions. The modulo operator% returns the remainder of a division. If x % 2 is 0, the number is even; otherwise, it's odd.
Example 5:
💡 Combining Input, Arithmetic, and Conditionals
A student takes a quiz and scores a certain number of points out of 100. Write a Python program that asks the user for their quiz score. If the score is 60 or above, print "You passed the quiz!". Otherwise, print "You need to study more.".
Example Interaction 1:
Enter your quiz score (out of 100): 75 You passed the quiz!
Example Interaction 2:
Enter your quiz score (out of 100): 55 You need to study more.
Solution:
This problem combines several fundamental concepts:
👉 Step 1: Get the quiz score from the user.
Use input() and remember to convert the string input to an integer using int().
score_str = input("Enter your quiz score (out of 100): ") score = int(score_str)
👉 Step 2: Apply the conditional logic.
Use an if-else statement to check if the score meets the passing threshold (60 or above).
if score >= 60: print("You passed the quiz!") else: print("You need to study more.")
✅ Complete Code:
score_str = input("Enter your quiz score (out of 100): ") score = int(score_str)
if score >= 60: print("You passed the quiz!") else: print("You need to study more.")
Explanation: This example demonstrates how to create a simple interactive program that takes user input, processes it numerically, and then makes a decision based on that data. The >= operator checks for "greater than or equal to".
Example 6:
📌 Introduction to Loops (for loop with range)
Write a Python program that uses a for loop to print the numbers from 1 to 5, inclusive. Each number should be on a new line.
Example Output:
1 2 3 4 5
Solution:
Let's use a for loop to iterate through numbers:
👉 Step 1: Use a for loop with range().
The range() function generates a sequence of numbers. range(1, 6) will generate numbers starting from 1 up to (but not including) 6, which means 1, 2, 3, 4, 5.
for i in range(1, 6):
👉 Step 2: Print each number inside the loop.
The code inside the loop will execute for each number generated by range().
print(i)
✅ Complete Code:
for i in range(1, 6): print(i)
Explanation: The for loop is used for iteration, meaning it repeats a block of code multiple times. The range(start, stop) function is commonly used with for loops to generate a sequence of numbers. Remember that the stop value is exclusive.
Example 7:
🌍 Calculating Simple Interest
Imagine you put money into a savings account. The bank offers a simple annual interest rate. Write a Python program that calculates the simple interest earned on a principal amount. Ask the user for the principal amount, the annual interest rate (as a percentage), and the number of years.
The formula for simple interest is: \( \text{Simple Interest} = \text{Principal} \times \text{Rate} \times \text{Time} \)
Example Interaction:
Enter the principal amount: 1000 Enter the annual interest rate (e.g., 5 for 5%): 5 Enter the number of years: 2 The simple interest earned is: 100.0
Solution:
Let's calculate simple interest using user input:
👉 Step 1: Get input for Principal, Rate, and Time.
Remember to convert the inputs to appropriate numerical types (float for principal and rate, int for years). The rate entered as "5" for 5% needs to be converted to a decimal (0.05) for the calculation.
principal = float(input("Enter the principal amount: ")) rate_percent = float(input("Enter the annual interest rate (e.g., 5 for 5%): ")) time_years = int(input("Enter the number of years: "))
👉 Step 2: Convert rate from percentage to decimal.
Divide the percentage rate by 100.
rate_decimal = rate_percent / 100
👉 Step 3: Calculate the simple interest.
Apply the formula: \( \text{Interest} = \text{Principal} \times \text{Rate} \times \text{Time} \)
simple_interest = principal rate_decimal time_years
👉 Step 4: Print the result.
Display the calculated simple interest.
print("The simple interest earned is: ", simple_interest)
✅ Complete Code:
principal = float(input("Enter the principal amount: ")) rate_percent = float(input("Enter the annual interest rate (e.g., 5 for 5%): ")) time_years = int(input("Enter the number of years: "))
rate_decimal = rate_percent / 100
simple_interest = principal rate_decimal time_years
print("The simple interest earned is: ", simple_interest)
Explanation: This program shows how Python can be used for financial calculations. It highlights the importance of correct type conversion (e.g., using float() for monetary values and rates) and applying mathematical formulas within code.
Example 8:
🛒 Discount Eligibility Check
A store offers a 10% discount to customers who spend more than 50. Write a Python program that asks the user for their total purchase amount. Then, tell them if they are eligible for the discount.
Example Interaction 1:
Enter your total purchase amount: 65 Congratulations! You are eligible for a 10% discount.
Example Interaction 2:
Enter your total purchase amount: 45 Sorry, you are not eligible for a discount at this time.
Solution:
Let's simulate a discount eligibility check:
👉 Step 1: Get the purchase amount from the user.
Use input() and convert to a float to handle potential decimal values.
purchase_amount = float(input("Enter your total purchase amount: "))
👉 Step 2: Check the discount condition.
Use an if-else statement to compare the purchase amount with the discount threshold (50).
if purchase_amount > 50: print("Congratulations! You are eligible for a 10% discount.") else: print("Sorry, you are not eligible for a discount at this time.")
✅ Complete Code:
purchase_amount = float(input("Enter your total purchase amount: "))
if purchase_amount > 50: print("Congratulations! You are eligible for a 10% discount.") else: print("Sorry, you are not eligible for a discount at this time.")
Explanation: This program demonstrates how conditional logic is used in everyday scenarios, like determining eligibility for promotions. The > operator checks if one value is strictly greater than another.