Python Assignments: Functions and If Conditions
Assignment 1: Temperature Converter
Objective: Create a function that converts temperatures between Celsius and Fahrenheit.
Instructions:
1. Define a function convert_temperature that takes two arguments: temperature and unit .
2. If unit is 'C' , convert the temperature to Fahrenheit.
3. If unit is 'F' , convert the temperature to Celsius.
4. Use if conditions to determine the conversion formula.
5. Return the converted temperature.
Example:
convert_temperature(100, 'C') # Output: 212.0
convert_temperature(32, 'F') # Output: 0.0
Assignment 2: Grade Calculator
Objective: Write a function to calculate the grade based on a score.
Instructions:
1. Define a function calculate_grade that takes a single argument score .
2. Use if-elif-else conditions to determine the grade:
A for scores 90 and above
B for scores 80-89
C for scores 70-79
D for scores 60-69
F for scores below 60
3. Return the grade as a string.
Example:
calculate_grade(85) # Output: 'B'
calculate_grade(72) # Output: 'C'
Assignment 3: Even or Odd Checker
Objective: Create a function to check if a number is even or odd.
Instructions:
1. Define a function is_even_or_odd that takes an integer number .
2. Use an if condition to check if the number is even or odd.
3. Return 'Even' if the number is even, and 'Odd' if the number is odd.
Example:
is_even_or_odd(10) # Output: 'Even'
is_even_or_odd(7) # Output: 'Odd'
Assignment 4: Simple Calculator
Objective: Implement a basic calculator using functions and if conditions.
Instructions:
1. Define a function simple_calculator that takes three arguments: num1 , num2 , and operation .
2. Use if conditions to perform the operation:
'add' for addition
'subtract' for subtraction
'multiply' for multiplication
'divide' for division
3. Return the result of the operation.
4. Handle division by zero by returning 'Error: Division by zero' .
Example:
simple_calculator(10, 5, 'add') # Output: 15
simple_calculator(10, 5, 'subtract') # Output: 5
simple_calculator(10, 5, 'multiply') # Output: 50
simple_calculator(10, 0, 'divide') # Output: 'Error: Division by zero'
Assignment 5: Leap Year Checker
Objective: Write a function to determine if a year is a leap year.
Instructions:
1. Define a function is_leap_year that takes an integer year .
2. Use if conditions to check if the year is a leap year:
A year is a leap year if it is divisible by 4, but not divisible by 100, except if it is also divisible by 400.
3. Return True if the year is a leap year, otherwise False .
Example:
is_leap_year(2020) # Output: True
is_leap_year(1900) # Output: False
is_leap_year(2000) # Output: True
These assignments provide a good mix of practice with functions and conditional logic in Python. Feel free to
adjust the complexity based on the learners' level.