SBL EXP 2 - Python

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

EXPERIMENT NO: -02

Aim: - a) Write a python program to convert


temperature to and from Celsius to Fahrenheit.
Theory: -
 Celsius to Fahrenheit is the conversion of temperature from Celsius unit to
Fahrenheit unit. Temperature scales provide a way of measuring how hot or cold a
body is. Fahrenheit and Celsius are temperature scales. The symbol used for
Fahrenheit is °F whereas for Celsius it's °C. Thus, the Celsius to Fahrenheit formula is
also commonly referred to as the C to F formula.

Formula: - To convert temperature into Celsius to Fahrenheit


°F = °C × (9/5) + 32
Where,
C = Measure of temperature in degree Celsius (°C).
F = Measure of temperature in degree Fahrenheit (°F).

 Step 1: Identify the temperature value in Celsius.


 Step 2: Multiply the given temperature by 9.
 Step 3: Divide the product by 5.
 Step 4: Add 32 to that number. That will be the required value in degree
Fahrenheit (°F).

To convert temperature into Fahrenheit to Celsius

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.

b) Write a python program to swap two variables.


The Swapping Variables Theory is a mathematical principle that states that it is
possible to exchange the values of two variables by following a simple process.
The theory works by creating a temporary variable that stores the value of one
of the variables, allowing the values to be exchanged without losing any
information.
The process can be described as follows:
1. Create a temporary variable: This variable will hold the value of one of
the two variables that are to be swapped.
2. Store the value of one of the variables in the temporary variable: The
value of one of the variables is assigned to the temporary variable.
3. Assign the value of the other variable to the first variable: The value of
the second variable is assigned to the first variable.
4. Assign the value stored in the temporary variable to the second variable:
The value stored in the temporary variable is assigned to the second variable.
This process can be represented in a mathematical formula as follows:
temp = a; a = b; b = temp;
This theory is useful in a wide range of applications, including computer
programming, cryptography, and even in everyday life. By following these
simple steps, it is possible to exchange the values of two variables with ease
and accuracy.

CODE:-
x=5
y = 10
print("Before swapping: x =", x, "y =", y)
x, y = y, x
print("After swapping: x =", x, "y =", y)

OUTPUT:-

Conclusion: - In conclusion, swapping two variables in Python can be done using a


temporary variable, tuple unpacking, or XOR bitwise operation. These methods effectively
exchange the values of two variables, providing a useful tool for programming.
C)write a python program to generate a random
number.
The Random Number Generation Theory in Python is a principle that allows
the generation of random numbers using the built-in random library in Python.
The library provides several functions that can be used to generate random
numbers with different distributions, such as uniform, Gaussian, and
exponential distributions.
One of the most commonly used functions in the random library is the
randint() function. This function generates a random integer between two
specified values, inclusive of both values. To generate a random number in
Python, the following steps should be followed:
Import the random library: This can be done by using the import statement
"import random".
Call the randint() function: This function generates a random integer between
two specified values, inclusive of both values. For example, to generate a
random integer between 0 and 10, the following line of code should be used:
random.randint(0, 10).
Store the generated number in a variable: To store the generated random
number in a variable, it should be assigned to a variable. For example, the
generated random number can be stored in the variable "x" by using the
following line of code: x = random.randint(0, 10).
This theory is useful in various applications, such as simulation, data analysis,
and even in game development. By using the randint() function, it is possible to
generate random numbers with ease and accuracy in Python.

CODE:-
import random
random_number = random.randint(1, 100)
print("Random number:", random_number)

OUTPUT:-

Conclusion: - In conclusion, generating random numbers in Python can be done using


the "random" module, with "randint" for integers and "random" for floats. These functions
provide a convenient way to create randomized data for various programming applications.

You might also like