Demonstrate your function mastery by solving these coding challenges.
Create two functions to convert temperatures between Celsius and Fahrenheit:
celsius_to_fahrenheit(celsius) - Convert Celsius to Fahrenheitfahrenheit_to_celsius(fahrenheit) - Convert Fahrenheit to CelsiusFormulas:
Both functions should return the converted value rounded to 1 decimal place.
Remember to use round(value, 1) to round to 1 decimal place. Make sure you're applying the formulas correctly - pay attention to the order of operations!
Create a function factorial(n) that calculates the factorial of a non-negative integer.
The factorial of n (written as n!) is the product of all positive integers from 1 to n.
Special case: 0! = 1
Error handling: If n is negative, return None.
Start by handling the edge cases: negative numbers (return None) and 0 (return 1). Then use a loop to multiply from 1 to n. You can also use recursion: n! = n × (n-1)!
Create a function calculate(a, b, operation="add") that performs basic math operations.
Parameters:
a - First numberb - Second numberoperation - Operation to perform (default: "add")Supported operations:
"add" - Returns a + b"subtract" - Returns a - b"multiply" - Returns a × b"divide" - Returns a ÷ b (return "Error" if b is 0)"power" - Returns a raised to the power of bIf an unknown operation is provided, return "Unknown operation".
Use if/elif/else to check the operation parameter. For "divide", check if b is 0 before dividing. Don't forget the default parameter value! Use ** for power.