0% found this document useful (0 votes)
69 views15 pages

Py 2

This document contains 23 Python programs submitted by a student. The programs cover basic concepts like printing text, performing mathematical operations, conditional statements, and more. Each program is labeled and includes the code and sample output. The student has submitted this lab work to their instructor for the Python Language course at their university.

Uploaded by

Abhirath kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views15 pages

Py 2

This document contains 23 Python programs submitted by a student. The programs cover basic concepts like printing text, performing mathematical operations, conditional statements, and more. Each program is labeled and includes the code and sample output. The student has submitted this lab work to their instructor for the Python Language course at their university.

Uploaded by

Abhirath kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

PYTHON LANGUAGE LAB

PRACTICAL FILE
(KCS-453)

GL BAJAJ INSTITUTE OF TECHNOLGY AND MANAGEMENT


GREATER NOIDA

BACHELOR OF TECHNOLOGY
In
COMPUTER SCIENCE AND ENGINEERING

Session: 2022-23

SUBMITTED BY: SUBMITTED TO:


Name: ABHIRATH KUMAR Ms. KIRTI
Section: CSE-A (Assistant Prof.)
Semester: 4
Roll No.: 2101920100009
PROGRAM 1
Python Program to Print Hello world

Print(“ Hello world”)

PROGRAM 2
Python Program to Add Two Numbers.

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


num2 = int(input("Enter the second number: "))
sum = num1 + num2
print("The sum of", num1, "and", num2, "is:", sum)

PROGRAM 3
Python Program to Find the Square Root.
num = int(input("Enter a number: "))
sqrt = num ** 0.5
print("The square root of", num, "is:", sqrt)

PROGRAM 4
Python Program to Calculate the Area of a Triangle.

side1 = int(input("Enter the length of side 1: "))


side2 = int(input("Enter the length of side 2: "))
side3 = int(input("Enter the length of side 3: "))
s = (side1 + side2 + side3) / 2
area = (s * (s - side1) * (s - side2) * (s - side3)) ** 0.
print("The area of the triangle is:", area)
PROGRAM 5
Python Program to Solve Quadratic Equation using exponent
operator.
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
if discriminant > 0:
root1 = (-b + discriminant ** 0.5) / (2 * a)
root2 = (-b - discriminant ** 0.5) / (2 * a)
print("The equation has two real and distinct roots:")
print("Root 1:", root1)
print("Root 2:", root2)
elif discriminant == 0:
root = -b / (2 * a)
print("The equation has one real root:")
print("Root:", root)
else:
real_part = -b / (2 * a)
imaginary_part = (-discriminant) ** 0.5 / (2 * a)
root1 = complex(real_part, imaginary_part)
root2 = complex(real_part, -imaginary_part)
print("The equation has complex roots:")
print("Root 1:", root1)
print("Root 2:", root2)

PROGRAM 6
Python Program to Swap Two Variables

a = input("Enter the value of variable a: ")


b = input("Enter the value of variable b: ")
print("Before swapping:")
print("a =", a)
print("b =", b)
temp = a
a=b
b = temp
print("After swapping:")
print("a =", a)
print("b =", b)
PROGRAM 7
Python Program to Convert Kilometres to Miles (1 mile = 1.67 km)

kilometers = float(input("Enter the distance in kilometers: "))


miles = kilometers / 1.67
print("Distance in miles:", miles)

PROGRAM 8
Python Program to Convert Celsius To Fahrenheit ( F=9*C/5+32)

celsius = float(input("Enter the temperature in Celsius: "))


fahrenheit = (9 * celsius / 5) + 32
print("Temperature in Fahrenheit:", fahrenheit)

PROGRAM 9
Python program to convert height (in feet and inches) to centimetres.
(1 feet= 12 inches, 1 inch=2.54 cm) and (cms=feet*12*2.54+inches*2.54)

feet = int(input("Enter the height in feet: "))


