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

python_practical_file

The document contains 15 Python programs, each with a specific objective such as performing arithmetic operations, calculating squares and cubes, greeting users, checking triangle validity, evaluating student grades, and more. Each program includes source code, a description of its functionality, and sample output demonstrating its use. The programs cover a range of topics suitable for beginners to practice basic programming concepts.

Uploaded by

abhinav1081992
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)
10 views15 pages

python_practical_file

The document contains 15 Python programs, each with a specific objective such as performing arithmetic operations, calculating squares and cubes, greeting users, checking triangle validity, evaluating student grades, and more. Each program includes source code, a description of its functionality, and sample output demonstrating its use. The programs cover a range of topics suitable for beginners to practice basic programming concepts.

Uploaded by

abhinav1081992
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

Program 1: Basic Arithmetic Operations

Objective: Write a Python program that takes two numbers as input from the user and performs basic
arithmetic operations: Addition, Subtraction, Multiplication, Division, and Modulus.

SOURCE CODE

# Program to perform basic arithmetic operations

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


num2 = float(input("Enter second number: "))

# Performing calculations
addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2 if num2 != 0 else "Undefined (Division by zero)"
modulus = num1 % num2 if num2 != 0 else "Undefined"

# Displaying results
print("
--- RESULTS ---")
print(f"Addition: {num1} + {num2} = {addition}")
print(f"Subtraction: {num1} - {num2} = {subtraction}")
print(f"Multiplication: {num1} * {num2} = {multiplication}")
print(f"Division: {num1} / {num2} = {division}")
print(f"Modulus: {num1} % {num2} = {modulus}")

SAMPLE OUTPUT

Enter first number: 25


Enter second number: 4

--- RESULTS ---


Addition: 25.0 + 4.0 = 29.0
Subtraction: 25.0 - 4.0 = 21.0
Multiplication: 25.0 * 4.0 = 100.0
Division: 25.0 / 4.0 = 6.25
Modulus: 25.0 % 4.0 = 1.0

Page 1
Program 2: Square and Cube Calculation

Objective: Write a Python program to calculate and display the square and cube of a given number.

SOURCE CODE

# Program to calculate square and cube of a number

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

# Calculating square and cube


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

# Displaying results
print(f"
Number: {number}")
print(f"Square of {number}: {square}")
print(f"Cube of {number}: {cube}")

SAMPLE OUTPUT

Enter a number: 7

Number: 7.0
Square of 7.0: 49.0
Cube of 7.0: 343.0

Page 2
Program 3: User Greeting and String Formatting

Objective: Write a Python program that accepts a user's name, class, and roll number, then displays a
personalized greeting and card format using string formatting techniques.

SOURCE CODE

# Program for personalized greeting and string formatting

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


student_class = input("Enter class and section: ")
roll_no = input("Enter roll number: ")

# Formatted Output
print("
" + "=" * 35)
print(" STUDENT PROFILE CARD ")
print("=" * 35)
print(f" Name : {[Link]()}")
print(f" Class : {student_class.upper()}")
print(f" Roll No : {roll_no}")
print("=" * 35)
print(f"Welcome, {[Link]()}! Wish you a great academic year!")

SAMPLE OUTPUT

Enter student's name: Kavya Yadav


Enter class and section: 10th-A
Enter roll number: 18

===================================
STUDENT PROFILE CARD
===================================
Name : Kavya Yadav
Class : 10TH-A
Roll No : 18
===================================
Welcome, Kavya Yadav! Wish you a great academic year!

Page 3
Program 4: Triangle Validity Check

Objective: Write a Python program to input three angles of a triangle and check whether the triangle is valid or
not (the sum of angles must equal 180°).

SOURCE CODE

# Program to check validity of a triangle based on interior angles

angle1 = float(input("Enter first angle: "))


angle2 = float(input("Enter second angle: "))
angle3 = float(input("Enter third angle: "))

# Checking validity condition


total_angle_sum = angle1 + angle2 + angle3

if angle1 > 0 and angle2 > 0 and angle3 > 0 and total_angle_sum == 180:
print("
Result: Valid Triangle! Sum of angles is exactly 180 degrees.")
else:
print("
Result: Invalid Triangle! Angles must be positive and sum to 180 degrees.")

SAMPLE OUTPUT

Enter first angle: 60


Enter second angle: 50
Enter third angle: 70

Result: Valid Triangle! Sum of angles is exactly 180 degrees.

Page 4
Program 5: Student Grade Evaluation

Objective: Write a Python program to calculate total marks, percentage, and award grades based on
percentage scored across five subjects.

SOURCE CODE

# Program for student grade evaluation

m1 = float(input("Enter marks for Subject 1: "))


m2 = float(input("Enter marks for Subject 2: "))
m3 = float(input("Enter marks for Subject 3: "))
m4 = float(input("Enter marks for Subject 4: "))
m5 = float(input("Enter marks for Subject 5: "))

total = m1 + m2 + m3 + m4 + m5
percentage = (total / 500) * 100

# Grade Determination
if percentage >= 90:
grade = "A+"
elif percentage >= 80:
grade = "A"
elif percentage >= 70:
grade = "B"
elif percentage >= 60:
grade = "C"
elif percentage >= 50:
grade = "D"
else:
grade = "F (Fail)"

print(f"
Total Marks Obtained: {total} / 500")
print(f"Percentage: {percentage:.2f}%")
print(f"Grade Assigned: {grade}")

SAMPLE OUTPUT

Enter marks for Subject 1: 92


Enter marks for Subject 2: 88
Enter marks for Subject 3: 95
Enter marks for Subject 4: 84
Enter marks for Subject 5: 91

Total Marks Obtained: 450.0 / 500


Percentage: 90.00%
Grade Assigned: A+

Page 5
Program 6: Factorial Calculation

Objective: Write a Python program to calculate the factorial of a given positive integer using a loop.

SOURCE CODE

# Program to calculate factorial of a number

num = int(input("Enter a non-negative integer: "))

if num < 0:
print("Factorial does not exist for negative numbers.")
elif num == 0 or num == 1:
print(f"The factorial of {num} is 1")
else:
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}")

SAMPLE OUTPUT

Enter a non-negative integer: 6

The factorial of 6 is 720

Page 6
Program 7: Reverse a Number

Objective: Write a Python program to take an integer from the user and display its reverse using a while loop.

SOURCE CODE

# Program to reverse a given integer

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


original_num = num
reversed_num = 0

while num > 0:


remainder = num % 10
reversed_num = (reversed_num * 10) + remainder
num = num // 10

print(f"
Original Number: {original_num}")
print(f"Reversed Number: {reversed_num}")

SAMPLE OUTPUT

Enter an integer to reverse: 849201

Original Number: 849201


Reversed Number: 102948

Page 7
Program 8: Sum of First N Natural Numbers

Objective: Write a Python program to find the sum of first N natural numbers using a loop.

SOURCE CODE

# Program to find sum of first N natural numbers

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

if n <= 0:
print("Please enter a positive integer.")
else:
sum_n = 0
for i in range(1, n + 1):
sum_n += i

print(f"
The sum of the first {n} natural numbers is: {sum_n}")

SAMPLE OUTPUT

Enter the value of N: 50

The sum of the first 50 natural numbers is: 1275

Page 8
Program 9: 6-Digit Number Operations

Objective: Write a Python program to take a 6-digit integer as input and extract/sum its individual digits.

SOURCE CODE

# Program to extract and sum digits of a 6-digit number

num_str = input("Enter a 6-digit positive integer: ")

if len(num_str) == 6 and num_str.isdigit():


num = int(num_str)
temp = num
digit_sum = 0
digits = []

while temp > 0:


d = temp % 10
[Link](d)
digit_sum += d
temp //= 10

[Link]()
print(f"Digits extracted: {digits}")
print(f"Sum of individual digits: {digit_sum}")
else:
print("Invalid input! Please enter exactly 6 digits.")

SAMPLE OUTPUT

Enter a 6-digit positive integer: 458129

Digits extracted: [4, 5, 8, 1, 2, 9]


Sum of individual digits: 29

Page 9
Program 10: Geometric Calculations

Objective: Write a Python program to calculate area and perimeter/circumference of shapes (Circle and
Rectangle).

SOURCE CODE

# Program to calculate Area and Perimeter of Circle and Rectangle

import math

# Rectangle
length = float(input("Enter length of rectangle: "))
width = float(input("Enter width of rectangle: "))
rect_area = length * width
rect_perimeter = 2 * (length + width)

# Circle
radius = float(input("
Enter radius of circle: "))
circle_area = [Link] * (radius ** 2)
circle_circumference = 2 * [Link] * radius

print("
--- RESULTS ---")
print(f"Rectangle Area: {rect_area:.2f}, Perimeter: {rect_perimeter:.2f}")
print(f"Circle Area: {circle_area:.2f}, Circumference: {circle_circumference:.2f}")

SAMPLE OUTPUT

Enter length of rectangle: 10


Enter width of rectangle: 5

Enter radius of circle: 7

--- RESULTS ---


Rectangle Area: 50.00, Perimeter: 30.00
Circle Area: 153.94, Circumference: 43.98

Page 10
Program 11: Count Digits in an Integer

Objective: Write a Python program to count the total number of digits in an integer.

SOURCE CODE

# Program to count total number of digits in an integer

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


count = 0
temp = abs(num)

if temp == 0:
count = 1
else:
while temp > 0:
count += 1
temp //= 10

print(f"Total number of digits in {num} is: {count}")

SAMPLE OUTPUT

Enter an integer: 9876543

Total number of digits in 9876543 is: 7

Page 11
Program 12: Marriage Eligibility Check

Objective: Write a Python program to check whether a person is eligible for marriage based on gender and
age limits (Male: 21, Female: 18).

SOURCE CODE

# Program to check marriage eligibility

gender = input("Enter gender (M/F): ").strip().upper()


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

if gender == 'M':
if age >= 21:
print("Status: Eligible for marriage.")
else:
print("Status: Not eligible. Minimum legal age for males is 21.")
elif gender == 'F':
if age >= 18:
print("Status: Eligible for marriage.")
else:
print("Status: Not eligible. Minimum legal age for females is 18.")
else:
print("Invalid gender input! Enter 'M' or 'F'.")

SAMPLE OUTPUT

Enter gender (M/F): M


Enter age: 22

Status: Eligible for marriage.

Page 12
Program 13: Count Vowels and Consonants

Objective: Write a Python program to count the total number of vowels and consonants in a given string.

SOURCE CODE

# Program to count vowels and consonants in a string

text = input("Enter a string: ")


vowels = "aeiouAEIOU"

vowel_count = 0
consonant_count = 0

for char in text:


if [Link]():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1

print(f"
String: '{text}'")
print(f"Number of Vowels: {vowel_count}")
print(f"Number of Consonants: {consonant_count}")

SAMPLE OUTPUT

Enter a string: Computer Science Practical

String: 'Computer Science Practical'


Number of Vowels: 9
Number of Consonants: 15

Page 13
Program 14: Find LCM of Two Numbers

Objective: Write a Python program to find the Least Common Multiple (LCM) of two user-input numbers.

SOURCE CODE

# Program to find LCM of two numbers

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


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

# Choose the greater number


greater = max(num1, num2)

while True:
if (greater % num1 == 0) and (greater % num2 == 0):
lcm = greater
break
greater += 1

print(f"The LCM of {num1} and {num2} is: {lcm}")

SAMPLE OUTPUT

Enter first number: 12


Enter second number: 18

The LCM of 12 and 18 is: 36

Page 14
Program 15: Swapping Two Variables

Objective: Write a Python program to swap two variables with and without using a third temporary variable.

SOURCE CODE

# Program to swap two variables

a = input("Enter value of A: ")


b = input("Enter value of B: ")

print(f"
Before Swapping: A = {a}, B = {b}")

# Method 1: Using temporary variable


temp = a
a_temp = b
b_temp = temp
print(f"Method 1 (Temp Variable) -> A = {a_temp}, B = {b_temp}")

# Method 2: Pythonic tuple unpacking


a, b = b, a
print(f"Method 2 (Tuple Unpacking) -> A = {a}, B = {b}")

SAMPLE OUTPUT

Enter value of A: 15
Enter value of B: 42

Before Swapping: A = 15, B = 42


Method 1 (Temp Variable) -> A = 42, B = 15
Method 2 (Tuple Unpacking) -> A = 42, B = 15

Page 15

You might also like