Python Programming Lab Manual
1) Program to display data of different types using variable and literal constants
Aim:
To display data of various data types using variables and literal constants.
Theory:
In Python, data types like integers, floats, strings, and booleans are used to store and
manipulate data. Literal constants are fixed values, and variables are used to store these
values.
# Variable and literal constants
integer_var = 10
float_var = 20.5
string_var = "Hello, Python!"
boolean_var = True
print("Integer:", integer_var)
print("Float:", float_var)
print("String:", string_var)
print("Boolean:", boolean_var)
Output:
Integer: 10
Float: 20.5
String: Hello, Python!
Boolean: True
Conclusion: The program successfully demonstrated the use of different data types using
variable and literal constants.
2) Program to read variables from the user
Aim:
To take input of different data types from the user.
Theory:
The `input()` function in Python is used to read data from the user. By default, it returns a
string, so type conversion is necessary for other data types.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
salary = float(input("Enter your salary: "))
print("Name:", name)
print("Age:", age)
print("Salary:", salary)
Output:
Enter your name: John
Enter your age: 25
Enter your salary: 50000
Name: John
Age: 25
Salary: 50000.0
Conclusion: The program successfully accepted input from the user and displayed it after
appropriate type conversions.
3) Program to exhibit indentation errors
Aim:
To demonstrate the effect of indentation errors in Python.
Theory:
Python uses indentation to define blocks of code. Improper indentation leads to errors.
# Incorrect indentation
x = 5
if x > 0:
print("Positive") # This will raise an indentation error
Output:
IndentationError: expected an indented block
Conclusion: The program displayed an indentation error, showing the importance of
proper code indentation in Python.
4) Program to perform all operations and expressions
Aim:
To perform basic arithmetic operations and evaluate expressions.
Theory:
Python supports operators like +, -, *, /, %, and can evaluate complex expressions.
a = 10
b = 5
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Modulus:", a % b)
print("Expression (a + b * 2):", a + b * 2)
Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Modulus: 0
Expression (a + b * 2): 20
Conclusion: The program successfully performed various arithmetic operations and
evaluated an expression.
5) Program to perform area of a circle
Aim:
To calculate the area of a circle.
Theory:
Area of circle = π × r². We use `math.pi` for π and `input()` for radius.
import math
r = float(input("Enter radius: "))
area = math.pi * r *r
print("Area of Circle:", area)
Output:
Enter radius: 3
Area of Circle: 28.274333882308138
Conclusion: The program successfully calculated the area of a circle using the given
radius.
6) Program to calculate average of two numbers
Aim:
To compute average of two given numbers.
Theory:
Average = (num1 + num2) / 2
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
average = (a + b) / 2
print("Average:", average)
Output:
Enter first number: 10
Enter second number: 20
Average: 15.0
Conclusion: The program successfully calculated the average of two numbers.
7) Program to convert degree Fahrenheit into degree Celsius
Aim:
To convert temperature from Fahrenheit to Celsius.
Theory:
Celsius = (Fahrenheit - 32) × 5/9
f = float(input("Enter temperature in Fahrenheit: "))
c = (f - 32) * 5 / 9
print("Temperature in Celsius:", c)
Output:
Enter temperature in Fahrenheit: 98.6
Temperature in Celsius: 37.0
Conclusion: The program successfully converted temperature from Fahrenheit to Celsius.
8) Program to calculate salary of an employee
Aim:
To calculate gross and net salary of an employee based on basic pay.
Theory:
Gross Salary = Basic + HRA + TA
Net Salary = Gross - Professional Tax
HRA = 10% of Basic, TA = 5% of Basic, PT = 2% of Gross
basic = float(input("Enter basic pay: "))
hra = 0.10 * basic
ta = 0.05 * basic
gross = basic + hra + ta
pt = 0.02 * gross
net = gross - pt
print("Gross Salary:", gross)
print("Net Salary:", net)
Output:
Enter basic pay: 30000
Gross Salary: 34500.0
Net Salary: 33810.0
Conclusion: The program successfully calculated the gross and net salary of an employee
using the given salary components.