inches = int(input("Enter the height in inches: "))
cms = feet * 12 * 2.54 + inches * 2.54
print("Height in centimeters:", cms)

PROGRAM 10
Python program to calculate the hypotenuse of a right-angled triangle.
(h=(b*b +h*h)**0.5)

base = float(input("Enter the length of the base: "))


height = float(input("Enter the length of the height: "))
hypotenuse = (base ** 2 + height ** 2) ** 0.5
print("Hypotenuse of the right-angled triangle:", hypotenuse)
PROGRAM 11
Python program to convert all units of time into seconds.

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


hours = int(input("Enter the number of hours: "))
minutes = int(input("Enter the number of minutes: "))
seconds = int(input("Enter the number of seconds: "))
total_seconds = (days * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60) + seconds
print("Total seconds:", total_seconds)

PROGRAM 12
Python program to display your details like name, age, address in
three different lines.

name = input("Enter your name: ")


age = input("Enter your age: ")
address = input("Enter your address: ")
print("Name:", name)
print("Age:", age)
print("Address:", address)
PROGRAM 13
To write a python program to compute the GCD of two numbers.
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if num1 < num2:
smaller = num1
else:
smaller = num2
gcd = 1
for i in range(1, smaller + 1):
if (num1 % i == 0 and num2 % i == 0):
gcd = i
print("GCD of", num1, "and", num2, "is:", gcd)
PROGRAM 14
To write a python program exponentiation (power of a number).

base = float(input("Enter the base number: "))


exponent = int(input("Enter the exponent: "))
result = 1
for _ in range(exponent):
result *= base
print(base, "raised to the power of", exponent, "is:", result)

PROGRAM 15
Write a program to find greatest no. two no.

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


num2 = float(input("Enter the second number: "))
if num1 > num2:
greatest = num1
else:
greatest = num2
print("The greatest number is:", greatest)

PROGRAM 16
Write a program to check if the given no. is odd or even

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


if( num % 2 == 0):
print(num, "is an even number.")
else:
print(num, "is an odd number.")
PROGRAM 17
Write a program to check if a given integer is divisible by no. or not

num = int(input("Enter an integer: "))


divisor = int(input("Enter the divisor: "))
if num % divisor == 0:
print(num, "is divisible by", divisor)
else:
print(num, "is not divisible by", divisor)

PROGRAM 18
Write a program to find greatest of 3 no. using else if ladder

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


