Python Expt 4
Python Expt 4
EXPERIMENT-04
❖ THEORY:
1. Conditional blocks statements or loops: They are fundamental concepts in programming that allow
you to control the flow of your code based on certain conditions.
Conditional statements allow you to execute certain parts hf your code only if specific conditions are
met. Python provides the following condition statements
Loops allow you to run a code multiple times which is helpful for repetitive task like calculations or
processing data
a. for loop: used to iterate over a sequence(like a list, tuple, list, or range)
b. while loop: repeats a block of code as long as the condition is true.
Python is a powerful language widely used for scientific computing due to its simplicity and
versatility. Two essential packages for mathematical and scientific computations in python are
NumPy and SciPy. These packages are particularly useful in fields like chemistry where you often
need to perform complex mathematical calculations and data analysis.
18
4/09/24
b. SciPy(Scientific Python)
SciPy builds on Numpy and provides additional functionality for scientific computing. It includes
modules for optimization, Integration, interpolation, eigen value problems, algebraic equations
and other tasks common in science and engineering.
Uses of SciPy are:
- Advanced mathematical functions: It is useful for solving differential equations, integrating
functions and optimizing parameters which are common tasks in physical chemistry.
❖ PRACTICE:
1. #Create 1-D NumPy array of atomic mass of oxygen, nitrogen, fluorine and helium
import numpy as np
a=np.array(["Oxygen","O","16"])
b=np.array(["Nitrogen","N","14"])
c=np.array(["Fluorine","F","18"])
d=np.array(["Helium","H","4"])
print(a,b,c,d)
#output
19
4/09/24
4. # check_reaction_conditions(temperature, pressure):
def check_reaction_conditions(temperature, pressure):
if temperature > 25 and pressure > 1:
return "Reaction conditions met."
else:
return "Reaction conditions not met."
# Example usage
temp = float(input("Enter the temperature (°C): "))
press = float(input("Enter the pressure (atm): "))
result = check_reaction_conditions(temp, press)
print(result)
#output
20
4/09/24
#output
6. # Constants
MIN_CONCENTRATION = 0.1 # Minimum concentration in Molar
MAX_CONCENTRATION = 2.0 # Maximum concentration in Molar
# User input for moles of solute and volume of solution
moles_of_solute = float(input("Enter the moles of solute: "))
volume_of_solution = float(input("Enter the volume of solution (in liters): "))
# Calculate molarity
molarity = moles_of_solute / volume_of_solution
# Check if molarity is within the specified range
if molarity < MIN_CONCENTRATION:
print("The solution is too dilute.")
elif molarity > MAX_CONCENTRATION:
print("The solution is too concentrated.")
else:
print("The solution is within the desired concentration range.")
#output
21
4/09/24
8. # Avogadro's number
avogadro_number = 6.022e23
# Number of moles for each case
moles = [1, 2, 0.5, 0.1]
# Calculate number of molecules for each amount of moles
for mole in moles:
number_of_molecules = mole * avogadro_number
print(f"{mole} moles contains {number_of_molecules:.2e} molecules")
#output
22
4/09/24
❖ RESULT: Conditional statements, conditional loops and packages in python were practiced
23
4/09/24
24