PART-A
1. Write a python program to swap two numbers using third variable.
x = 10
y = 50
print("Values of before swapping")
print("Value of x:",x)
print("Value of y:",y)
temp = x
x=y
y = temp
print("Values of after swapping")
print("Value of x:",x)
print("Value of y:",y)
Algorithm: Flow chart:
Step 1: start
Step 2: print x, y
Step 3: temp=x
x=y
y=temp
Step 4: print x, y
Step 5: Stop
2. Write a python program to enter two integers and perform all
arithmetic operations on them
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
print("printing the result for all arithmetic opertions:")
print("Addition:",a+b)
print("substraction:",a-b)
print("Multiplication:",a*b)
print("Division:",a/b)
print("Modulus:",a%b)
Algorithm: 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.
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)
Algorithm: Flow chart:
Algorithm:
Step 1: Start
Step 2: input l, b
Step 3: area=l*b
Perimeter = 2*(l+b)
Step 4: print area, perimeter
Step 5:Stop
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*T*R)/[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: Flow chart:
Step1:start
Step2:input P, T, R
Step3:si=(p*t*r*)/100
ap =p+si
step 4: print SI, AP
Step 5: Stop
[Link] 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)
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: Print largest
Step 5: Stop
Flowchart:
Start
Input num1, num2, num3
Largest=num1
If
num2>largest
Largest=num2
If
num3>largest
Largest=num3
Print largest
stop
[Link] 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
Step 4:print("you are eligible to apply for the driving license.")
else:
print("you are not eligible to apply for the driving license.")
Step 5: Stop
Flow chart: start
Input name,
age
If
age>=18
Print (Not Eligible)
Print (Eligible)
[Link] a python program that prints minimum and maximum of
five number entered by the user.
Algorithm: stop
Step 1: Start
Step 2: smallest=0 largest=0
Step 3: input for a in range (0,5):
Step 4: if a==0:
smallest=largest=x
if(x<smallest):
smallest=x
if(x>largest):
largest=x
Step 5: Print smallest , largest
Step 6: Stop
Program:
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)
start
Flow chart:
Input a
If a==0:
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
Algorithm:
Step 1: Start
Step 2: input 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: print percentage
Step 5: Stop
Program:
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:
start
Input n
Elif n>70 Elif n>=60
If n>90 Elif n>80
Grade B Grade C Grade D Grade E
Grade A
Print Grade
stop
Output:
Enter the percentage of the student 83
The grade of the given percentage is: B
[Link] a python program to print the table of a given number. The
number has to be entered by the user.
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
Program:
n=int (input("Enter the number:"))
count=1
while count<=10:
print(n, 'x', count,'=',n*count)
count=count+1
Output: Flow chart:
Enter the number:5 start
5x1=5
5 x 2 = 10
5 x 3 = 15 Input n
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35 Count=1
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
While count<=10
Print n
Count=count+1
stop
10. Write a python program to find the sum of digits of an integer
number, input by the user.
Algorithm:
Step 1: Start
Step 2: input n
Step 3: while n>0:
digit=n%10
sum=sum + digit
n=n//10
Step 4: print sum
Step 5: Stop
Program:
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)
Output: Flow chart:
enter the number:12345 Start
The sum of digits of the number is 15
Input n
Sum=0
While n>0
digit=n%10
sum=sum + digit
n=n//10
Print sum
stop
11. Write a Python Program to check Whether an input number is a
palindrome or not.
Algorithm:
Step 1: Start
Step 2: input n
Step 3: while n>0:
digit=n%10
reverse=reverse*10+digit
n=n//10
Step 4: if temp==reverse:
print("it is palindrome")
else:
print("not a palindrome")
Step 5: Stop
Program:
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")
output:
Enter a number:121
it is palindrome
Enter a number:1231
not a palindrome
Flow chart: Output:
start Enter a number:121
it is palindrome
Enter a number:1231
Input n
not a palindrome
Reverse=0
Temp=n
While n>0:
digit=n%10
reverse=reverse*10+digitn
=n//10
if temp==rev
Print(palindrome) Print(not a palindrome)
stop
12. Write a python program to print the following patterns.
12345
1234
123
12
1
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
Program:
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()
Output: Flow chart:
Enter the number of rows:5
12345
1234
123
12
1
PART-B
[Link] 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.
Program :
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)
Algorithm:
Step 1: Start
Step 2: def prefix name , gender
Step 3: 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")
Step 4: input name, gender
Step 5: function call prefix name ,gender
Step 6: Stop
FLOWCHART:
START
INPUT Name, Gender
IF
GENDER==M
PRINT(“Hello, Mr”, name)
IF
GENDER==F
Print(“ Hello, Ms”, name)
Print(“enter only M OR F”)
STOP
2. Write a python program that has a user defined function to accept the
coefficients of a quadratic equation in variable and calculates its
discriminant. For example: if the coefficients are stored in the variable a, b,
c then calculate discriminants as b2-4ac. Write the appropriate condition to
check discriminant on positive, zero and negative and out appropriate
result.
Program:
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()
Algorithm:
Step 1: Start
Step 2: def Input a, b, c
Step 3: dis=b**2-4*a*c
print("Discriminant=",dis)
Step 4: if dis>0:
print("Two distinct real roots")
elif dis==0:
print("One distinct real roots")
else:
print("No distinct real roots")
Step 5: Stop
FLOW CHART: START
INPUT a, b, c
dis=b**2-4*a*c
if dis>0: Print(Two distinct real)
roots")
elif dis==0:
Print(one distinct real) Print (No distinct real)
ro
Stop
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 number 1
is reformed in place of number 2, otherwise the same order is returned.
Program:
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 Swapping:")
num1,num2=swapn(num1, num2)
print("Number 1:",num1,"Number 2:",num2)
Algorithm:
Step 1: Start
Step 2: def num1, num2
Step 3: if num1<num2:
return num2, num1
else:
return num1, num2
Step 4: input num1, num2
Step 5: print num1, num2 after swapping
Step 6: Stop
FLOWCHART: START
Input num1,num2
If
num1<num2:
Return num2, num1 Return num1, num2
Print(num1, num2)
Print(after swapn, num1, num2)
stop
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).
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 alpha, digit, space, spl
Step 5: Stop
Program:
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)
START
FLOWCHART:
Input a
Print(No of character, len(a))
alpha=digit=space=spl=0
if a[i].isalpha(): alpha=alpha+1
elif a[i].isdigit(): digit=digit+1
space=space+1
elif a[i].isspace():
spl=spl+1
Print( alpha, digit, space, spl)
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
STOP
capitalized).
Algorithm:
Step 1: Start
Step 2: def convert(a):
Step 3: if" " in a:
b=[Link](" ")
if len(b)>1:
print([Link]())
else:
print("One Word")
Step 4: input text
Step 5 convert(text)
Step 6: Stop
Program:
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)
FLOW CHART: START
Input text
if" " in a:
b=[Link](" ")
if len(b)>1:
print([Link]()) print("One Word")
STOP
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.
Algorithm:
Step 1:Start
Step 2: def replace(string)
Step 3: return [Link](" ","-")
Step 4:input text
Step 5: result=replace(text)
print(result)
Step 6: Stop
Program:
def replace(string):
return [Link](" ","-")
text=input("Enter the sentence:")
result=replace(text)
print("The new sentence is:",result)
FLOW CHART: START
INPUT Text
return [Link](" ","-")
result=replace(text)
Print( “new sentence is”,result)
STOP
7. Write a python program to find the number of times an element occurs
in the list.
Algorithm:
Step 1: Start
Step 2: input list1
Step3: count=[Link](item)
Step 4: if item in list1:
print(The no of times occured in the list)
else:
print(The item is not in the list)
Step 5: Stop
Program:
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?"))
count=[Link](item)
if item in list1:
print("The no of times","occured in the list is:",count)
else:
print("The item",item,"is not in the list")
FLOW CHART:
START
INPUT list1[elements]
Input item
Count=[Link](item)
If item in list1:
Print(Item occurred in list, count) Print(item is not in the list)
STOP
8. Write a python program with function that returns the largest element
of the list passed as parameter.
Algorithm:
Step 1: start
Step 2: def largest(list1):
Step 3: largest=max(list1)
return largest
Step 4: input l
Step 5: print largest(l)
Step 6: Stop
Program:
def largest(list1):
largest=max(list1)
return largest
l=eval(input("enter the elements"))
print("The largest element in the list is:", largest(l))
FLOW CHART:
START
Input l
Largest=max(list1)
Return largest
Print largest
STOP
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.
Algorithm:
Step 1:Start
Step 2: input list
Step 3: list1=[]
for i in list:
if i not in list1:
[Link](i)
Step 4: print list1
Step 5: Stop
Program:
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)
FLOW CHART:
START
Input list
List1=[]
If I not in list1 [Link](i)
Print(without duplicate, list1)
stop
[Link] a python 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().]
Algorithm:
Step1: Start
Step2: input n
Step3: for i in range(0,n):
emid=input(">")
emails=emails+(emid,)
spl=[Link]("@")
username=username+(spl[0],)
domainname=domainname+(spl[1],)
Step4: print email id, username, domain name
Step5: Stop
Program:
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("\n the email ids in the tuple are:")
print(emails)
print("\n the username in the email ids are:")
print(username)
print("\n the domain name in the email ids are:")
print(domainname)
START
FLOW CHART:
emails=tuple()
username=tuple()
domainname=tuple()
Input n
for i in range(0,n)
Input emid
emid=input(">")
emails=emails+(emid,)
spl=[Link]("@")
username=username+(spl[0],)
domainname=domainname+(spl[1],)
print email id, username, domain name
STOP
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:
Step1: Start
Step2: input n
Step3: for i in range(0, n):
num=input(">")
name=name+(num,)
Step4: print name
Step5: input search
Step6: if search in name:
Print(name present in tuple)
Else:
Print(name not present in tuple)
Program:
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("\n The names entered in the tuple are:")
print(name)
search=input("\n Enter 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")
FLOW CHART: START
Name=tuple()
Input n
For i in range(0,n)
num=input(">")
name=name+(num,)
Print name
Input search
If search in name:
Print(name present in tuple) Print(name not present in tuple)
STOP
12. Write python program to create a dictionary from a string.
Algorithm:
Step 1: Start
Step 2:input String
Step 3: dic={}
for ch in string:
if ch in dic:
dic[ch]=dic[ch]+1
else:
dic[ch]=1
Step 4: for key in dic:
print(key,':',dic[key])
Step 5: Stop
Program:
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])
FLOWCHART:
Start
Input str
Dic{}
If ch in
Dic[ch]=1
dic?
Dic[ch]+=1
Print dic [key]
stop