num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
if num1 > num2 and num1 > num3:
greatest = num1
elif num2 > num1 and num2 > num3:
greatest = num2
else:
greatest = num3
print("The greatest number is:", greatest)
PROGRAM 19
Write a program to check whether an entered year is leap year or not
year = int(input("Enter a year: "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year, "is not a leap year")

PROGRAM 20
Write a program to check whether an alphabet entered by user is vowel
or consonant.

alphabet = input("Enter an alphabet: ")


alphabet = alphabet.lower()
if alphabet in ['a', 'e', 'i', 'o', 'u']:
print(alphabet, "is a vowel")
else:
print(alphabet, "is a consonant")
PROGRAM 21
. Write a program to stimulate arithmetic calculator.

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


num2 = float(input("Enter the second number: "))
operation = input("Enter the operation (+, -, *, /): ")
if operation == "+":
result = num1 + num2
print("The sum is:", result)
elif operation == "-":
result = num1 - num2
print("The difference is:", result)
elif operation == "*":
result = num1 * num2
print("The product is:", result)
elif operation == "/":
result = num1 / num2
print("The quotient is:", result)
else:
print("Invalid operation")
PROGRAM 22
Write a program to check students’ grades. Your program should
fulfill the following conditions:
Grade A – Outstanding
Grade B – Excellent
Grade C – Very Good
Grade D – Good
Grade E – Satisfactory
others – Unrecognized
A program should also ask to enter the student’s name, class, and
section.

name = input("Enter student's name: ")


class_name = input("Enter student's class: ")
section = input("Enter student's section: ")
marks = float(input("Enter student's marks: "))
if marks >= 90:
grade = "A - Outstanding"
elif marks >= 80:
grade = "B - Excellent"
elif marks >= 70:
grade = "C - Very Good"
elif marks >= 60:
grade = "D - Good"
elif marks >= 50:
grade = "E - Satisfactory"
else:
grade = "Unrecognized"
print("\nStudent Details:")
print("Name:", name)
print("Class:", class_name)
print("Section:", section)
print("\nGrade:", grade)
PROGRAM 23

Multiple conditions Students result with grade Modify the earlier


program students’ grades in such a way that they should take in five
subject marks. Find the total mark and their percentage. Your program
should check for the following conditions:
If the percentage falls below 45, they are considered fail. If the
percentage is between 45 and 60, grade them as pass. If the percentage is
between 60 and 75, grade them as good. If the percentage is between 75
and 85, grade them as very good. If the percentage is between 85 and
100, grade them excellent. If the percentage is below zero or above 100,
it’s an error.

name = input("Enter student's name: ")


class_name = input("Enter student's class: ")
section = input("Enter student's section: ")
subject1 = float(input("Enter marks for subject 1: "))
subject2 = float(input("Enter marks for subject 2: "))
subject3 = float(input("Enter marks for subject 3: "))
subject4 = float(input("Enter marks for subject 4: "))
subject5 = float(input("Enter marks for subject 5: "))
total_marks = subject1 + subject2 + subject3 + subject4 + subject5
percentage = (total_marks / 500) * 100
if percentage < 0 or percentage > 100:
grade = "Error"
elif percentage < 45:
grade = "Fail"
elif percentage < 60:
grade = "Pass"
elif percentage < 75:
grade = "Good"
elif percentage < 85:
grade = "Very Good"
else:
grade = "Excellent"
print("\nStudent Details:")
print("Name:", name)
print("Class:", class_name)
print("Section:", section)
print("\nTotal Marks:", total_marks)
print("Percentage:", percentage)
print("Grade:", grade)
PROGRAM 24
To write a python program to perform Matrix Multiplication.
rows1 = int(input("Enter the number of rows for matrix 1: "))
cols1 = int(input("Enter the number of columns for matrix 1: "))
rows2 = int(input("Enter the number of rows for matrix 2: "))
cols2 = int(input("Enter the number of columns for matrix 2: "))
if cols1 != rows2:
print("Cannot perform matrix multiplication.")
else:
matrix1 = []
matrix2 = []
print("Enter the elements of matrix 1:")
for i in range(rows1):
row = []
for j in range(cols1):
element = int(input("Enter element at position ({}, {}): ".format(i, j)))
row.append(element)
matrix1.append(row)
print("Enter the elements of matrix 2:")
for i in range(rows2):
row = []
for j in range(cols2):
element = int(input("Enter element at position ({}, {}): ".format(i, j)))
row.append(element)
matrix2.append(row)
result = [[0 for _ in range(cols2)] for _ in range(rows1)]
for i in range(rows1):
for j in range(cols2):
for k in range(rows2):
result[i][j] += matrix1[i][k] * matrix2[k][j]
print("Result of matrix multiplication:")
for row in result: print(row)
PROGRAM 25

To write a python program find the square root of a number


(Newton’s method).

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


if number < 0:
print("Square root of a negative number is not defined.")
else:
guess = number / 2
while True:
new_guess = 0.5 * (guess + (number / guess))
if abs(new_guess - guess) < 0.0001: # Desired accuracy
square_root = new_guess
break
guess = new_guess

print("Square root of", number, "is", square_root)


PROGRAM 26
To write a python program find the maximum of a list of numbers.

numbers = input("Enter a list of numbers (separated by spaces): ").split()


numbers = [int(num) for num in numbers]
if len(numbers) == 0:
print("List is empty.")
else:
maximum = numbers[0]
for num in numbers:
if num > maximum:
maximum = num
print("Maximum value:", maximum)

You might also like