0% found this document useful (0 votes)
5 views28 pages

1pu Lab Program

The document contains a series of Python programming exercises, each accompanied by algorithms, flow charts, and sample outputs. It covers various topics including swapping numbers, arithmetic operations, calculating area and perimeter, simple interest, finding the largest number, checking for palindromes, and more. Additionally, it includes user-defined functions for string manipulation and list operations.

Uploaded by

vejjukk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views28 pages

1pu Lab Program

The document contains a series of Python programming exercises, each accompanied by algorithms, flow charts, and sample outputs. It covers various topics including swapping numbers, arithmetic operations, calculating area and perimeter, simple interest, finding the largest number, checking for palindromes, and more. Additionally, it includes user-defined functions for string manipulation and list operations.

Uploaded by

vejjukk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PYTHON PART A PROGRAMS

1. Write a python program to swap two numbers using a third variable.

x=10
y=50
print("Values of variables before swapping")
print("Value of x:", x)
print("Value of y:", y)
temp = x
x=y
y = temp
print("Values of variables after swapping")
print("Value of x:", x)
print("Value of y:", y)

FLOW CHART

start
Algorithm
x=10
Step 1: start y=50
step 2: print x ,y

step 3: temp=x Print before


swapping x,y
x=y

y=temp temp = x
x=y
step 4: print x,y y = temp

step 5: stop
Print after swapping
x,y

stop

