✦ VidyaVerseOpen interactive version

← Python Lab Practical Record

EXPERIMENT No. 4

Python Lab Practical Record · Python programming laboratory

Read with diagrams, search and self-test

4A. Function to calculate the area of a circle

Aim

To write a Python program that defines a function to calculate the area of a circle.

Program (function_area_circle.py)

# Exp 4A: Function to calculate the area of a circle
def area(r):
    a = 3.14 * r * r
    print("Area =", a)
radius = float(input("Enter the radius: "))
area(radius)

Output

Enter the radius: 5
Area = 78.5

Result

The area of the circle was calculated using a user-defined function.

4B. GCD of two numbers using a function

Aim

To write a Python program to find the greatest common divisor (GCD) of two numbers using a function.

Program (gcd_function.py)

# Exp 4B: GCD of two numbers using a function
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
print("GCD =", gcd(x, y))

Output

Enter first number: 12
Enter second number: 18
GCD = 6

Result

The GCD of the two numbers was found using a function (Euclid's method).

4C. Lambda function for squaring a number

Aim

To explore lambda functions by creating small one-line functions, such as squaring a number.

Program (lambda_square.py)

# Exp 4C: Lambda function for squaring a number
square = lambda x: x * x
print(square(5))

Output

25

Result

A lambda (one-line) function was used to find the square of a number.

4D. Using the math module for trigonometric calculations

Aim

To write a Python program to use the math module for trigonometric calculations.

Formula / Note

The math functions take angles in radians; for degrees use math.sin(math.radians(angle)).

Program (math_module.py)

# Exp 4D: Using the math module for trigonometric calculations
import math
x = math.sin(0)
y = math.cos(0)
print("sin value =", x)
print("cos value =", y)

Output

sin value = 0.0
cos value = 1.0

Result

Trigonometric values were calculated using the math module.