Practicals 13 To 19
Practicals 13 To 19
AIM
Input a list/tuple of elements and search for a given element in the list/tuple Python Program
Source code
mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
print("Enter an element to be search: ")
element = int(input())
for i in range(5):
if element == mylist[i]:
print("\nElement found at Index:", i)
print("Element found at Position:", i+1)
Output
14. AIM
Input a list of numbers and find the smallest and largest number from the list Python Program
Source Code
mylist = []
number = int(input('How many elements to put in List: '))
for n in range(number):
element = int(input('Enter element '))
mylist.append(element)
print("Maximum element in the list is :", max(mylist))
print("Minimum element in the list is :", min(mylist))
Output
15. AIM
Create a dictionary with the roll number, name and marks of n students in a class and display the names of students
who have scored marks above 75.
Source Code
Output
16. AIM
Input a list of numbers and swap elements at the even location with the elements at the odd location Python Program
Source Code
# Entering 5 element Lsit
mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
# printing original list
print("The original list : " + str(mylist))
# Separating odd and even index elements
odd_i = []
even_i = []
for i in range(0, len(mylist)):
if i % 2:
even_i.append(mylist[i])
else :
odd_i.append(mylist[i])
result = odd_i + even_i
# print result
print("Separated odd and even index list: " + str(result))
Output
17. AIM
Write a program which reverses a string passed as parameter and stores the reversed string in a new string. Use a user
defined function for reversing the string.
Source Code
def reverseString(st):
newstr = '' #create a new string
length = len(st)
for i in range(-1,-length-1,-1):
newstr += st[i]
return newstr
#end of function
st = input("Enter a String: ")
st1 = reverseString(st)
print("The original String is:",st)
print("The reversed String is:",st1)
Output
18. AIM
Write a program with a user defined function to count the number of times a character (passed as argument) occurs in
the given string.
Source Code
def charCount(ch,st):
count = 0
for character in st:
if character == ch:
count += 1
return count
#end of function
st = input("Enter a string: ")
ch = input("Enter the character to be searched: ")
count = charCount(ch,st)
print("Number of times character",ch,"occurs in the string is:",count)
Output
19. AIM
Write a program to enter names of employees and their salaries as input and store them in a dictionary.
Source Code
num = int(input("Enter the number of employees whose data to be stored: "))
count = 1
employee = dict() #create an empty dictionary
while count <= num:
name = input("Enter the name of the Employee: ")
salary = int(input("Enter the salary: "))
employee[name] = salary
count += 1
print("\n\nEMPLOYEE_NAME\tSALARY")
for k in employee:
print(k,'\t\t',employee[k])
Output