2. Write a python program to enter two integers and perform all arithmetic operations on them.
a = int(input("Enter first number: ")) Output:
b = int(input("Enter second number: ")) Enter first number: 5
print("Printing the result for all arithmetic Enter second number: 4
operations:") Printing the result for all arithmetic operations:-
print("Addition: ",a + b) Addition: 9
print("Subtraction: ",a-b) Subtraction: 1
print("Multiplication: ",a*b) Multiplication: 20
print("Division: ",a/b) Division: 1.25
print("Modulus: ", a %b) Modulus: 1
FLOW CHART

Algorithm
step 1: start
step 2: input a,b
step 3: print a+b
print a-b
print a*b
print a/b
print a%b
step 4: stop

3. Write a Python program to accept length and width of a rectangle and compute its perimeter and area.

algorithm output
step 1: start Enter length of the rectangle: 5
step 2: input l,b Enter breadth of the rectangle: 3
step 3 : area = l*b Area of rectangle = 15.0
perimeter = 2*(l+b) Perimeter of rectangle = 16.0
step 4 : print area , perimeter
step 5: stop
l= float(input("Enter length of the rectangle: "))
b = float(input("Enter breadth of the rectangle: "))
area = l * b
perimeter = 2 * (l + b)
print("Area of rectangle = ", area)
print("Perimeter of rectangle = ", perimeter)

Flow chart
4. Write a Python program to calculate the amount payable if money has been lent on simple interest.
Principle or money lent = P, Rate of interest = R% per annum and Time = T years. Then Simple Interest
(SI) = (P x R x T)/ [Link] payable = Principle + SI . P, R and T are given as input to the program.

p=float(input('Enter amount: '))


t=float(input('Enter time: '))
r=float(input('Enter rate: '))
si=(p*t*r)/100
ap=p+si
print('Simple interest is: ',si)
print('amount payable is:',ap)

Algorithm
step 1: start
step 2 : input p,t,r
step 3 : si=p*t*r/100
ap= p+si
step 4 : print si , ap
step 5: stop
Flow chart

5. Write a Python program to find largest among three numbers.

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


num2=float(input("Enter second number: "))
num3=float(input("Enter third number: "))
largest=num1
if num2>largest:
largest=num2
if num3>largest:
largest=num3
print("The largest number is", largest)
output

Algorithm
step 1 : start
step 2: input num1 , num2 , num3
step 3 : largest=num1
if num2>largest:
largest=num2
if num3>largest:
largest=num3
step 4 : output largest
step 5: stop
Flow chart

6. Write a python program that takes the name and age of the user as input and displays a message whether
the user is eligible to apply for a driving license or not.(the eligible age is 18 years).

name=input("What is your name? ")


age=int(input("What is your age? "))
if age>=18:
print("You are eligible to apply for the driving license.")
else:
print("You are not eligible to apply for the driving license.")
Algorithm
step 1: start
step 2 : input name , age
step 3 : if age>=18:
print:you are eligible to apply for the driving license.
else:
print:you are not eligible to apply for the driving license.
step 4: stop
Flow chart

7. Write a python program that prints minimum and maximum of five numbers entered by the user.

smallest=0
largest=0
for a in range(0,5):
x=int(input("Enter the number: "))
if a==0:
smallest = largest = x
if(x<smallest):
smallest = x
if(x>largest):
largest = x
print("The smallest number is" ,smallest)
print("The largest number is ",largest)
FLOW CHART

START

Algorithm
step 1: start input a
step 2: smallest =0 , largest = 0
step 3: input for a in range(0,5):
step 4: if a==0: if a==0: smallest = largest = x
if(x<smallest):smallest
smallest = largest =x =x
if(x<smallest): if(x>largest):largest = x
smallest = x
if(x>largest): Output
largest = x largest ,smallest
step 5: output largest ,smallest
step 6: stop
STOP

8. Write a python program to find the grade of a student when grades are allocated as given in the table
below. Percentage of Marks Grade
Above 90% A
80% to 90% B
70% to 80% C
60% to 70% D
Below 60% E

Algorithm
step 1: start
step 2: input percentage n
step 3:
if n>90:
grade="a"
elif n>80:
grade="b"
elif n>70 :
grade="c"
elif n>=60:
grade="d"
else:
grade="e"
step 4: output grade
step 5: stop
n = float(input(“Enter the percentage of the student: “))
if n>90:
grade="A"
elif n>80:
grade="B"
elif n>70 :
grade="C"
elif n>=60:
grade="D"
else:
grade="E"
print("The Grade of the given percentage is:",grade)

Flow chart
9. Write a python program to print the table of a given number. The number has to be entered by the user.

n = int(input("Enter the number: "))


count = 1
while count <= 10:
print(n, 'x', count, '=', n*count)
count=count+1

Algorithm
step 1: start
step 2: input n
step 3: count = 1
while count <= 10:
print(n, 'x', count, '=', n*count)
count=count+1
step 4: stop

Flow chart
START
Input n

count = 1

while count NO
<= 10:

YES
print(n, 'x', count, '=',
n*count)

count = count+1

STOP

10 . Write a program to find the sum of digits of an integer number, input by the user.
sum=0
n = int(input("Enter the number: "))
while n > 0:
digit = n % 10
sum = sum + digit
n = n//10
print("The sum of digits of the number is",sum)

Algorithm
step 1: start
stpe 2: input n
step 3: while n > 0:
digit = n % 10
sum = sum + digit
n = n//10
step 4: output sum
step 5: stop
Flow chart
11 . Write a python program to check whether an input number is a palindrome or not.
n=int(input("Enter a number:"))
temp=n
reverse=0
while n>0:
digit=n%10
reverse=reverse*10+digit
n=n//10
if temp==reverse:
print("It is Palindrome")
else:
print("Not a Palindrome")

Algorithm
step 1: start
step 2: input n
step 3: while n>0:
digit=n%10
reverse=reverse*10+digit
n=n//10
if temp==reverse:
print("It is Palindrome")
else:
print("Not a Palindrome")
step 4: stop
Flow chart

12 .Write a python program to print the following patterns:


rows = int(input("Enter the number of rows: "))

for i in range(rows ,0 ,-1):

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

print(j, end=" ")

print()

Algorithm
Step 1: start
Step 2: input rows
Step 3: for i in range(rows ,0 ,-1):
for j in range(1 ,i+1):
Step 4: print(j, end=" ")
print()
Step 5: stop

Flow Chart
PYTHON PART B PROGRAMS
1. Write a python program that uses a user defined function that accepts name and gender (as M for Male,
F for Female) and prefixes Mr. / Ms. based on the gender.

Flow chart

Algorithm
Step 1: Start
Step 2: input name ,gender
Step 3: result = prefix(name ,gender)
Step 4: output result
Step 5: Stop
def prefix(name, gender):
if gender=='M' or gender=='m':
print("Hello, Mr.", name)
elif gender=='F' or gender=='f':
print("Hello, Ms.", name)
else:
print("Please enter only M or F in gender")
name=input("Enter your name:")
gender=input("Enter your gender: M for Male or F for Female:")
prefix(name, gender)
2. Write a python program that has a user defined function to accept the coefficients of a quadratic
equation in variables and calculates its [Link] example: if the coefficients are stored in the
variables a, b, c then calculate discriminant as b2-4ac. Write the appropriate condition to check
discriminant on positive, zero and negative and output appropriate result.

Flow chart

Algorithm
Step 1: Start
Step 2: input a,b,c
Step 3: dis=b**2-4*a*c
Step 4: print the roots
step 5: Stop
def quadratic() :
a=float(input("Enter the value of a :"))
b=float(input("Enter the value of b :"))
c=float(input("Enter the value of c :"))
dis=b**2-4*a*c
print("Discriminant =", dis)
if dis>0:
print("The quadratic equation has two distinct real roots")
elif dis==0:
print("The quadratic equation has one distinct real roots")
else:
print("The quadratic equation has no distinct real roots")
quadratic()
3. Write a python program that has a user defined function to accept 2 numbers as parameters, if number 1
is less than number 2 then numbers are swapped and returned, i.e., number 2 is returned in place of
number1 and number 1 is reformed in place of number 2, otherwise the same order is returned.

Flow chart

Algorithm
Step 1: Start
Step 2: input num1,num2
Step 3: num1,num2=swapn(num1,num2)
Step 4: print num1,num2 after swapping
Step 5: Stop

def swapn(num1,num2):
if num1<num2:
return num2,num1
else:
return num1,num2
num1=int(input("Enter the value of num1:"))
num2=int(input("Enter the value of num2:"))
print("Entered values are:")
print("Number 1:",num1,"Number 2:",num2)
print("Values after Swaping:")
num1,num2=swapn(num1,num2)
print("Number 1:",num1,"Number 2:",num2)
Output 1 Output 2

4. Write a python program to input line(s) of text from the user until enter is pressed. Count the total
number of characters in the text (including white spaces), total number of alphabets, total number of
digits, total number of special symbols and total number of words in the given text. (Assume that each
word is separated by one space).

Flow Chart
Algorithm:
Step 1: Start
Step 2: input a
Step 3:
alpha=digit=space=spl=0
for i in range(len(a)):
if a[i].isalpha():
alpha=alpha+1
elif a[i].isdigit():
digit=digit+1
elif a[i].isspace():
space=space+1
else:
spl=spl+1
Step 4: print alphabets,digits,words,special symbols
Step 5: Stop
a=input("Enter the text :")
print("No of character:",len(a))
alpha=digit=space=spl=0
for i in range(len(a)):
if a[i].isalpha():
alpha=alpha+1
elif a[i].isdigit():
digit=digit+1
elif a[i].isspace():
space=space+1
else:
spl=spl+1
print("No of Alphabets:",alpha)
print("No of Digits:",digit)
print("No of Words:",space+1)
print("No of Special Symbols:",spl)
Output

5. Write a python program with user defined function to convert a string with more than one word into
title case string where string is passed as parameter. (Title case means that the first letter of each word is
capitalized).
Flow Chart

Algorithm
Step 1: Start
Step 2: input text
Step 3: convert text
Step 4:print text
Step 5: Stop
def convert(a):
if " " in a:
b=[Link](" ")
if len(b)>1:
print([Link]())
else:
print("One Word")
text=input("Enter the Text :")
convert(text)
Output

6. Write a function that takes a sentence as an input parameter where each word in the sentence is
separated by a space. The function should replace each blank with a hyphen and then return the
modified sentence.
Flow Chart

Algorithm
Step 1: Start
Step 2: input text
Step 3: convert text
Step 4: print result
Step 5: Stop
def replace(string):
return [Link](" ","-")
text=input("Enter the sentence :")
result=replace(text)
print("The new sentence is :",result)
Output

7. Write a program to find the number of times an element occurs in the list.
Algorithm
Step 1: Start
Step2: print list1,Input item
Stpe3: count=[Link](item)
Step4: print count
Step 5: Stop
list1=[10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
print("The list is:",list1)
item=int(input("Which element occurrence would you like to count? "))
if item in list1:
print("The no of times",item,"occured in the list is:",count)
else:
print("The item ",item,"is not in the list")
Output 1
Output 2

Flow Chart

8. Write a python program with function that returns the largest element of the list passed as parameter.
Algorithm
Step 1: Start
Step 2: input elements
Step 3: find the largest
Step 4: print Largest
Step 5: Stop
def largest(list1):
largest=max(list1)
return largest
l=eval(input("Enter the Elements"))
print("The largest element in the list is :",largest(l))
Output
Flow Chart

9. Write a program to read a list of elements. Modify this list so that it does not contain any duplicate
elements, i.e., all elements occurring multiple times in the list should appear only once.

Flow Chart

Algorithm
Step 1: Start
Step 2: enter the elements in list
Step 3: check for duplicate in list
Step 4: print the list with out duplicate elements
Step 5: Stop
list=eval(input("Enter a list :"))
list1=[]
for i in list:
if i not in list1:
[Link](i)
print("The list with out duplicate elements",list1)
Output

10. Write a program to read email IDs of n number of students and store them in a tuple. Create two new
tuples, one to store only the usernames from the email IDs and second to store domain names from the
email IDs. Print all three tuples at the end of the program. [Hint: You may use the function split().

Flow chart
Algorithm
Step 1: Start
Step 2: input email id
Step 3: print email id, user name , domain name
Step 4: Stop
emails = tuple()
username = tuple()
domainname = tuple()
n = int(input("How many email ids you want to enter?: "))
for i in range(0,n):
emid = input("> ")
emails = emails +(emid,)
spl = [Link]("@")
username = username + (spl[0],)
domainname = domainname + (spl[1],)
print("\nThe email ids in the tuple are:")
print(emails)
print("\nThe username in the email ids are:")
print(username)
print("\nThe domain name in the email ids are:")
print(domainname)
Output
11. Write a program to input names of n students and store them in a tuple. Also, input a name from the user and find
if this student is present in the tuple or not.

Algorithm
Step 1: Start
Step 2: input the names of the student
Step 3: check the name of the student in the list
Step 4: print the name of the student if found if not print data not found
Step 5: Stop
name = tuple()
n = int(input("How many names do you want to enter?: "))
for i in range(0, n):
num = input("> ")
name = name + (num,)
print("\nThe names entered in the tuple are:")
print(name)
search=input("\nEnter the name of the student you want to search? ")
if search in name:
print("The name",search,"is present in the tuple")
else:
print("The name",search,"is not found in the tuple")
Output
Flow Chart
12. Write a Python program to create a dictionary from a string.

Algorithm
Step 1: Start
Step 2: input a string
Step 3: check in the dictionary
Step 4: print the string
Step 5: Stop
string=input("Enter a String :")
dic={}
for ch in string:
if ch in dic:
dic[ch]=dic[ch]+1
else:
dic[ch]=1
for key in dic:
print(key,':',dic[key])
Output

Flow Chart

You might also like