ATMA RAM SANATAN DHARMA COLLEGE
(UNIVERSITY OF DELHI) DHAULA KUAN, NEW DELHI – 110021
Web: www.arsdcollege.ac.in
ACCREDITED GRADE ‘A+ +’ NACC ALL INDIA 6th RANK NIRF [MHRD]
PRACTIAL
Name: Aman Kumar
Course: BA Hindi (Honours)
Roll No.: 23/31022
Section: A
SUBJECT: Essential of Python [SEC]
Submitted to: Nisha Mam
Grade: _________
Date: ___/___/2023 signature
PRACTIAL FOR PYTHON
1. Write a python program to calculate area of following shapes:
a. Calculate the area of circle.
import math
def calculate_circle_area(r):
area= math.pi*r*2
return area
r= float(input("enter r of circle"))
result = calculate_circle_area (r)
print(f"area of the circle with r {r}is:{result}")
Output:
enter r of circle
8
area of the circle with r 8.0is:50.26548245743669
b. Calculate the area of square.
import math
def calculate_square_area(a):
area = a**2
return area
a= float(input("enter side of square:"))
result= calculate_square_area(a)
print(f"area of square with a {a}is: {result}")
Output:
enter side of square:
8
area of square with a 8.0is: 64.0
c. Calculate the area of rectangle:
def printarea(width=1, height=2):
area= width*height
print("width:", width, "\theight:", height,"\tarea:",area)
printarea()
printarea(4,2.5)
printarea(height=5, width=3)
printarea(width=1, height=4)
printarea(height=6.2)
Output:
width: 1 height: 2 area: 2
width: 4 height: 2.5 area: 10.0
width: 3 height: 5 area: 15
width: 1 height: 4 area: 4
width: 1 height: 6.2 area: 6.2
2. Write a python to print Fibonacci:
def fibonacci(n):
fib_series = [0, 1]
for i in range(2, n):
next_term = fib_series[-1] + fib_series[-2]
fib_series.append(next_term)
return fib_series
# Example: Generate the first 10 terms of the Fibonacci series
n = 10
result = fibonacci(n)
print(f"The first {n} terms of the Fibonacci series are: {result}")
Output:
The first 10 terms of the Fibonacci series are: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
3. Write a python to find HCF and LCM if 2 no.:
def calculate_hcf(x, y):
while y:
x, y = y, x % y
return x
def calculate_lcm(x, y):
return abs(x * y) // calculate_hcf(x, y)
# Example: Calculate HCF and LCM of two numbers
num1 = int(input("enter the first number:"))
num2 = int(input("enter the second number:"))
hcf_result = calculate_hcf(num1, num2)
lcm_result = calculate_lcm(num1, num2)
print(f"The HCF of {num1} and {num2} is: {hcf_result}")
print(f"The LCM of {num1} and {num2} is: {lcm_result}")
Output:
enter the first number: 8
enter the second number: 12
The HCF of 8 and 12 is: 4
The LCM of 8 and 12 is: 24
4. Write a python to illustrate various functions of maths module and statistics module.
import math
import statistics
# Math Module
# Basic math operations
print("Basic Math Operations:")
print("Square root of 25:", math.sqrt(25))
print("Power of 2^3:", math.pow(2, 3))
print("Absolute value of -10:", math.fabs(-10))
print("Ceiling of 4.3:", math.ceil(4.3))
print("Floor of 4.8:", math.floor(4.8))
print("Factorial of 5:", math.factorial(5))
# Trigonometric functions
print("\nTrigonometric Functions:")
print("Sine of 30 degrees:", math.sin(math.radians(30)))
print("Cosine of 60 degrees:", math.cos(math.radians(60)))
print("Tangent of 45 degrees:", math.tan(math.radians(45)))
# Logarithmic functions
print("\nLogarithmic Functions:")
print("Natural logarithm (base e) of 10:", math.log(10))
print("Logarithm of 1000 to base 10:", math.log10(1000))
# Statistics Module
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Measures of central tendency
print("\nMeasures of Central Tendency:")
print("Mean:", statistics.mean(data))
print("Median:", statistics.median(data))
print("Mode:", statistics.mode(data))
# Measures of spread
print("\nMeasures of Spread:")
print("Variance:", statistics.variance(data))
print("Standard deviation:", statistics.stdev(data))
# Other statistical functions
print("\nOther Statistical Functions:")
print("Harmonic mean:", statistics.harmonic_mean(data))
print("Geometric mean:", statistics.geometric_mean(data))
Output:
Basic Math Operations:
Square root of 25: 5.0
Power of 2^3: 8.0
Absolute value of -10: 10.0
Ceiling of 4.3: 5
Floor of 4.8: 4
Factorial of 5: 120
Trigonometric Functions:
Sine of 30 degrees: 0.49999999999999994
Cosine of 60 degrees: 0.5000000000000001
Tangent of 45 degrees: 0.9999999999999999
Logarithmic Functions:
Natural logarithm (base e) of 10: 2.302585092994046
Logarithm of 1000 to base 10: 3.0
Measures of Central Tendency:
Mean: 5
Median: 5
Mode: 1
Measures of Spread:
Variance: 7.5
Standard deviation: 2.7386127875258306
Other Statistical Functions:
Harmonic mean: 3.1813718614111375
Geometric mean: 4.147166274396913
5. Write a python to count number of vowels using sets in given string:
def count_vowels(input_string):
vowels = set("aeiouAEIOU") # Case-insensitive set of vowels
vowel_count = 0
for char in input_string:
if char in vowels:
vowel_count += 1
return vowel_count
# Example: Count vowels in a string
input_str = "Hello, World!"
result = count_vowels(input_str)
print(f"The number of vowels in the string '{input_str}' is: {result}")
Output:
The number of vowels in the string 'Hello, World!' is: 3
6. Write a python to remove all duplicates in given string:
def remove_duplicates(input_string):
unique_chars = set()
result_string = ""
for char in input_string:
if char not in unique_chars:
unique_chars.add(char)
result_string += char
return result_string
# Example: Remove duplicates from a string
input_str = "programming"
result = remove_duplicates(input_str)
print(f"The string without duplicates: {result}")
Output:
The string without duplicates: progamin
** Process exited - Return Code: 0 **
Press Enter to exit terminal
7. Write a python to find the sum of all elements in the list:
# Example: Find the sum of all elements in a list
numbers = [1, 2, 3, 4, 5]
# Using the sum function
sum_of_elements = sum(numbers)
print(f"The sum of all elements in the list is: {sum_of_elements}")
Output:
The sum of all elements in the list is: 15
8. Write a python to create a tuple from a given list having number and cube in each tuple:
# Example: Create a tuple from a list with numbers and their cubes
numbers_list = [1, 2, 3, 4, 5]
# Using a list comprehension to create tuples
tuples_list = [(num, num ** 3) for num in numbers_list]
print("List of tuples:", tuples_list)
Output:
List of tuples: [(1, 1), (2, 8), (3, 27), (4, 64), (5, 125)]
9. Write a python to create a dictionary which has record of student’s info
[Name, Roll no., admission no., Marks]
# Example: Create a dictionary with student information
students_info = {
'student1': {'Name': 'John', 'Roll No.': 'A101', 'Admission No.': 'AD12345', 'Marks': 85},
'student2': {'Name': 'Alice', 'Roll No.': 'A102', 'Admission No.': 'AD12346', 'Marks': 92},
'student3': {'Name': 'Bob', 'Roll No.': 'A103', 'Admission No.': 'AD12347', 'Marks': 78},
}
# Accessing and printing student information
for student_id, info in students_info.items():
print(f"\nStudent ID: {student_id}")
print("Name:", info['Name'])
print("Roll No.:", info['Roll No.'])
print("Admission No.:", info['Admission No.'])
print("Marks:", info['Marks'])
Output:
Student ID: student1
Name: John
Roll No.: A101
Admission No.: AD12345
Marks: 85
Student ID: student2
Name: Alice
Roll No.: A102
Admission No.: AD12346
Marks: 92
Student ID: student3
Name: Bob
Roll No.: A103
Admission No.: AD12347
Marks: 78
10. Write a python to find factorial of a number:
def factorial_recursive(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial_recursive(n - 1)
# Example: Find the factorial of a number using recursion
number = 5
result_recursive = factorial_recursive(number)
print(f"The factorial of {number} (using recursion) is: {result_recursive}")
Output:
The factorial of 5 (using recursion) is: 120
11. Loops to create student information using class:
class Student:
def __init__(self, name, roll_no, admission_no, marks):
self.name = name
self.roll_no = roll_no
self.admission_no = admission_no
self.marks = marks
def display_info(self):
print(f"\nStudent Information:")
print("Name:", self.name)
print("Roll No.:", self.roll_no)
print("Admission No.:", self.admission_no)
print("Marks:", self.marks)
# Example: Create student information using a loop
num_students = 3
students_list = []
for i in range(1, num_students + 1):
name = input(f"Enter the name of student {i}: ")
roll_no = input(f"Enter the roll number of student {i}: ")
admission_no = input(f"Enter the admission number of student {i}: ")
marks = float(input(f"Enter the marks of student {i}: "))
# Create an instance of the Student class
student = Student(name, roll_no, admission_no, marks)
students_list.append(student)
# Display information for all students
for student in students_list:
student.display_info()
Output:
Enter the name of student 1:
AMAN KUMAR
Enter the roll number of student 1:
01
Enter the admission number of student 1:
ARSD3156
Enter the marks of student 1:
96
Enter the name of student 2:
ADARSH KUMAR
Enter the roll number of student 2:
02
Enter the admission number of student 2:
ARSD3157
Enter the marks of student 2:
67
Enter the name of student 3:
ARYAN KUMAR
Enter the roll number of student 3:
03
Enter the admission number of student 3:
ARSD3158
Enter the marks of student 3:
56
Student Information:
Name: AMAN KUMAR
Roll No.: 01
Admission No.: ARSD3156
Marks: 96.0
Student Information:
Name: ADARSH KUMAR
Roll No.: 02
Admission No.: ARSD3157
Marks: 67.0
Student Information:
Name: ARYAN KUMAR
Roll No.: 03
Admission No.: ARSD3158
Marks: 56.0
** Process exited - Return Code: 0 **
Press Enter to exit terminal