0% found this document useful (0 votes)
3 views10 pages

083 - XI - CS - Practical Programs

The document provides a series of Python programming exercises aimed at various tasks such as finding larger and smaller numbers, generating Fibonacci series, calculating factorials, and manipulating lists and dictionaries. Each exercise includes a clear aim, code implementation, and a confirmation of successful execution. The examples cover basic programming concepts and operations, including loops, conditionals, and data structures.
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)
3 views10 pages

083 - XI - CS - Practical Programs

The document provides a series of Python programming exercises aimed at various tasks such as finding larger and smaller numbers, generating Fibonacci series, calculating factorials, and manipulating lists and dictionaries. Each exercise includes a clear aim, code implementation, and a confirmation of successful execution. The examples cover basic programming concepts and operations, including loops, conditionals, and data structures.
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

Ex 1 : Python program to input two numbers and display larger and smaller number

AIM : To create python program to input two numbers and display larger and smaller number

# python program to find smaller and larger number


#take two number from user
num1 = int(input("Enter first number :"))
num2 = int(input("Enter second number :"))
if (num1 > num2):
print(num1,"is larger than",num2)
elif (num1 == num2):
print(num1,"is equal to",num2)
else :
print(num1,"is smaller than",num2)

RESULT : Thus, the above Python program is executed successfully and the output is verified.

Ex 2 : Input three numbers and display the largest/smallest number Python


Program

AIM : To create Input three numbers and display the largest/smallest number Python
Program

##input first,Second and third number

num1=int(input("Enter First Number"))

num2=int(input("Enter Second Number"))

num3=int(input("Enter Third Number"))

#Check if first number is greater than rest of the two numbers.

if (num1> num2 and num1> num3):

print("The Largest number is", num1)

#Check if Second number is greater than rest of the two numbers.

elif (num2 > num1 and num2> num3):

print ("The Largest number is", num2)

else:

print ("The Largest number is", num3)

RESULT : Thus, the above Python program is executed successfully and the output is verified.
Ex 3 : Patterns using nested for Loop in Python

AIM : To write a python program to display the following patterns using nested for Loop

pattern #1

for i in range(5): #change the range stop value for rows

for j in range(10): # change the range stop value for columns

print('*',end='')

print('')

Output

**********
**********
**********
**********
**********
pattern #2

for i in range(10):

for j in range(10-i):

print('*',end='')

print('')

**********
*********
********
*******
******
*****
****
***
**
*
pattern #3

for i in range(1,9):

for j in range(i):

print('*',end='')

print('')

*
**
***
****
*****
******
*******
********
pattern #4

for i in range(1,11):

for k in range(i,10):

print(' ',end='')

for j in range(2*i-1):

print('*',end='')

print('')

*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
pattern #5

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

for k in range(i,11):

print(' ',end='')

for j in range((2*i)-1):

print('*',end='')

print('')

*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
pattern #6

for i in range(0,12):

for k in range(i,11):

print('*',end='') # left side

for j in range((2*i)): #

print(' ',end='') # blank between


for k in range(i,11):

print('*',end='') # right side

print('')

**********************
********** **********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *

RESULT : Thus, the above Python program is executed successfully and the output is verified.

Ex 4 : Generating Fibonacci Series

AIM : To generate a Fibonacci Series using while loop in python

n=15 # number of elements

i=0 # counter

n1=1 # One before

n2=0 # two before

while( i<n):

print(n2, end=', ')

temp=n1+n2

n1=n2

n2=temp

i = i+1

RESULT : Thus, the above Python program is executed successfully and the output is verified.

Ex 5 : Factorial of an input number by using loop

AIM : To calculate factorial of a number given by the user using for loop

a=int(input("Enter a number : "))


b=1
for i in range(1,a+1):
b=b*i
print("Factorial : ", b)

Write a program to find the largest number in a list.


#Variable declaration
n=int(input("Enter the number of elements:"))
l=[]
m=0
#Input from user
for i in range(n):
val=int(input("Enter element:"))
l.append(val)
print("The original list:",l)

#Finding maximum
for i in l:
if i>m:
m=i
print("The lasrgest number is:",m)

RESULT : Thus, the above Python program is executed successfully and the output is verified.

Ex 6 : Write a program to swap elements at the even location with the elements
odd location.

AIM : To Write a program to swap elements at the even location with the elements odd
location.

#Accept the list values


l=eval(input("Enter the list:"))

#printing orioginal list


print("Original List:",l)

#Finding length and determining length for odd location


length=len(l)
if length%2!=0:
length=length-1

#Logic for swapping elements


for i in range(0,length,2):
l[i],l[i+1]=l[i+1],l[i]

#printing elements after swap


print("List after swap:",l)
Input a list of elements and search a particular element.
#Accept the list values
l=eval(input("Enter the list:"))

#Ask for element to search


n=int(input("Enter element to search:"))

#Variable to check whether element found or not


f=0

#printing original list


print("Original List:",l)

#Checking element is present in the list or not


for i in l:
if i==n:
f=1
break
else:
f=0

#Printing the message if element found in the list


if f==1:
print("Element found in the list")
else:
print("Element not found in the list")

RESULT : Thus, the above Python program is executed successfully and the output is verified.

Ex 7 : Write a python program to create a tuple and print a square of each element.

AIM : To Write a python program to create a tuple and print a square of each element.

#Accepting the tuple


t=eval(input("Enter elements for tuple"))

