Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Python Labs Manual 13 Nov 2024

Uploaded by

sachintr862001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python Labs Manual 13 Nov 2024

Uploaded by

sachintr862001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Python lab programs

1. a. Develop a program to read the student details like Name, USN, and Marks in three subjects.
Display the student details, total marks and percentage with suitable messages.
b. Develop a program to read the name and year of birth of a person. Display whether the person is
a senior citizen or not.

2. a. Develop a program to generate Fibonacci sequence of length (N). Read N from the console.
b. Write a function to calculate factorial of a number.
c. Develop a program to compute binomial coefficient (Given N and R).

3. Read N numbers from the console and create a list. Develop a program to print mean, variance
and standard deviation with suitable messages.

4. Read a multi-digit number (as chars) from the console. Develop a program to print the frequency
of each digit with suitable message.

5. Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use
dictionary with distinct words and their frequency of occurrences. Sort the dictionary in the reverse
order of frequency and display dictionary slice of first 10 items]

6. Develop a program to sort the contents of a text file and write the sorted contents into a separate
text file. [Hint: Use string methods strip(), len(), list methods sort(), append(), and file methods
open(), readlines(), and write()].

7. Develop a program to backing Up a given Folder (Folder in a current working directory) into a
ZIP File by using relevant modules and suitable methods.

8. Write a function named DivExp which takes TWO parameters a, b and returns a value c (c=a/b).
Write suitable assertion for a>0 in function DivExp and raise an exception for when b=0. Develop a
suitable program which reads two values from the console and calls a function DivExp.

9. Define a function which takes TWO objects representing complex numbers and returns new
complex number with a addition of two complex numbers. Define a suitable class ‘Complex’ to
represent the complex number. Develop a program to read N (N >=2) complex numbers and to
compute the addition of N complex numbers.

10. Develop a program that uses class Student which prompts the user to enter marks in three
subjects and calculates total marks, percentage and displays the score card details. [Hint: Use list to
store the marks in three subjects and total marks. Use __init__() method to initialize name, USN
and the lists to store marks and total, Use getMarks() method to read marks into the list, and
display() method to display the
score card details.
Algorithm:

step 1. Read Student Details


- Name
- USN (Unique Student Number)

step 2. Read Marks for Three Subjects


- Subject 1
- Subject 2
- Subject 3

step 3. Calculate Total Marks and Percentage


- Total Marks = Sum of marks in all three subjects
- Percentage = (Total Marks / 300) * 100

step 4. Display Results


- Student Name
- Student USN
- Total Marks
- Percentage
#1.a Develop a python code to read student details like name, usn makrs in
# three subjects and Display the results like total_marks and percentage, with suitable messages

name= input("Student please enter your name : ")

usn= input("Student please enter your usn : ")

s1=int(input("Student please enter your s1 marks : "))

s2=int(input("Student please enter your s2 marks : "))

s3=int(input("Student please enter your s3 marks : "))

totalmarks=s1+s2+s3

percentage=(totalmarks/300)*100

print("Student name : ",name)

print("Student usn : ",usn)

print("Student total_marks : ",totalmarks)

print("Student percentage : ",percentage)

................................................................................................................................................................

Program Output
Student please enter your name : Ali
Student please enter your usn : 24saec1234
Student please enter your s1 marks : 99
Student please enter your s2 marks : 98
Student please enter your s3 marks : 99

Student name : Ali


Student usn : 24saec1234
Student total_marks : 296
Student percentage : 98.66666666666667
Algorithm: Develop a program to read the name and year of birth of a person. Display whether the
person is a senior citizen or not.

1. Read User Details


- Name
- Birth Year
- Current Year
2. Calculate Age
- Age = Current Year - Birth Year
3. Determine Senior Citizen Status
- If Age ≥ 60, Display a message “person is a senior citizen”
- Else, Display a message “person is not a senior citizen”

................................................................................................................................................................

# 1.b Develop a program to read the name and year of birth of a person. Display whether the person

# is a senior citizen or not.

name=input("User please enter your name : ")

birthYear=int(input("Enter your birth year : "))

currentYear=int(input("Enter current year : "))

if ((currentYear-birthYear)>=60):

print(name,"is a senior citizen")

else:

print(name,"is not a senior citizen")


................................................................................................................................................................

Program output

User please enter your name : Ramesh


Enter your birth year : 1990
Enter current year : 2024
Ramesh is not a senior citizen

................................................................................................................................................................

User please enter your name : Suresh


Enter your birth year : 1940
Enter current year : 2024
Suresh is a senior citizen
#2.a. Develop a program to generate Fibonacci sequence of length (N). Read N from the console.

print("Welcome to Fabonacci series")

n=int(input("User how many numbers of this series you want to display ?"))

a=0

b=1

for i in range(n):

print(a)

temp=a

a=b

b=temp+b

print("here program ends")

................................................................................................................................................................

Program output

Welcome to Fabonacci series


User how many numbers of this series you want to display ? 6
0
1
1
2
3
5
here program ends
................................................................................................................................................................

Welcome to Fabonacci series


User how many numbers of this series you want to display ? 8
0
1
1
2
3
5
8
13
here program ends
# 2.b Write a function to calculate factorial of a number.

def myFactorial(number):

factorial=1

if number >0:

for i in range (1,number+1):

factorial=factorial*i

return factorial

elif number==0 :

return(1)

else:

return ("Factorial does not exist for negative numbers ")

print("Python code to find factorial given number")

n=int(input("User enter a number : "))

result=myFactorial(n)

print("Factorial of " ,n, "is:", result)

................................................................................................................................................................

Program output

Python code to find factorial given number

User enter a number : 4

Factorial of 4 is: 24

................................................................................................................................................................

Python code to find factorial given number

User enter a number : 0

Factorial of 0 is: 1

................................................................................................................................................................

Python code to find factorial given number

User enter a number : -4

Factorial of -4 is: Factorial does not exist for negative numbers

The Factorial of -3 is 0
#2.c Python program to compute binomial coefficient (Given N and R).

import math

def binomial_coefficient( n, r ):

if r > n:

return 0

else:

return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))

