(Grade X) Chapter 4- Programming in Python - Part 3- Concept of Library and Package





1.     Write a program to find area and volume of a cylinder. (area=2πrh, volume=πr2h)

a. Import entire module

import math

r = float(input("Enter radius: "))

h = float(input("Enter height: "))

area = 2 * math.pi * r * h

volume = math.pi * r * r * h

print("Area of Cylinder =", area)

print("Volume of Cylinder =", volume)

 

b. Import a specific function/constant

from math import pi

r = float(input("Enter radius: "))

h = float(input("Enter height: "))

area = 2 * pi * r * h

volume = pi * r * r * h

print("Area of Cylinder =", area)

print("Volume of Cylinder =", volume)

 

c. Import with an alias (shortcut)

import math as m

r = float(input("Enter radius: "))

h = float(input("Enter height: "))

area = 2 * m.pi * r * h

volume = m.pi * r * r * h

print("Area of Cylinder =", area)

print("Volume of Cylinder =", volume)

 

d. Import all functions/constants

from math import *

r = float(input("Enter radius: "))

h = float(input("Enter height: "))

area = 2 * pi * r * h

volume = pi * r * r * h

print("Area of Cylinder =", area)

print("Volume of Cylinder =", volume)

 

Note for students:

import math → Use math.pi

from math import pi → Use pi directly

import math as m → Use m.pi

from math import * → All functions/constants can be used directly (e.g., pi, sqrt(), sin()).

 

2.     Write a program to find HCF of two given numbers.

Formula/Function Used

  • gcd(a, b) returns the Greatest Common Divisor (GCD), which is as the Highest Common Factor (HCF).

 

a. Import entire module

import math

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

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

hcf = math.gcd(num1, num2)

print("HCF =", hcf)

 

b. Import a specific function

from math import gcd

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

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

hcf = gcd(num1, num2)

print("HCF =", hcf)

 

c. Import with an alias (shortcut)

import math as m

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

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

hcf = m.gcd(num1, num2)

print("HCF =", hcf)

 

d. Import all functions

from math import *

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

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

hcf = gcd(num1, num2)

print("HCF =", hcf)

 

3.     Write a program to find absolute value of a given numbers.

Note: The math module provides fabs() to return the absolute value as a floating-point number.

 

a. Import entire module

import math

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

result = math.fabs(num)

print("Absolute value =", result)

 

b. Import a specific function

from math import fabs

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

result = fabs(num)

print("Absolute value =", result)

 

c. Import with an alias (shortcut)

import math as m

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

result = m.fabs(num)

print("Absolute value =", result)

 

d. Import all functions

from math import *

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

result = fabs(num)

print("Absolute value =", result)

 

4.     Write a program to display the value of sin, cos and tan of a given nember.

Note: Python's sin(), cos(), and tan() functions take the angle in radians, so we first convert degrees to radians using radians().

 

a. Import entire module

import math

angle = float(input("Enter angle in degrees: "))

r = math.radians(angle)

print("Sin =", math.sin(r))

print("Cos =", math.cos(r))

print("Tan =", math.tan(r))

 

b. Import specific functions

from math import sin, cos, tan, radians

angle = float(input("Enter angle in degrees: "))

r = radians(angle)

print("Sin =", sin(r))

print("Cos =", cos(r))

print("Tan =", tan(r))

 

c. Import with an alias (shortcut)

import math as m

angle = float(input("Enter angle in degrees: "))

r = m.radians(angle)

print("Sin =", m.sin(r))

print("Cos =", m.cos(r))

print("Tan =", m.tan(r))

 

d. Import all functions

from math import *

angle = float(input("Enter angle in degrees: "))

r = radians(angle)

print("Sin =", sin(r))

print("Cos =", cos(r))

print("Tan =", tan(r))

 

5.     Use math library to calculate the square root of a given number.

Note:

sqrt() is a function in the math module used to find the square root of a number.

The input number should be 0 or positive. A negative number will produce an error (ValueError: math domain error).

 

a. Import entire module

import math

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

result = math.sqrt(num)

print("Square root =", result)

 

b. Import a specific function

from math import sqrt

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

result = sqrt(num)

print("Square root =", result)

 

