Python Lab File
Python Lab File
Python Lab File
Category
Files
Reminder
Status Open
URL
Input
import math
Output
Enter the radius of the circle: 45
Area of the circle: 6361.725123519331
Circumference of the circle: 282.7433388230814
Input
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
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
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
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): "))
Output
Enter the principal amount: 3456
Enter the rate of interest: 43
Enter the time period (in years): 2002
Simple Interest: 2975132.16
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
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
Input
year = int(input("Enter a year: "))
Output
Enter a year: 2020
2020 is a leap year
Lab File 3
Input
distance_km = float(input("Enter the distance in kilometers: "))
Output
Enter the distance in kilometers: 56
Distance in meters: 56000.0
Distance in centimeters: 5600000.0
Distance in millimeters: 56000000.0
Input
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
Output
Enter the first number: 1
Enter the second number: 2
Enter the third number: 3
The largest number is: 3.0
Input
number = int(input("Enter a number: "))
temp = number
sum = 0
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
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
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
Input
number = int(input("Enter a number: "))
sum_of_digits = 0
temp = number
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
Input
age = int(input("Enter your age: "))
Output
Enter your age: 20
You are eligible to vote
Input
import cmath
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: "))
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)
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
Lab File 7
Input
units_consumed = float(input("Enter the units consumed: "))
Output
Enter the units consumed: 6
Invalid units consumed
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: "))
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
Input
basic_pay = float(input("Enter the basic pay: "))
Output
Enter the basic pay: 230
Net Pay: 305.9
Yearly Pay: 3670.7999999999997
Tax Amount: 917.6999999999999
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
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
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
Input
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
sum_of_series = 0
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
sum_of_series = 0
Output
Enter the value of x: 7
Enter the value of n: 9
Sum of the series: 909.7493827160494
Input
n = int(input("Enter the value of n: "))
sum_of_series = 0
Output
Enter the value of n: 7
Sum of the series: 784
Input
n = int(input("Enter the value of n: "))
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
Input
import math
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
Input
def gcd(a, b):
if b == 0:
Lab File 12
return a
else:
return gcd(b, a % b)
Output
1
12
123
1234
12345
Input
rows = int(input("Enter the number of rows: "))
Output
Enter the number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
*
**
***
****
*****
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
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)))
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
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()
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
Input
def display_pattern(rows):
for i in range(1, rows + 1):
print(" " * (rows - i) + (str(i) + " ") * i)
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:]
Output
Enter a decimal number: 4
Binary representation: 100
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
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
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 = []
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
X^1/1+x^3/3!+------------+x^N/n!
Lab File 17
Input
import math
return series_sum
X^2/2!+x^4/4!+------------+x^2N/2n!
Input
import math
return series_sum
X^2/2!+x^4/4!+------------+x^2N/2n!
Input
import math
Lab File 18
def calculate_series_sum(x, n):
series_sum = 0
return series_sum
Input
my_list = [1, 2, 3, 4, 5]
my_list.append(element)
Input
my_list = [1, 2, 3, 4, 5]
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]
Lab File 19
Input
my_tuple = (1, 2, 3, 4, 5)
Input
my_tuple = (1, 2, 3, 4, 5)
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
Input
my_list = [1, 2, 3, 4, 5]
my_list.append(element)
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
Lab File 21