Python Practicals
Subject: Introduction to Python
Name: Aditya Kumar Misra
Roll No.: 1225302
Class: [Link] CSE (3rd Semester)
Introduction to basic of python using shell
Practical: Arithmetic Operations
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("Power:", a ** b)
Practical: Greatest Number
a = 15
b = 25
if a > b:
print("Greatest:", a)
else:
print("Greatest:", b)
Practical: Even/Odd Number
num = 7
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
Practical: Percentage of a Number
marks = 450
total = 500
percentage = (marks / total) * 100
print("Percentage:", percentage, "%")
Practical: Factorial of a Number
num = 5
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial of", num, "is", fact)
Practical: GCD of Two Numbers
a = 100
b = 75
a_factor = []
b_factor = []
GCD = []
for i in range(1, a + 1):
if a % i == 0:
a_factor.append(i)
for i in range(1, b + 1):
if b % i == 0:
b_factor.append(i)
ptr_a = 0
ptr_b = 0
while ptr_a < len(a_factor) and ptr_b < len(b_factor):
if a_factor[ptr_a] == b_factor[ptr_b]:
[Link](a_factor[ptr_a])
ptr_a += 1
ptr_b += 1
elif a_factor[ptr_a] < b_factor[ptr_b]:
ptr_a += 1
else:
ptr_b += 1
print(GCD[-1])
Practical: Square Root of a Number
num = 25
sqrt = num ** (1/2)
print("Square root:", sqrt)
Practical: Table of a Number
num = 7
for i in range(1, 11):
print(num, "x", i, "=", num * i)
Practical: Area of Shapes
# Area of Circle
r=5
area_circle = 3.14 * r * r
print("Area of Circle:", area_circle)
# Area of Rectangle
l = 10
w=4
area_rect = l * w
print("Area of Rectangle:", area_rect)
# Area of Triangle
b=6
h=8
area_tri = 0.5 * b * h
print("Area of Triangle:", area_tri)
Practical: Find Grade of a Student using if-else
marks = 85
if marks >= 90:
grade = "A"
elif marks >= 75:
grade = "B"
elif marks >= 60:
grade = "C"
elif marks >= 40:
grade = "D"
else:
grade = "F"
print("Grade:", grade)
Practical: Reverse a Number
number = 1234
string = str(number)
array = list(string)
[Link]()
reversed_string = "".join(array)
reversed_number = int(reversed_string)
print(reversed_number)