c. Import with an alias (shortcut)

import math as m

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

result = m.sqrt(num)

print("Square root =", result)

 

d. Import all functions

from math import *

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

result = sqrt(num)

print("Square root =", result)

 

 

6.     Use math library to calculate factorial of a given number.

 

Note for Students

  • factorial() is a function in the math module that calculates the factorial of a non-negative integer.
  • Factorial of a number is the product of all positive integers from 1 to that number.
  • Example:
    • 5! = 5 × 4 × 3 × 2 × 1 = 120
    • 0! = 1

 

a. Import entire module

import math

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

result = math.factorial(num)

print("Factorial =", result)

 

b. Import a specific function

from math import factorial

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

result = factorial(num)

print("Factorial =", result)

 

c. Import with an alias (shortcut)

import math as m

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

result = m.factorial(num)

print("Factorial =", result)

 

d. Import all functions

from math import *

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

result = factorial(num)

print("Factorial =", result)

 

7.     Import only pie and cos function from the math library and calculate cosine of  60 degree.

from math import pi, cos

angle = 60

# Convert degree to radian

radian = angle * pi / 180

result = cos(radian)

print("Cos 60° =", result)

 

8.     Use math.pow() to calculate power of a given number.

a. Import entire module

import math

base = float(input("Enter base: "))

power = float(input("Enter power: "))

result = math.pow(base, power)

print("Result =", result)

 

b. Import a specific function

from math import pow

base = float(input("Enter base: "))

power = float(input("Enter power: "))

result = pow(base, power)

print("Result =", result)

 

c. Import with an alias (shortcut)

import math as m

base = float(input("Enter base: "))

power = float(input("Enter power: "))

result = m.pow(base, power)

print("Result =", result)

 

d. Import all functions

from math import *

base = float(input("Enter base: "))

power = float(input("Enter power: "))

result = pow(base, power)

print("Result =", result)

 

6. Use the random library to shuffle a list of 10 numbers and print the result.

import random

numbers = [1,2,3,4,5,6,7,8,9,10]

random.shuffle(numbers)

print("Shuffled List:", numbers)

.

7. Write a program to guess a random number generated by the computer (1–20).

import random

number = random.randint(1, 20)

guess = int(input("Guess a number between 1 and 20: "))

if guess == number:

    print("Congratulations! You guessed correctly.")

else:

    print("Wrong Guess!")

    print("The correct number is", number)

 

8. Generate 5 random numbers between 1 and 50 and store them in a list.

import random

numbers = []

for i in range(5):

    numbers.append(random.randint(1, 50))

print("Random Numbers:", numbers)

 

9. Draw a square using Turtle.

import turtle

t = turtle.Turtle()

for i in range(4):

    t.forward(100)

    t.right(90)

turtle.done()

 

10. Draw an equilateral triangle using Turtle and fill it with a color.

import turtle

t = turtle.Turtle()

t.fillcolor("green")

t.begin_fill()

for i in range(3):

    t.forward(100)

    t.left(120)

t.end_fill()

turtle.done()

 

11. Create a Turtle object, change its shape to "turtle", and move it forward by 100 units.

import turtle

t = turtle.Turtle()

t.shape("turtle")

t.forward(100)

turtle.done()

 

12. Change the pen color to red and fill the shape with blue using Turtle.

import turtle

t = turtle.Turtle()

t.pencolor("red")

t.fillcolor("blue")

t.begin_fill()

for i in range(4):

    t.forward(100)

    t.right(90)

t.end_fill()

turtle.done()

 

13. Draw a star using forward() and left() commands.

import turtle

t = turtle.Turtle()

for i in range(5):

    t.forward(150)

    t.left(144)

turtle.done(


14. To divide two numbers given by the user and handle the division by zero error using try-except.

    try:

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

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

 

    result = a / b

    print("Result =", result)

 

except ZeroDivisionError:

    print("Error: Cannot divide by zero.")

 

15. To create a list of numbers and try to access an index that doesn't exist. Handle the Index Error using try-except.

numbers = [10, 20, 30, 40, 50]

try:

    print("Number =", numbers[10])

except IndexError:

    print("Error: The given index does not exist.")