0% found this document useful (0 votes)
8 views5 pages

Practical_12

The document is a lab manual for Python programming that includes practical exercises and examples. It covers various topics such as mathematical operations using the math module, random number generation, date and time manipulation, and creating user-defined modules. Each section provides code snippets, expected outputs, and explanations for better understanding.
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)
8 views5 pages

Practical_12

The document is a lab manual for Python programming that includes practical exercises and examples. It covers various topics such as mathematical operations using the math module, random number generation, date and time manipulation, and creating user-defined modules. Each section provides code snippets, expected outputs, and explanations for better understanding.
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/ 5

Programming with ‘Python’ Practical: 12 Lab Manual (Solved)

Sr. Remark if
Name of Resource Broad Specification Quantity
No. any
1 Computer System Intel i3, 4 GB RAM
For All
2 Operating System Windows/Linux 20
Experiments
3 Development Software Python IDE And VS Code

1. What is the output of the following program?


import math

print(math.sqrt(25)) # Square root of 25


print(math.pi) # Value of pi
print(math.degrees(2)) # Convert 2 radians to degrees
print(math.radians(60)) # Convert 60 degrees to radians
print(math.sin(2)) # Sine of 2 radians
print(math.cos(0.5)) # Cosine of 0.5 radians
print(math.tan(0.23)) # Tangent of 0.23 radians
print(math.factorial(4)) # Factorial of 4
Output:
5.0
3.141592653589793
114.59155902616465
1.0471975511965976
0.9092974268256817
0.8775825618903728
0.2334453638559054
24
Explanation:
1. math.sqrt(25) → Square root of 25 is 5.0
2. math.pi → Returns the value of π (3.141592653589793)

Jamia Polytechnic Akkalkuwa Page No:01 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 12 Lab Manual (Solved)

3. math.degrees(2) → Converts 2 radians to degrees (114.59 approx.)


4. math.radians(60) → Converts 60 degrees to radians (1.047 approx.)
5. math.sin(2) → Sine of 2 radians (0.909 approx.)
6. math.cos(0.5) → Cosine of 0.5 radians (0.878 approx.)
7. math.tan(0.23) → Tangent of 0.23 radians (0.233 approx.)
8. math.factorial(4) → Factorial of 4 (4 × 3 × 2 × 1 = 24)

2. What is the output of the following program?


import random

print(random.randint(0, 5)) # Random integer between 0 and 5 (inclusive)


print(random.random()) # Random float between 0 and 1
print(random.random() * 100) # Random float between 0 and 100

List = [1, 4, True, 800, "Python", 27, "hello"]


print(random.choice(List)) # Randomly selects an element from the list

Example Output (Values will change on each run):


3
0.7524382923791894
57.89243190327593
Python
Explanation:
1. random.randint(0, 5)
→ Returns a random integer between 0 and 5 (inclusive).
2. random.random()
→ Returns a random float between 0.0 and 1.0.
3. random.random() * 100
→ Returns a random float between 0.0 and 100.0.
4. random.choice(List)
→ Selects a random element from the given list [1, 4, True, 800, "Python", 27, "hello"].
Since random functions generate different values each time, the output will vary on each
execution.

Jamia Polytechnic Akkalkuwa Page No:02 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 12 Lab Manual (Solved)

3. What is the output of the following program?


import datetime
from datetime import date
import time

print(time.time()) # Current timestamp in seconds


print(date.fromtimestamp(454554)) # Convert given timestamp to date

Expected Output:
1709062923.123456 # (This value will change based on the current timestamp)
1970-01-06 # (Fixed output for given timestamp)

Explanation:
1. time.time()
o Returns the current time in seconds since epoch (Jan 1, 1970, 00:00:00 UTC).
o The value changes every time the program runs.
2. date.fromtimestamp(454554)
o Converts the given timestamp 454554 (seconds since epoch) into a readable date.
o 454554 seconds from Jan 1, 1970, results in January 6, 1970 (1970-01-06).

Epoch time (Jan 1, 1970, 00:00:00 UTC) → 0 seconds


Jan 2, 1970, 00:00:00 UTC → 86,400 seconds (1 day = 86,400 sec)
Feb 1, 1970, 00:00:00 UTC → 2,678,400 seconds
Current timestamp (time.time()) → Around 1709062923 (Changes every second)

1. Write a Python program to create a user defined module that will ask your college name
and will display the name of the college.

Step 1: Create a Module (college.py)


Create a new Python file named college.py and write the following code:
def get_college_name():
name = input("Enter your college name: ")
print("Your college name is:", name)

Jamia Polytechnic Akkalkuwa Page No:03 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 12 Lab Manual (Solved)

Step 2: Import and Use the Module


Now, created another Python file (e.g., TY_Student.py) in the same directory and write:
import college
college.get_college_name()

How It Works:
1. The college.py module contains the function get_college_name(), which asks for the
college name and displays it.
2. In main.py, we import the college module and call college.get_college_name().
3. When you run main.py, it will prompt you to enter your college name, then display it.

Example Output:
Enter your college name: Jamia Polytechnic
Your college name is: Jamia Polytechnic

2. Write a Python program that will calculate area and circumference of circle using
inbuilt Math Module
The area and circumference of a circle using the inbuilt math module:
import math

# Input radius
radius = float(input("Enter the radius of the circle: "))

# Calculating area and circumference


area = math.pi * radius * radius # πr²
circumference = 2 * math.pi * radius # 2πr

# Display results
print("Area of the circle:", area)
print("Circumference of the circle:", circumference)

Example Output:
Enter the radius of the circle: 5
Area of the circle: 78.53981633974483

Jamia Polytechnic Akkalkuwa Page No:04 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 12 Lab Manual (Solved)

Circumference of the circle: 31.41592653589793

Explanation:
1. math.pi → Provides the value of π (3.141592653589793).
2. Area formula: πr²
3. Circumference formula: 2πr

3.Write a Python program that will display Calendar of given month using Calendar
Module

import calendar
# Input year and month
year = int(input("Enter the year: "))
month = int(input("Enter the month (1-12): "))

# Display calendar
print(calendar.month(year, month))

Example Output:
Enter the year: 2025
Enter the month (1-12): 2
February 2025
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28

Explanation:
1. calendar.month(year, month) → Displays the calendar of the given month.
2. The user enters the year and month, and the program prints the calendar.

Jamia Polytechnic Akkalkuwa Page No:05 Prepared by: Sayyed Waliullah

You might also like