Python Lab File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Lab File

Category

Files

Created @June 12, 2023 7:46 PM

Reminder

Status Open

URL

Updated @June 12, 2023 11:05 PM

1. Program to calculate the area and circumference of a circle:

Input
import math

radius = float(input("Enter the radius of the circle: "))

area = math.pi * radius ** 2


circumference = 2 * math.pi * radius

print("Area of the circle:", area)


print("Circumference of the circle:", circumference)

Output
Enter the radius of the circle: 45
Area of the circle: 6361.725123519331
Circumference of the circle: 282.7433388230814

2. Program to calculate the area and perimeter of a rectangle:

Input
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))

area = length * width


perimeter = 2 * (length + width)

print("Area of the rectangle:", area)


print("Perimeter of the rectangle:", perimeter)

Output
Enter the length of the rectangle: 79
Enter the width of the rectangle: 74

Lab File 1
Area of the rectangle: 5846.0
Perimeter of the rectangle: 306.0

3. Program to check if a number is odd or even:

Input
number = int(input("Enter a number: "))

if number % 2 == 0:
print(number, "is even")
else:
print(number, "is odd")

Output
Enter a number: 47
47 is odd

4. Program to calculate simple interest:

Input
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time period (in years): "))

simple_interest = (principal * rate * time) / 100

print("Simple Interest:", simple_interest)

Output
Enter the principal amount: 3456
Enter the rate of interest: 43
Enter the time period (in years): 2002
Simple Interest: 2975132.16

5. Program to convert temperature from Celsius to Fahrenheit and vice versa:

