25-sept Control Structure
LAB 1: Write a Python function is | prime_or_even_odd(n) that takesan | integer n as input and performs the | following: | | If n is a prime number, return True. | If n is not a prime number, return | "Even" if nis even, or "Odd" if nis odd. | Constraint | The input will be an integer between 1 and 1000. |
def prime_or_even_odd(n):
if n < 2:
return "Even" if n % 2 == 0 else "Odd"
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return "Even" if n % 2 == 0 else "Odd"
return True
LAB 2 : Create a Python function ~~ grade(score) that takes a student's score (integer) as input and returns a grade as per the following rules: | A:if score >= 90 | B: if score >= 80 | C:if score >= 70 | D: if score >= 60 | F: if score < 60
def grade(score):
if not isinstance(score, int):
raise TypeError("Score must be an integer")
if score < 0 or score > 100:
raise ValueError("Score must be between 0 and 100")
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
elif score >= 60:
return 'D'
else:
return 'F'
LAB 3 : Write a Python function | categorize_temperature(temp) that | takes a temperature (in Celsius) as | input and returns: | "Hot" if the temperature is above 30, | "Warm" if the temperature is between | 20 and 30 (inclusive), | “Cool" if the temperature is between | 10 and 19 (inclusive), | "Cold" if the temperature is below 10. | | Constraint | The input will be a float between -50.0 and 50.0. |
def categorize_temperature(temp):
if not isinstance(temp, (int, float)):
raise TypeError("Temperature must be a float or an integer")
if temp < -50.0 or temp > 50.0:
raise ValueError("Temperature must be between -50.0 and 50.0")
if temp > 30:
return "Hot"
elif 20 <= temp <= 30:
return "Warm"
elif 10 <= temp <= 19:
return "Cool"
else:
return "Cold"
LAB4: Write a Python function multiplication_table(number) that takes a number as input and prints its multiplication table from 1 to 10. Constraint The input will be an integer between 1 and 20.
def multiplication_table(number):
if not isinstance(number, int):
raise TypeError("Number must be an integer")
if number < 1 or number > 20:
raise ValueError("Number must be between 1 and 20")
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")
.png)