#Converting the tuples into list for manipulation


l=list(t)

#Printing original list


print("The original tuple:",t)

#traversing the list for computation


for i in range(len(l)):
l[i]=l[i]**2

#Converting list back to tuple


t=tuple(l)

#Printing the square of each element


print(t)

RESULT : Thus, the above Python program is executed successfully and the output is verified.

Ex 8 : Write a program to accept string into tuple and extract the digits into a new
list. Print the extracted numeric list.

AIM : Write a program to accept string into tuple and extract the digits into a new list.
Print the extracted numeric list.

#Accepting the tuple


t=eval(input("Enter string elements for tuple:"))

#Declaring list object for extracted elements


l=[]

#Logic to extract numbers from the given tuple


for i in t:
if i.isdigit():
l.append(i)

#CONVERTING THE STRIN INTO INTEGER


for i in range(len(l)):
l[i]=int(l[i])

#PRINTING THE FINAL EXTRACTED LIST


print(l)

RESULT : Thus, the above Python program is executed successfully and the output is verified.

Ex 9 : Write a program to create a dictionary with the roll number, name and
marks of n students in a class and display the names of students who have marks
above 75.

AIM : Write a program to create a dictionary with the roll number, name and marks of n
students in a class and display the names of students who have marks above 75.

#Input for Number of stude


n=int(input("How many students?:"))

#Empty Dictionary
stu={}

#Data Input
for i in range(n):
print("Enter details of student:")
rn=int(input("Enter roll number:"))
name=input("Enter Name:")
marks=float(input("Enter Marks:"))
stu[rn]=[name,marks]
#Logic to display detail of students more than 75 marks
for i in stu:
if stu[i][1]>75:
print("Name:",stu[i][0],"Marks:",stu[i][1])

RESULT : Thus, the above Python program is executed successfully and the output is verified.

Ex 10 : Menu-driven program to perform Show Record, Add new Customer, Delete a


customer, Search Record, Sort Record operations using Dictionary:

AIM : To write a Menu-driven program to perform the following operations using


Dictionary

1. Show record
2. Add new customer
3. Delete a customer
4. Search record
5. Update record
6. Sort record
7. Exit
n=int(input("How many customers:"))
cust={}
for i in range(n):
print("Enter details of customer:",i+1)
cname=input("Enter name of customer:")
pn=int(input("Enter Phone number:"))
cust[cname]=pn
c=0
while c!=7:
print('''
1. Show record
2. Add new customer
3. Delete a customer
4. Search record
5. Update record
6. Sort record
7. Exit
''')
c=int(input("Enter your choice:"))
if c==1:
for i in cust:
print(i,":",cust[i])
elif c==2:
print("Enter details of new customer:")
cname=input("Enter name:")
pn=int(input("Enter phone number:"))
cust[cname]=pn
elif c==3:
nm=input("Enter name to be deleted:")
f=cust.pop(nm,-1)
if f!=-1:
print(f, " is deleted successfully.")
else:
print(f, " not found.")
elif c==4:
name=input("Enter Customer Name:")
if name in cust:
print(name, " found in the record.")
else:
print(name, " not found in the record.")
elif c==5:
cname=input("Enter Customer Name:")
pn=int(input("Enter phone number to modify:"))
cust[cname]=pn
elif c==6:
l=sorted(cust)
for i in l:
print(i,":",cust[i])
elif c==7:
break

RESULT : Thus, the above Python program is executed successfully and the output is verified.
Ex 11 : Python program to find the highest 2 values in the dictionary.

AIM : To write a python program to find the highest 2 values in the dictionary.

n=int(input("How many students:"))


std={}
for i in range(n):
print("Enter details of customer:",i+1)
sname=input("Enter name of student:")
per=int(input("Enter percentage:"))
std[sname]=per

s=sorted(std.values())
print("Top two values:",s[-1],s[-2])

RESULT : Thus, the above Python program is executed successfully and the output is verified.

Ex 12 : Python Program to choose any 4 customers randomly for lucky winners out
of 100 customers

AIM : To write a Python Program to choose any 4 customers randomly for lucky winners
out of 100 customers
Import random

c1=random.randint(1,100)
c2=random.randint(1,100)
c3=random.randint(1,100)
c4=random.randint(1,100)
print("Lucky winners are:",c1,c2,c3,c4)

RESULT : Thus, the above Python program is executed successfully and the output is verified.

Ex 13 : Menu-driven program to perform few string operations by importing string


module.

AIM : Write a Menu-driven program to perform following operations by importing string


module.

1. Display the ascii letters


2. Display the digits
3. Display Hexadedigits
4. Display Octdigits
5. Display Punctuation
6. Display the first letter of each word into capital by removing spaces

import string
ch=0
while ch!=7:
print('''
1. Display the ascii letters
2. Display the digits
3. Display Hexadedigits
4. Display Octaldigits
5. Display Punctuation
6. Display string in title case
7. Exit
''')
ch=int(input("Enter your choice:"))
if ch==1:
print(string.ascii_letters)
elif ch==2:
print(string.digits)
elif ch==3:
print(string.hexdigits)
elif ch==4:
print(string.octdigits)
elif ch==5:
print(string.punctuation)
elif ch==6:
s=input("Enter sentence:")
print(string.capwords(s))
elif ch==7:
break

RESULT : Thus, the above Python program is executed successfully and the output is verified.

You might also like