SBL EXP 2 - Python
SBL EXP 2 - Python
The conversion of temperature from Fahrenheit to Celsius can be described by the following
formula:
C = (F - 32) * 5/9
where C is the temperature in Celsius and F is the temperature in Fahrenheit.
This formula is based on the fact that the difference between the freezing and boiling points
of water is 180°F in Fahrenheit and 100°C in Celsius. This means that a temperature increase
of 1°F is equivalent to an increase of 5/9°C.
By subtracting 32 from the Fahrenheit temperature and multiplying the result by 5/9, the
formula converts the temperature to Celsius, which is the standard unit for measuring
temperature in most of the world.
CODE:-
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
return celsius
temperature = float(input("Enter temperature: "))
unit = input("Enter unit (C or F): ")
if unit == "C":
result = celsius_to_fahrenheit(temperature)
result_unit = "F"
elif unit == "F":
result = fahrenheit_to_celsius(temperature)
result_unit = "C"
else:
print("Invalid unit")
print(f"Temperature: {result} {result_unit}")
OUTPUT:-
Conclusion: - With the help of mathematical formulas and conversion tools, it is
possible to determine the temperatures to and from Celsius to Fahrenheit.
CODE:-
x=5
y = 10
print("Before swapping: x =", x, "y =", y)
x, y = y, x
print("After swapping: x =", x, "y =", y)
OUTPUT:-
CODE:-
import random
random_number = random.randint(1, 100)
print("Random number:", random_number)
OUTPUT:-