python
exercises
exercises.py🐍python
"""
01 - Python Basics: Exercises
Practice what you've learned!
Instructions:
1. Read each exercise carefully
2. Write your solution in the designated area
3. Run the file to check your answers
4. Compare with the solutions at the bottom (try first!)
"""
print("=" * 60)
print("PYTHON BASICS - EXERCISES")
print("=" * 60)
# =============================================================================
# EXERCISE 1: Variables
# Create variables for your personal information
# =============================================================================
print("\n--- Exercise 1: Variables ---")
# TODO: Create the following variables:
# - your_name (string): Your name
# - your_age (integer): Your age
# - your_height (float): Your height in meters
# - is_programmer (boolean): Are you a programmer? (True/False)
# Write your code below:
your_name = None # Replace None with your answer
your_age = None
your_height = None
is_programmer = None
# Test (uncomment when ready):
# print(f"Name: {your_name}, Age: {your_age}, Height: {your_height}m, Programmer: {is_programmer}")
# =============================================================================
# EXERCISE 2: Arithmetic Operations
# Calculate the result of these operations
# =============================================================================
print("\n--- Exercise 2: Arithmetic Operations ---")
# Given:
a = 25
b = 7
# TODO: Calculate and store in variables:
# - addition: a + b
# - subtraction: a - b
# - multiplication: a * b
# - division: a / b (float division)
# - floor_division: a // b (integer division)
# - modulus: a % b (remainder)
# - power: a raised to power b
# Write your code below:
addition = None
subtraction = None
multiplication = None
division = None
floor_division = None
modulus = None
power = None
# Test (uncomment when ready):
# print(f"a + b = {addition}")
# print(f"a - b = {subtraction}")
# print(f"a * b = {multiplication}")
# print(f"a / b = {division}")
# print(f"a // b = {floor_division}")
# print(f"a % b = {modulus}")
# print(f"a ** b = {power}")
# =============================================================================
# EXERCISE 3: Type Casting
# Convert between different data types
# =============================================================================
print("\n--- Exercise 3: Type Casting ---")
# Given:
string_number = "123"
float_number = 45.89
integer_number = 100
# TODO:
# - string_to_int: Convert string_number to integer
# - string_to_float: Convert string_number to float
# - float_to_int: Convert float_number to integer
# - int_to_str: Convert integer_number to string
# - float_to_bool: Convert float_number to boolean
# Write your code below:
string_to_int = None
string_to_float = None
float_to_int = None
int_to_str = None
float_to_bool = None
# Test (uncomment when ready):
# print(f"'{string_number}' to int: {string_to_int} (type: {type(string_to_int).__name__})")
# print(f"'{string_number}' to float: {string_to_float} (type: {type(string_to_float).__name__})")
# print(f"{float_number} to int: {float_to_int} (type: {type(float_to_int).__name__})")
# print(f"{integer_number} to str: '{int_to_str}' (type: {type(int_to_str).__name__})")
# print(f"{float_number} to bool: {float_to_bool} (type: {type(float_to_bool).__name__})")
# =============================================================================
# EXERCISE 4: Comparison and Logical Operators
# Evaluate these expressions
# =============================================================================
print("\n--- Exercise 4: Comparison and Logical Operators ---")
# Given:
x = 15
y = 20
z = 15
# TODO: What is the result of each expression? (True or False)
# Store your answers as boolean values
# Write your code below:
result1 = None # x == z
result2 = None # x != y
result3 = None # x > y
result4 = None # x <= z
result5 = None # x == z and y > x
result6 = None # x > y or z == x
result7 = None # not (x == y)
# Test (uncomment when ready):
# print(f"x == z: {result1} (correct: {x == z})")
# print(f"x != y: {result2} (correct: {x != y})")
# print(f"x > y: {result3} (correct: {x > y})")
# print(f"x <= z: {result4} (correct: {x <= z})")
# print(f"x == z and y > x: {result5} (correct: {x == z and y > x})")
# print(f"x > y or z == x: {result6} (correct: {x > y or z == x})")
# print(f"not (x == y): {result7} (correct: {not (x == y)})")
# =============================================================================
# EXERCISE 5: Temperature Converter
# Create a temperature converter
# =============================================================================
print("\n--- Exercise 5: Temperature Converter ---")
# Given temperature in Celsius:
celsius = 28
# TODO:
# Convert Celsius to Fahrenheit using: F = (C × 9/5) + 32
# Convert Celsius to Kelvin using: K = C + 273.15
# Write your code below:
fahrenheit = None
kelvin = None
# Test (uncomment when ready):
# print(f"{celsius}°C = {fahrenheit}°F")
# print(f"{celsius}°C = {kelvin}K")
# =============================================================================
# EXERCISE 6: Calculate Circle Properties
# Calculate area and circumference of a circle
# =============================================================================
print("\n--- Exercise 6: Circle Properties ---")
import math
# Given radius:
radius = 7
# TODO:
# Calculate area using: A = π × r²
# Calculate circumference using: C = 2 × π × r
# Write your code below:
area = None
circumference = None
# Test (uncomment when ready):
# print(f"Circle with radius {radius}:")
# print(f" Area = {area:.2f} square units")
# print(f" Circumference = {circumference:.2f} units")
# =============================================================================
# EXERCISE 7: BMI Calculator
# Calculate Body Mass Index
# =============================================================================
print("\n--- Exercise 7: BMI Calculator ---")
# Given:
weight_kg = 70 # Weight in kilograms
height_m = 1.75 # Height in meters
# TODO:
# Calculate BMI using: BMI = weight / height²
# Round the result to 1 decimal place
# Write your code below:
bmi = None
# Determine category based on BMI:
# - Below 18.5: Underweight
# - 18.5 - 24.9: Normal
# - 25 - 29.9: Overweight
# - 30+: Obese
# Write your code below (use comparison operators):
category = None
# Test (uncomment when ready):
# print(f"Weight: {weight_kg}kg, Height: {height_m}m")
# print(f"BMI: {bmi}")
# print(f"Category: {category}")
# =============================================================================
# EXERCISE 8: Simple Interest Calculator
# =============================================================================
print("\n--- Exercise 8: Simple Interest ---")
# Given:
principal = 5000 # Initial amount
rate = 8 # Interest rate (percentage)
time = 3 # Time in years
# TODO:
# Calculate simple interest using: SI = (P × R × T) / 100
# Calculate total amount: A = P + SI
# Write your code below:
simple_interest = None
total_amount = None
# Test (uncomment when ready):
# print(f"Principal: ${principal}")
# print(f"Rate: {rate}%")
# print(f"Time: {time} years")
# print(f"Simple Interest: ${simple_interest}")
# print(f"Total Amount: ${total_amount}")
# =============================================================================
# EXERCISE 9: Check Even/Odd
# Determine if a number is even or odd using modulus
# =============================================================================
print("\n--- Exercise 9: Even or Odd ---")
# Given:
number = 47
# TODO:
# Use the modulus operator (%) to check if number is even or odd
# If number % 2 == 0, it's even; otherwise, it's odd
# Store "Even" or "Odd" in the result variable
# Write your code below:
is_even = None # True if even, False if odd
result = None # "Even" or "Odd"
# Test (uncomment when ready):
# print(f"Number {number} is {result}")
# =============================================================================
# EXERCISE 10: Swap Variables
# Swap the values of two variables
# =============================================================================
print("\n--- Exercise 10: Swap Variables ---")
# Given:
first = 100
second = 200
print(f"Before swap: first = {first}, second = {second}")
# TODO:
# Swap the values of first and second
# Method 1: Use a temporary variable
# Method 2: Use Python's tuple unpacking (one line!)
# Write your code below:
# (Swap here)
# Test (uncomment when ready):
# print(f"After swap: first = {first}, second = {second}")
# =============================================================================
# SOLUTIONS (Don't look until you've tried!)
# =============================================================================
print("\n" + "=" * 60)
print("SOLUTIONS (scroll down only after trying!)")
print("=" * 60)
"""
EXERCISE 1:
your_name = "John"
your_age = 25
your_height = 1.75
is_programmer = True
EXERCISE 2:
addition = a + b # 32
subtraction = a - b # 18
multiplication = a * b # 175
division = a / b # 3.571...
floor_division = a // b # 3
modulus = a % b # 4
power = a ** b # 6103515625
EXERCISE 3:
string_to_int = int(string_number) # 123
string_to_float = float(string_number) # 123.0
float_to_int = int(float_number) # 45
int_to_str = str(integer_number) # "100"
float_to_bool = bool(float_number) # True
EXERCISE 4:
result1 = True # x == z (15 == 15)
result2 = True # x != y (15 != 20)
result3 = False # x > y (15 > 20)
result4 = True # x <= z (15 <= 15)
result5 = True # x == z and y > x (True and True)
result6 = True # x > y or z == x (False or True)
result7 = True # not (x == y) (not False)
EXERCISE 5:
fahrenheit = (celsius * 9/5) + 32 # 82.4
kelvin = celsius + 273.15 # 301.15
EXERCISE 6:
area = math.pi * radius ** 2 # 153.94
circumference = 2 * math.pi * radius # 43.98
EXERCISE 7:
bmi = round(weight_kg / height_m ** 2, 1) # 22.9
# For category, you could use:
if bmi < 18.5:
category = "Underweight"
elif bmi < 25:
category = "Normal"
elif bmi < 30:
category = "Overweight"
else:
category = "Obese"
EXERCISE 8:
simple_interest = (principal * rate * time) / 100 # 1200
total_amount = principal + simple_interest # 6200
EXERCISE 9:
is_even = number % 2 == 0 # False
result = "Even" if is_even else "Odd" # "Odd"
EXERCISE 10:
# Method 1 (using temp):
temp = first
first = second
second = temp
# Method 2 (Python way - tuple unpacking):
first, second = second, first
"""
print("\n🎉 Great job completing the exercises!")
print("Move on to 02_strings when you're ready!")