0% found this document useful (0 votes)
2 views14 pages

Python Assignments

The document is a Python programming lab journal containing a collection of foundational Python programs, including source code and sample outputs for various tasks. It covers topics such as calculating surface area and volume of a cuboid, average marks, swapping numbers, checking for palindromes, and more. Each program is accompanied by user input prompts and corresponding outputs to demonstrate functionality.
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)
2 views14 pages

Python Assignments

The document is a Python programming lab journal containing a collection of foundational Python programs, including source code and sample outputs for various tasks. It covers topics such as calculating surface area and volume of a cuboid, average marks, swapping numbers, checking for palindromes, and more. Each program is accompanied by user input prompts and corresponding outputs to demonstrate functionality.
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

PYTHON PROGRAMMING LAB JOURNAL

A Comprehensive Collection of Foundational Python Programs with Executed


Source Code and Sample Outputs

Q1. Write a python program to calculate surface area and volume of a


cuboid and the value must be entered by user.

SOURCE CODE

# Program to calculate Surface Area and Volume of a Cuboid


# Taking dimensions from the user
length = float(input("Enter the length of the cuboid: "))
width = float(input("Enter the width of the cuboid: "))
height = float(input("Enter the height of the cuboid: "))

# Calculating surface area and volume


surface_area = 2 * (length * width + width * height + height * length)
volume = length * width * height

# Displaying the results


print(f"Total Surface Area of the Cuboid: {surface_area:.2f}")
print(f"Volume of the Cuboid: {volume:.2f}")

SAMPLE OUTPUT

Enter the length of the cuboid: 5.5


Enter the width of the cuboid: 3.2
Enter the height of the cuboid: 4.0
Total Surface Area of the Cuboid: 104.80
Volume of the Cuboid: 70.40

Python Practical Assignment Page 1


Q2. Write a python program to calculate average marks for a subject.

SOURCE CODE

# Program to calculate average marks of a subject across multiple students


# Asking the user for the total number of students
num_students = int(input("Enter the number of students: "))

total_marks = 0
for i in range(1, num_students + 1):
marks = float(input(f"Enter marks for student {i}: "))
total_marks += marks

# Calculating the average


average = total_marks / num_students

# Displaying the average marks


print(f"The average marks for the subject is: {average:.2f}")

SAMPLE OUTPUT

Enter the number of students: 4


Enter marks for student 1: 85
Enter marks for student 2: 78
Enter marks for student 3: 92
Enter marks for student 4: 69
The average marks for the subject is: 81.00

Python Practical Assignment Page 2


Q3. Write a python code to accept marks in 5 subjects and display
average marks.

SOURCE CODE

# Program to accept marks in 5 subjects and compute average


marks = []
for i in range(1, 6):
sub_marks = float(input(f"Enter marks for Subject {i}: "))
[Link](sub_marks)

# Calculating total and average


total_marks = sum(marks)
average_marks = total_marks / 5

# Displaying result
print(f"Total Marks Scored: {total_marks} / 500")
print(f"Average Marks: {average_marks:.2f}")

SAMPLE OUTPUT

Enter marks for Subject 1: 75


Enter marks for Subject 2: 88
Enter marks for Subject 3: 94
Enter marks for Subject 4: 67
Enter marks for Subject 5: 82
Total Marks Scored: 406.0 / 500
Average Marks: 81.20

Python Practical Assignment Page 3


Q4. Write a python program to input two numbers and swap the two
numbers without using the third number.

SOURCE CODE

# Program to swap two numbers without a temporary/third variable


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

print(f"Before swapping: num1 = {num1}, num2 = {num2}")

# Method: Arithmetic approach (Can also use Python's comma assignment:


num1, num2 = num2, num1)
num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2

print(f"After swapping: num1 = {num1}, num2 = {num2}")

SAMPLE OUTPUT

Enter first number (num1): 15


Enter second number (num2): 42
Before swapping: num1 = 15.0, num2 = 42.0
After swapping: num1 = 42.0, num2 = 15.0

Python Practical Assignment Page 4


Q5. Write a python code to check whether the given number is
palindrome or not.

SOURCE CODE

# Program to check if a number is a palindrome


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

temp = num
reverse_num = 0

while temp > 0:


remainder = temp % 10
reverse_num = (reverse_num * 10) + remainder
temp = temp // 10

if num == reverse_num:
print(f"{num} is a palindrome number.")
else:
print(f"{num} is not a palindrome number.")

SAMPLE OUTPUT

Enter a number: 12321


12321 is a palindrome number.

Python Practical Assignment Page 5


Q6. Write a python program to input a number and display whether
it’s a prime or not.

SOURCE CODE

# Program to check if a number is prime


num = int(input("Enter a positive integer: "))

is_prime = True

if num <= 1:
is_prime = False
else:
# Checking factor loop up to square root of num
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
is_prime = False
break