n = int(input("Enter the value of n : "))

r = int(input("Enter the value of r : "))

result = binomial_coefficient(n, r)

print(f"The binomial coefficient C({n}, {r}) is {result}")

................................................................................................................................................................
Program output

Enter the value of n : 5


Enter the value of r : 2
The binomial coefficient C(5, 2) is 10
................................................................................................................................................................
Enter the value of n : 8
Enter the value of r : 10
The binomial coefficient C(8, 10) is 0
Mean:

The mean, also known as the average, represents the central tendency of a dataset.

Formula: (Sum of all values) / Number of values

Example:

Values: 2, 4, 6, 8, 10

Mean = (2 + 4 + 6 + 8 + 10) / 5 = 30 / 5 = 6
................................................................................................................................................................

Variance:

Variance measures the spread or dispersion of a dataset.

Formula: Σ(xi - μ)² / (n)

where:

xi = individual values
μ = mean
n = number of values

Example:

Values: 2, 4, 6, 8, 10

Mean (μ) = 6

Variance = [(2-6)² + (4-6)² + (6-6)² + (8-6)² + (10-6)²] / (5)


= (16 + 4 + 0 + 4 + 16) / 5
= 40 / 5
=8
................................................................................................................................................................

Standard Deviation:

Standard deviation is the square root of variance, representing the spread of a dataset.
Formula: √Variance

Example:
Variance = 8
Standard Deviation = √8 = 2.8284271247461903
# 3. Read N numbers from the console and create a list. Develop a program to print mean, variance
# and standard deviation with suitable messages.

import math

def calculate_mean(numbers):

return sum(numbers) / len(numbers)

def calculate_variance(numbers,mean):

variance = 0

for x in numbers:

variance = variance + (x - mean) ** 2

variance = variance / len(numbers)

return variance

def calculate_standard_deviation(variance):

return math.sqrt(variance)

N = int(input("Enter the number of elements: "))

numbers = []

for i in range(N):

number = float(input("Enter element "+ str(i)+" : "))

numbers.append(number)

mean = calculate_mean(numbers)

variance = calculate_variance(numbers, mean)

standard_deviation = calculate_standard_deviation(variance)

print("Mean: ",mean)

print("Variance: ",variance)

print("Standard Deviation: ",standard_deviation)

...............................................................................................................................................................
Program output

Enter the number of elements: 5


Enter element 0 = 2
Enter element 1 = 4
Enter element 2 = 6
Enter element 3 = 8
Enter element 4 = 10
Mean: 6.0
Variance: 8.0
Standard Deviation: 2.8284271247461903

................................................................................................................................................................

Program output

Enter the number of elements: 5


Enter element 0 = 2.5
Enter element 1 = 3.3
Enter element 2 = 1.9
Enter element 3 = 3.4
Enter element 4 = 5.5
Mean: 3.3200000000000003
Variance: 1.4896
Standard Deviation: 1.2204917041913885
# 4. Read a multi-digit number (as chars) from the console. Develop a program to print the
# frequency of each digit with suitable message.

def count_digits():

num = input("Enter a number: ")

if not num.isdigit():

print("Error: Only digits allowed.")

else:

unique_digits = []

for digit in num:

if digit not in unique_digits:

unique_digits.append(digit)

print("Digit " + digit + ": " + str(num.count(digit)) + " time(s)")

count_digits()

................................................................................................................................................................
program output

Enter a number: 35354353555


Digit 3: 4 time(s)
Digit 5: 6 time(s)
Digit 4: 1 time(s)

................................................................................................................................................................

Enter a number: 6786869980808088084


Digit 6: 3 time(s)
Digit 7: 1 time(s)
Digit 8: 8 time(s)
Digit 9: 2 time(s)
Digit 0: 4 time(s)
Digit 4: 1 time(s)

You might also like