0% found this document useful (0 votes)
24 views

Python Manualex1, Ex2

Python programming

Uploaded by

gaganprince831
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)
24 views

Python Manualex1, Ex2

Python programming

Uploaded by

gaganprince831
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/ 10

Introduction to Python Programming (23PLB105/205) Lab Manual

Simple programs

Programming Exercises:

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. 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]

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’

CSE, CITECH 2023-24 Page 1


Introduction to Python Programming (23PLB105/205) Lab Manual

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.]

1. Input a welcome message and display it.

message=input('Enter Your Name : ')


print('Hello, '+ message)

Output

2. Adding 2 floating point numbers

# program to add 2 numbers

num1 = input('Enter first number: ')

num2 = input('Enter second number: ')

sum = float(num1) + float(num2)

print('The sum of '+str(num1)+' and '+str(num2)+' is '+str(sum))

print('The sum of',num1,'and',num2,'is',sum)

# different ways to print the sum

# The format() method formats the specified value(s) and insert

# them inside the string's placeholder.

# The placeholder is defined using curly brackets: {}.

# The format() method returns the formatted string.

CSE, CITECH 2023-24 Page 2


Introduction to Python Programming (23PLB105/205) Lab Manual

print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

print('The sum of {} and {} is {}'.format(num1, num2, sum))

# To create an f-string, prefix the string with letter "f"

# F-strings provide a concise and convenient way to embed

# python expressions inside string literals for formatting

print(f'The sum of {num1} and {num2} is {sum}')

Output

Enter first number: 6

Enter second number: 7.1

The sum of 6 and 7.1 is 13.1

The sum of 6 and 7.1 is 13.1

The sum of 6 and 7.1 is 13.1

The sum of 6 and 7.1 is 13.1

The sum of 6 and 7.1 is 13.1

3. Swap two values

x=5

y = 10

x, y = y, x

print('x =', x)

print('y =', y)

Output

x = 10

y=5

4. Swap two values using temporary variable

CSE, CITECH 2023-24 Page 3


Introduction to Python Programming (23PLB105/205) Lab Manual

x=5

y = 10

t=x

x=y

y=t

print('x =', x)

print('y =', y)

Output

x = 10

y=5

5. Input two numbers and display the larger / smaller number.

# Python Program to find Largest of two Numbers using if-else statements


a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))
if a >=b:
print(a, 'is greater or equal to b')
else:
print(b, 'is greater')

Output

CSE, CITECH 2023-24 Page 4


Introduction to Python Programming (23PLB105/205) Lab Manual

6. Input three numbers and display the largest / smallest number.

# Python program to find the largest number among the three input numbers
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num3:
largest = num2
else:
largest = num3
print("The largest number is", largest)
Output

7. Generate the following patterns using nested loop.

Pattern-1

*
for i in range(1,6):
**
for j in range(1,i+1):
***
print("*",end=' ')
****
print()
*****

Output

CSE, CITECH 2023-24 Page 5


Introduction to Python Programming (23PLB105/205) Lab Manual

Pattern-2
rows=5
12345
for i in range(rows + 1, 0, -1):
1234
for j in range(0, i - 1):
123
print(j+1, end=' ')
12 print(" ")
1

Output

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.

# 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.

usn = input('Enter the USN: ')

name = input('Enter the name: ')

m1 = int (input('Enter the marks in the first subject: '))

CSE, CITECH 2023-24 Page 6


Introduction to Python Programming (23PLB105/205) Lab Manual

m2 = int (input('Enter the marks in the second subject: '))

m3 = int (input('Enter the marks in the third subject: '))

total=m1+m2+m3

per=total/3

print('USN ',usn)

print('Name ',name)

print('Marks in Subject1 ',m1)

print('Marks in Subject2 ',m2)

print('Marks in Subject3 ',m3)

print('Total',total)

print('Percentage',round(per,2))Output

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.
# 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("Enter Name ")
current_year=int(input("Enter current year "))
yob=int(input("Enter year of birth "))
age=current_year-yob
if age>60:
status=name+ ' is a senior citizen'

CSE, CITECH 2023-24 Page 7


Introduction to Python Programming (23PLB105/205) Lab Manual

else:
status=name+' is not a senior citizen'
print(status)

Output

2. a. Develop a program to generate Fibonacci sequence of length (N). Read N from the
console.

# 2.a. Develop a program to generate Fibonacci sequence of length (N).

# Read N from the console.

n = int(input('Enter the number of terms '))

num1, num2 = 0, 1

if n <= 0:

print('Please enter a positive integer greater than zero')

elif n == 1:

print('Fibonacci sequence upto',n,':')

print(num1)

else:

print('Fibonacci sequence:')

print(num1,end=' ')

print(num2,end=' ')

i=2

while i < n:
CSE, CITECH 2023-24 Page 8
Introduction to Python Programming (23PLB105/205) Lab Manual

num3 = num1 + num2

print(num3,end=' ')

num1 = num2

num2 = num3

i += 1

Output

2 b. Write a function to calculate factorial of a number. Develop a program to compute


binomial coefficient (Given N and R).
# 2.b. Write a function to calculate factorial of a number.
# Develop a program to compute binomial coefficient (Given N and R).

def nCr(n, r):

return (fact(n) / (fact(r)* fact(n - r)))

def fact(n):

if n == 0:

return 1

res = 1

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

res = res * i

return res

n = int(input('Enter n '))

CSE, CITECH 2023-24 Page 9


Introduction to Python Programming (23PLB105/205) Lab Manual

r = int(input('Enter r '))

res=int(nCr(n,r))

print('Binomial Co-efficient of the given',n,'and',r,':',res)

Output

CSE, CITECH 2023-24 Page 10

You might also like