if is_prime:
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")

SAMPLE OUTPUT

Enter a positive integer: 29


29 is a prime number.

Python Practical Assignment Page 6


Q7. Write a python program to add the elements of the two lists.

SOURCE CODE

# Program to add elements of two lists element-wise


list1 = [10, 20, 30, 40, 50]
list2 = [1, 2, 3, 4, 5]

print("List 1:", list1)


print("List 2:", list2)

# Using list comprehension and zip to add corresponding elements


sum_list = [x + y for x, y in zip(list1, list2)]

print("Element-wise Summed List:", sum_list)

SAMPLE OUTPUT

List 1: [10, 20, 30, 40, 50]


List 2: [1, 2, 3, 4, 5]
Element-wise Summed List: [11, 22, 33, 44, 55]

Python Practical Assignment Page 7


Q8. Write a python code to input the temperature in Celsius and
convert it into Fahrenheit using the formula (F=C*9/5+32).

SOURCE CODE

# Program to convert Celsius to Fahrenheit


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

# Applying formula
fahrenheit = (celsius * 9 / 5) + 32

# Displaying the output


print(f"{celsius}°C is equal to {fahrenheit:.2f}°F")

SAMPLE OUTPUT

Enter temperature in Celsius: 37


37.0°C is equal to 98.60°F

Python Practical Assignment Page 8


Q9. Write python code to input two numbers and display the LCM of
the two numbers.

SOURCE CODE

# Program to find LCM of two numbers


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

# Finding the greater number to start searching from


if num1 > num2:
greater = num1
else:
greater = num2

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

print(f"The L.C.M. of {num1} and {num2} is: {lcm}")

SAMPLE OUTPUT

Enter first number: 12


Enter second number: 18
The L.C.M. of 12 and 18 is: 36

Python Practical Assignment Page 9


Q10. Write a python program to find the sum of digits.

SOURCE CODE

# Program to compute sum of digits of a number


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

temp = abs(num) # Handling negative inputs cleanly


digit_sum = 0

while temp > 0:


digit = temp % 10
digit_sum += digit
temp = temp // 10

print(f"The sum of the digits of {num} is: {digit_sum}")

SAMPLE OUTPUT

Enter an integer: 4568


The sum of the digits of 4568 is: 23

Python Practical Assignment Page 10


Q11. Write a python program to find the result of 22/7 * 5*5 i.e. the
area of circle having a radius of 5.

SOURCE CODE

# Program to compute area of a circle with fixed radius 5


radius = 5

# Formula: area = (22/7) * r * r


area = (22 / 7) * radius * radius

# Displaying results
print(f"Radius of the circle: {radius}")
print(f"Calculated Area of the Circle: {area}")

SAMPLE OUTPUT

Radius of the circle: 5


Calculated Area of the Circle: 78.57142857142857

Python Practical Assignment Page 11


Q12. Write a python program to print Fibonacci series first 10
elements like 0 1 1 2 3 5 8.

SOURCE CODE

# Program to print the first 10 elements of Fibonacci series


n_terms = 10

n1, n2 = 0, 1
count = 0

print("Fibonacci series first 10 elements:")


while count < n_terms:
print(n1, end=" ")
nth = n1 + n2
# Updating values
n1 = n2
n2 = nth
count += 1
print() # Newline

SAMPLE OUTPUT

Fibonacci series first 10 elements:


0 1 1 2 3 5 8 13 21 34

Python Practical Assignment Page 12


Q13. Write a python program to display the string in the reverse
order.

SOURCE CODE

# Program to reverse a user-entered string


input_string = input("Enter a string to reverse: ")

# Using slicing syntax [start:stop:step] with a step of -1


reversed_string = input_string[::-1]

print(f"Original String: {input_string}")


print(f"Reversed String: {reversed_string}")

SAMPLE OUTPUT

Enter a string to reverse: Python Programming


Original String: Python Programming
Reversed String: gnimmargorP nohtyP

Python Practical Assignment Page 13


Q14. Write a python program to calculate Simple Interest if
principal=2000, rate=4.5 & time=10.

SOURCE CODE

# Program to calculate Simple Interest with static constants


principal = 2000
rate = 4.5
time = 10

# Formula: SI = (P * R * T) / 100
simple_interest = (principal * rate * time) / 100
total_amount = principal + simple_interest

# Displaying details
print(f"Principal Amount: {principal}")
print(f"Annual Interest Rate: {rate}%")
print(f"Time Period: {time} years")
print(f"Calculated Simple Interest: {simple_interest}")
print(f"Total Maturity Value: {total_amount}")

SAMPLE OUTPUT

Principal Amount: 2000


Annual Interest Rate: 4.5%
Time Period: 10 years
Calculated Simple Interest: 900.0
Total Maturity Value: 2900.0

Python Practical Assignment Page 14

You might also like