Input
choice = input("Enter '1' to convert Celsius to Fahrenheit, or '2' to convert Fahrenhe
it to Celsius: ")

if choice == '1':
celsius = float(input("Enter the temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)
elif choice == '2':
fahrenheit = float(input("Enter the temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5/9
print("Temperature in Celsius:", celsius)
else:

Lab File 2
print("Invalid choice!")

Output
Enter '1' to convert Celsius to Fahrenheit, or '2' to convert Fahrenheit to Celsius: 1
Enter the temperature in Celsius: 2
Temperature in Fahrenheit: 35.6

6. Program to calculate the sum and average of ten numbers:

Input
numbers = []
for i in range(10):
number = float(input("Enter a number: "))
numbers.append(number)

total = sum(numbers)
average = total / len(numbers)

print("Sum:", total)
print("Average:", average)

Output
Enter a number: 98
Enter a number: 67
Enter a number: 56
Enter a number: 45
Enter a number: 34
Enter a number: 98
Enter a number: 56
Enter a number: 78
Enter a number: 34
Enter a number: 67
Sum: 633.0
Average: 63.3

7. Program to check if a year is a leap year or not:

Input
year = int(input("Enter a year: "))

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):


print(year, "is a leap year")
else:
print(year, "is not a leap year")

Output
Enter a year: 2020
2020 is a leap year

8. Program to convert distance from km to meter, cm, and millimeter:

Lab File 3
Input
distance_km = float(input("Enter the distance in kilometers: "))

distance_meter = distance_km * 1000


distance_cm = distance_km * 100000
distance_mm = distance_km * 1000000

print("Distance in meters:", distance_meter)


print("Distance in centimeters:", distance_cm)
print("Distance in millimeters:", distance_mm)

Output
Enter the distance in kilometers: 56
Distance in meters: 56000.0
Distance in centimeters: 5600000.0
Distance in millimeters: 56000000.0

9. Program to determine the largest of three numbers:

Input
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

largest = max(num1, num2, num3)

print("The largest number is:", largest)

Output
Enter the first number: 1
Enter the second number: 2
Enter the third number: 3
The largest number is: 3.0

10. Program to check if a number is an Armstrong number or not:

Input
number = int(input("Enter a number: "))
temp = number
sum = 0

while temp > 0:


digit = temp % 10
sum += digit ** 3
temp //= 10

if number == sum:
print(number, "is an Armstrong number")
else:
print(number, "is not an Armstrong number")

Lab File 4
Output
Enter a number: 2
2 is not an Armstrong number

11. Program to check if a number is a prime number or not:

Input
number = int(input("Enter a number: "))

if number > 1:
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
print(number, "is not a prime number")
break
else:
print(number, "is a prime number")
else:
print(number, "is not a prime number")

Output
Enter a number: 3
3 is a prime number

12. Program to check if a number is a palindrome or not:

Input
number = input("Enter a number: ")

if number == number[::-1]:
print(number, "is a palindrome")
else:
print(number, "is not a palindrome")

Output
Enter a number: 6
6 is a palindrome

13. Program to display the sum of the digits in a given number:

Input
number = int(input("Enter a number: "))

sum_of_digits = 0
temp = number

while temp > 0:


digit = temp % 10
sum_of_digits += digit
temp //= 10

Lab File 5
print("Sum of the digits in", number, "is", sum_of_d

Output
Enter a number: 5
Sum of the digits in 5 is 5

14. Program to check whether a person is eligible to vote or not:

Input
age = int(input("Enter your age: "))

if age >= 18:


print("You are eligible to vote")
else:
print("You are not eligible to vote")

Output
Enter your age: 20
You are eligible to vote

15. Program to calculate the root of a quadratic equation:

Input
import cmath

a = float(input("Enter the coefficient of x^2: "))


b = float(input("Enter the coefficient of x: "))
c = float(input("Enter the constant term: "))

discriminant = (b**2) - (4*a*c)


root1 = (-b - cmath.sqrt(discriminant)) / (2*a)
root2 = (-b + cmath.sqrt(discriminant)) / (2*a)

print("Root 1:", root1)


print("Root 2:", root2)

Output
Enter the coefficient of x^2: 34
Enter the coefficient of x: 5
Enter the constant term: 8
Root 1: (-0.07352941176470588-0.47946589373238546j)
Root 2: (-0.07352941176470588+0.47946589373238546j)

16. Program to calculate the root of a quadratic equation and check whether the
roots are real, equal, or imaginary:

Input
import cmath

Lab File 6
a = float(input("Enter the coefficient of x^2: "))
b = float(input("Enter the coefficient of x: "))
c = float(input("Enter the constant term: "))

discriminant = (b**2) - (4*a*c)


root1 = (-b - cmath.sqrt(discriminant)) / (2*a)
root2 = (-b + cmath.sqrt(discriminant)) / (2*a)

if discriminant > 0:
print("Root 1:", root1)
print("Root 2:", root2)
print("The roots are real and different")
elif discriminant == 0:
print("Root 1:", root1)
print("Root 2:", root2)
print("The roots are real and equal")
else:
print("Root 1:", root1)
print("Root 2:", root2)
print("The roots are complex or imaginary")

Output
Enter the coefficient of x^2: 45
Enter the coefficient of x: 6
Enter the constant term: 8
Root 1: (-0.06666666666666667-0.41633319989322654j)
Root 2: (-0.06666666666666667+0.41633319989322654j)

17. Program to calculate the factorial of a number:

Input
number = int(input("Enter a number: "))

factorial = 1

if number < 0:
print("Factorial does not exist for negative numbers")
elif number == 0:
print("Factorial of 0 is 1")
else:
for i in range(1, number + 1):
factorial *= i
print("Factorial of", number, "is", factorial)

Output
Enter a number: 6
Factorial of 6 is 720

18. Program to display an electricity bill based on unit consumption:

Lab File 7
Input
units_consumed = float(input("Enter the units consumed: "))

if units_consumed >= 200 and units_consumed <= 500:


rate_per_unit = 2
elif units_consumed > 500 and units_consumed <= 800:
rate_per_unit = 5
elif units_consumed > 800:
rate_per_unit = 10
penalty = 1000
total_bill = units_consumed * rate_per_unit + penalty
print("Electricity Bill:", total_bill)
else:
print("Invalid units consumed")

if units_consumed >= 200 and units_consumed <= 800:


total_bill = units_consumed * rate_per_unit
print("Electricity Bill:", total_bill)

Output
Enter the units consumed: 6
Invalid units consumed

19. Program to display the mark sheet of a student:

Input
subject1 = float(input("Enter marks for Subject 1: "))
subject2 = float(input("Enter marks for Subject 2: "))
subject3 = float(input("Enter marks for Subject 3: "))

total_marks = subject1 + subject2 + subject3


average_marks = total_marks / 3

print("Total Marks:", total_marks)


print("Average Marks:", average_marks)

if average_marks > 90:


grade = 'A'
elif average_marks > 80 and average_marks <= 90:
grade = 'B'
elif average_marks > 70 and average_marks <= 80:
grade = 'C'
elif average_marks > 60 and average_marks <= 70:
grade = 'D'
elif average_marks > 50 and average_marks <= 60:
grade = 'E'
else:
grade = 'FAIL'

print("Grade:", grade)

Output
Enter marks for Subject 1: 66
Enter marks for Subject 2: 67

Lab File 8
Enter marks for Subject 3: 89
Total Marks: 222.0
Average Marks: 74.0
Grade: C

20. Program to calculate the Pay Slip of an employee:

Input
basic_pay = float(input("Enter the basic pay: "))

HRA = 0.12 * basic_pay


DA = 0.33 * basic_pay
PF = 0.12 * basic_pay

net_pay = basic_pay + HRA + DA - PF


yearly_pay = 12 * net_pay

if yearly_pay >= 500000 and yearly_pay < 800000:


tax = 0.12
elif yearly_pay >= 800000 and yearly_pay < 1200000:
tax = 0.18
elif yearly_pay >= 1200000 and yearly_pay < 1600000:
tax = 0.22
else:
tax = 0.25

tax_amount = tax * yearly_pay

print("Net Pay:", net_pay)


print("Yearly Pay:", yearly_pay)
print("Tax Amount:", tax_amount)

Output
Enter the basic pay: 230
Net Pay: 305.9
Yearly Pay: 3670.7999999999997
Tax Amount: 917.6999999999999

21. Program to check if a number is positive, negative, or zero:

Input
number = float(input("Enter a number: "))

if number > 0:
print("The number is positive")
elif number < 0:
print("The number is negative")
else:
print("The number is zero")

Output

Lab File 9
Enter a number: 4
The number is positive

22. Program to display a Fibonacci series:

Input
def fibonacci_series(n):
fibonacci = [1, 1]
for i in range(2, n):
next_number = fibonacci[i - 1] + fibonacci[i - 2]
fibonacci.append(next_number)
return fibonacci

terms = int(input("Enter the number of terms: "))

fibonacci = fibonacci_series(terms)
print("Fibonacci Series:", fibonacci)

Output
Enter the number of terms: 4
Fibonacci Series: [1, 1, 2, 3]

23. Program to calculate the sum of the first ten natural numbers:

Input
n = 10
sum_of_numbers = (n * (n + 1)) / 2
print("Sum of the first ten natural numbers:", sum_of_numbers)

Output
Sum of the first ten natural numbers: 55.0

24. Program to calculate the sum of a series:

Input
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

sum_of_series = 0

for i in range(1, n+1):


sum_of_series += x ** i

print("Sum of the series:", sum_of_series)

Output
Enter the value of x: 45
Enter the value of n: 8
Sum of the series: 17197287331320.0

Lab File 10
25. Program to calculate the sum of a series using factorials:

Input
import math

x = float(input("Enter the value of x: "))


n = int(input("Enter the value of n: "))

sum_of_series = 0

for i in range(1, n+1):


sum_of_series += (x ** i) / math.factorial(i)

print("Sum of the series:", sum_of_series)

Output
Enter the value of x: 7
Enter the value of n: 9
Sum of the series: 909.7493827160494

26. Program to calculate the sum of the series:

Input
n = int(input("Enter the value of n: "))

sum_of_series = 0

for i in range(1, n+1):


sum_of_series += i ** 3

print("Sum of the series:", sum_of_series)

Output
Enter the value of n: 7
Sum of the series: 784

27. Program to display the series:

Input
n = int(input("Enter the value of n: "))

for i in range(1, n+1):


print(i**2, end=" ")

Output
Enter the value of n: 6
1 4 9 16 25 36

Lab File 11
28. Program to display multiplication from 1 to 20:

Input
for i in range(1, 21):
for j in range(1, 11):
print(i*j, end="\t")
print()

Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
11 22 33 44 55 66 77 88 99 110
12 24 36 48 60 72 84 96 108 120
13 26 39 52 65 78 91 104 117 130
14 28 42 56 70 84 98 112 126 140
15 30 45 60 75 90 105 120 135 150
16 32 48 64 80 96 112 128 144 160
17 34 51 68 85 102 119 136 153 170
18 36 54 72 90 108 126 144 162 180
19 38 57 76 95 114 133 152 171 190
20 40 60 80 100 120 140 160 180 200

29. Program to check if a number is a perfect square or not:

Input
import math

number = int(input("Enter a number: "))

if math.isqrt(number)**2 == number:
print(number, "is a perfect square")
else:
print(number, "is not a perfect square")

Output
Enter a number: 4
4 is a perfect square

30. Program to calculate the GCD of two numbers:

Input
def gcd(a, b):
if b == 0:

Lab File 12
return a
else:
return gcd(b, a % b)

num1 = int(input("Enter the first number: "))


num2 = int(input("Enter the second number: "))

result = gcd(num1, num2)


print("GCD:", result)

Output

31. Program to display the pattern:

1
12
123
1234
12345

Input
rows = int(input("Enter the number of rows: "))

for i in range(1, rows+1):


for j in range(1, i+1):
print(j, end=" ")
print()

Output
Enter the number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

32. Program to display the pattern:

*
**
***
****
*****

Input
rows = int(input("Enter the number of rows: "))

Lab File 13
for i in range(1, rows+1):
for j in range(1, i+1):
print("*", end=" ")
print()
Output

Enter the number of rows: 5


*
* *
* * *
* * * *
* * * * *

33. Program to display the pattern:

1
123
12345
1234567
123456789

Input
def display_pattern(rows):
for i in range(1, rows + 1):
print(" " * (rows - i) + " ".join(str(j) for j in range(1, 2 * i)))

# Test the function


display_pattern(5)

Output
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9

34. Program to display the pattern:

1
10
101
1010
10101

Input
def display_pattern(rows):

Lab File 14
for i in range(1, rows + 1):
for j in range(1, i + 1):
if j % 2 == 0:
print("0", end=" ")
else:
print("1", end=" ")
print()

# Test the function


display_pattern(5)

Output
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1

35. Program to convert upper case letter to lower case and vice versa:

Input
character = input("Enter a character: ")

if character.isupper():
converted_character = character.lower()
print("Converted character:", converted_character)
elif character.islower():
converted_character = character.upper()
print("Converted character:", converted_character)
else:
print("Invalid input")
Output
Enter a character: T
Converted character: t

36. Program to display the pattern:

Input
def display_pattern(rows):
for i in range(1, rows + 1):
print(" " * (rows - i) + (str(i) + " ") * i)

# Test the function


display_pattern(5)

Output
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5

Lab File 15
37. Program to convert a decimal to binary:

Input
decimal = int(input("Enter a decimal number: "))

binary = bin(decimal)[2:]

print("Binary representation:", binary)

Output
Enter a decimal number: 4
Binary representation: 100

38. Program to check if a number is Krishnamurthy's number or not:

Input
def factorial(num):
if num == 0 or num == 1:
return 1
else:
return num * factorial(num - 1)

def is_krishnamurthy(num):
original_num = num
sum_of_factorial = 0

while num != 0:
digit = num % 10
sum_of_factorial += factorial(digit)
num = num // 10

return original_num == sum_of_factorial

number = int(input("Enter a number: "))

if is_krishnamurthy(number):
print(number, "is a Krishnamurthy number")
else:
print(number, "is not a Krishnamurthy number")

Output
Enter a number: 7
7 is not a Krishnamurthy number

39. Program to square and cube a number:

Input
number = int(input("Enter a number: "))

square = number ** 2
cube = number ** 3

Lab File 16
print("Square:", square)
print("Cube:", cube)

Output
Enter a number: 5
Square: 25
Cube: 125

40. Program to calculate mean, standard deviation, mode, median, and standard
deviation:

Input
import statistics

data = []

n = int(input("Enter the number of data points: "))

for i in range(n):
value = float(input("Enter a value: "))
data.append(value)

mean = statistics.mean(data)
standard_deviation = statistics.stdev(data)
mode = statistics.mode(data)
median = statistics.median(data)

print("Mean:", mean)
print("Standard Deviation:", standard_deviation)
print("Mode:", mode)
print("Median:", median)

Output
Enter the number of data points: 7
Enter a value: 7
Enter a value: 9
Enter a value: 9
Enter a value: 6
Enter a value: 5
Enter a value: 8
Enter a value: 8
Mean: 7.428571428571429
Standard Deviation: 1.511857892036909
Mode: 9.0
Median: 8.0

41. Program to calculate the sum of the series:

X^1/1+x^3/3!+------------+x^N/n!

Lab File 17
Input
import math

def calculate_series_sum(x, n):


series_sum = 0

for i in range(1, n + 1):


term = x ** i / math.factorial(i)
series_sum += term

return series_sum

# Test the function


x = 2
n = 5
result = calculate_series_sum(x, n)
print(f"The sum of the series is: {result}")
Output
The sum of the series is: 6.333333333333333

42. Program to calculate the sum of the series:

X^2/2!+x^4/4!+------------+x^2N/2n!

Input
import math

def calculate_series_sum(x, n):


series_sum = 0

for i in range(1, n + 1):


term = (x ** (2 * i)) / (2 * i * math.factorial(i))
series_sum += term

return series_sum

# Test the function


x = 3
n = 4
result = calculate_series_sum(x, n)
print(f"The sum of the series is: {result}")
Output
The sum of the series is: 36.9375

43. Program to calculate the sum of the series:

X^2/2!+x^4/4!+------------+x^2N/2n!

Input
import math

Lab File 18
def calculate_series_sum(x, n):
series_sum = 0

for i in range(1, n + 1):


term = (x ** (2 * i)) / (2 * i * math.factorial(2 * i))
series_sum += term

return series_sum

# Test the function


x = 2
n = 5
result = calculate_series_sum(x, n)
print(f"The sum of the series is: {result}")
Output
The sum of the series is: 3.6666666666666665

44. Program to add an element to a list:

Input
my_list = [1, 2, 3, 4, 5]

element = int(input("Enter the element to add: "))

my_list.append(element)

print("Updated list:", my_list)


Output
Enter the element to add: 5
Updated list: [1, 2, 3, 4, 5, 5]

45. Program to delete an element from a list:

Input
my_list = [1, 2, 3, 4, 5]

element = int(input("Enter the element to delete: "))

if element in my_list:
my_list.remove(element)
print("Updated list:", my_list)
else:
print("Element not found in the list.")

Output
Enter the element to delete: 4
Updated list: [1, 2, 3, 5]

46. Program to add an element to a tuple:

Lab File 19
Input
my_tuple = (1, 2, 3, 4, 5)

element = int(input("Enter the element to add: "))

my_tuple = my_tuple + (element,)

print("Updated tuple:", my_tuple)


Output
Enter the element to add: 4
Updated tuple: (1, 2, 3, 4, 5, 4)

47. Program to delete an element from a tuple:

Input
my_tuple = (1, 2, 3, 4, 5)

element = int(input("Enter the element to delete: "))

if element in my_tuple:
my_list = list(my_tuple)
my_list.remove(element)
my_tuple = tuple(my_list)
print("Updated tuple:", my_tuple)
else:
print("Element not found in the tuple.")
Outout

Enter the element to delete: 4


Updated tuple: (1, 2, 3, 5)

48. Program to add an element to a list:

Input
my_list = [1, 2, 3, 4, 5]

element = int(input("Enter the element to add: "))

my_list.append(element)

print("Updated list:", my_list)


Output
Enter the element to add: 8
Updated list: [1, 2, 3, 4, 5, 8]

49. Write a program in Python to calculate the HCF and LCF of tow Numbers.

Lab File 20
Input
def calculate_hcf(x, y):
while y != 0:
x, y = y, x % y
return x

def calculate_lcm(x, y):


gcd = calculate_hcf(x, y)
lcm = (x * y) // gcd
return lcm

num1 = int(input("Enter the first number: "))


num2 = int(input("Enter the second number: "))

hcf = calculate_hcf(num1, num2)


lcm = calculate_lcm(num1, num2)

print("HCF of", num1, "and", num2, "is", hcf)


print("LCM of", num1, "and", num2, "is", lcm)
Output
Enter the first number: 7
Enter the second number: 5
HCF of 7 and 5 is 1
LCM of 7 and 5 is 35

Lab File 21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy