List, Tuple, Set, Dictionary
Assign natural numbers from 1 to 5 and display the list
lst=[1,2,3,4,5]
print(lst)
Output
[1, 2, 3, 4, 5]
Assign natural numbers from 1 to 5 and the vowels and display the list
lst=[1,2,3,4,5,'a','e','i','o','u']
print(lst)
Output
[1, 2, 3, 4, 5, 'a', 'e', 'i', 'o', 'u']
Assign natural numbers from 1 to 5 and the vowels and display only the vowels from the list
lst=[1,2,3,4,5,'a','e','i','o','u']
print(lst[5:])
Output
['a', 'e', 'i', 'o', 'u']
Assign natural numbers from 1 to 5 and the vowels and display only the natural numbers from the list
lst=[1,2,3,4,5,'a','e','i','o','u']
print(lst[:5])
Output
[1, 2, 3, 4, 5]
Assign name of five pet animals in the list and display the third element present in the list
lst=['cow','dog','cat','goat','hen']
print(lst[2])
Output
cat
Assign name of five pet animals in the list and display the third and fourth element present in the list
lst=['cow','dog','cat','goat','hen']
print(lst[2:4])
Output
['cat', 'goat']
Append three elements in the list and display the list
lst=[]
lst.append(input("Enter no :-"))
lst.append(input("Enter no :-"))
lst.append(input("Enter no :-"))
print(lst)
Output
Enter no :-1
Enter no :-2
Enter no :-3
['1', '2', '3']
Assign natural numbers from 1 to 5 in the list. Insert a new element in the list in the second position and display the list.
lst=[1,2,3,4,5]
n=int(input("Enter number to be inserted"))
lst.insert(1,n)
print(lst)
Output
Enter number to be inserted 10
[1, 10, 2, 3, 4, 5]
Assign natural numbers from 1 to 5 in the list. Delete the last element of the list.
lst=[1,2,3,4,5]
print("Original list ",lst)
lst.pop()
print("Final list after Deletion",lst)
Output
Original list [1, 2, 3, 4, 5]
Final list after Deletion [1, 2, 3, 4]
Assign natural numbers from 1 to 5 in the list. Delete the element from the given position of the list.
lst=[1,2,3,4,5]
print("Original list ",lst)
n=int(input("Enter the position"))
lst.pop(n-1)
print("Final list after Deletion",lst)
Output
Original list [1, 2, 3, 4, 5]
Enter the position 3
Final list after Deletion [1, 2, 4, 5]
Accept six elements in the list and display the list.
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
print(lst)
Output
Enter no :-1
Enter no :-2
Enter no :-q
Enter no :-r
Enter no :-"cat"
Enter no :-"dog"
['1', '2', 'q', 'r', '"cat"', '"dog"']
Accept six elements in the list and display the list in the reverse order as inputted
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
print("Original list ",lst)
lst.reverse()
print("Reversed list ",lst)
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Enter no :-6
Original list ['1', '2', '3', '4', '5', '6']
Reversed list ['6', '5', '4', '3', '2', '1']
Accept six elements in the list and display the list in the ascending sorted order
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
print("Original list ",lst)
lst.sort()
print("Sorted list ",lst)
Output
Enter no :-5
Enter no :-6
Enter no :-3
Enter no :-4
Enter no :-8
Enter no :--4
Original list ['5', '6', '3', '4', '8', '-4']
Sorted list ['-4', '3', '4', '5', '6', '8']
Accept six elements in the list and display the list in the descending sorted order
lst=[]
lst1=[]
for i in range(6):
lst.append(input("Enter no :-"))
print("Original list ",lst)
lst.sort(reverse=True)
print("Descending list",lst)
Output
Enter no :-6
Enter no :-7
Enter no :-1
Enter no :-2
Enter no :-4
Enter no :-6
Original list ['6', '7', '1', '2', '4', '6']
Descending list ['7', '6', '6', '4', '2', '1']
Accept six elements in the list and display the first three element of the list.
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
for i in range(3):
print(lst[i])
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Enter no :-6
1
2
3
Accept six elements in the list and display the last three element of the list.
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
for i in range(5,2,-1):
print(lst[i])
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Enter no :-6
6
5
4
Sum of all the elements accepted in a list. The size of the list is five.
s=0
lst=[]
for i in range(5):
lst.append(int(input("Enter no :-")))
for i in range(5):
s=s+lst[i]
print("Sum of Array elements ",s)
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Sum of List elements 15
Sum of Odd and Even elements present in the List. The size of the list is five.
s1=s2=0
lst=[]
for i in range(5):
lst.append(int(input("Enter no :-")))
for i in range(5):
if lst[i]%2 == 0 :
s1=s1+lst[i]
else :
s2=s2+lst[i]
print("Sum of Even elem ",s1)
print("Sum of Odd elem ",s2)
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Sum of Even elem 6
Sum of Odd elem 9
Sum of Odd and Even position present in the List. The size of the list is five.
s1=s2=0
lst=[]
for i in range(5):
lst.append(int(input("Enter no :-")))
for i in range(5):
if i%2 == 0 :
s1=s1+lst[i]
else :
s2=s2+list[i]
print("Sum - Even position ",s1)
print("Sum - Odd position ",s2)
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Sum - Even position 9
Sum - Odd position 6
Accept six elements in the list and find the largest and smallest element present in the list.
a=[]
for i in range(6):
a.append(int(input("Enter no :-")))
l=a[0]
s=a[0]
for i in range(1,6,1):
if a[i]>l:
l=a[i]
if a[i]<s:
s=a[i]
print("Largest ",l)
print("Smallest ",s)
Output
Enter no :-6
Enter no :-8
Enter no :-1
Enter no :-2
Enter no :-5
Enter no :-4
Largest 8
Smallest 1
Accept five elements in list A and three elements in list B and merge them to a single list one after the another
lst=[]
lst1=[]
lst2=[]
for i in range(5):
lst.append(int(input("Enter no :-")))
for i in range(5):
lst1.append(int(input("Enter no :-")))
lst2=lst+lst1
print(lst2)
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Enter no :-6
Enter no :-7
Enter no :-8
Enter no :-9
Enter no :-10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Accept six elements in the list and a search element form the user and find whether the element is present in the list by linear search method.
c=0
lst=[]
for i in range(6):
lst.append(int(input("Enter no :-")))
se=int(input("Search Element"))
for i in range(6) :
if lst[i] == se :
c=c+1
if(c>0):
print("Search Found ")
else :
print("Search Not Found ")
Output
Enter no :-4
Enter no :-5
Enter no :-8
Enter no :-9
Enter no :-3
Enter no :-7
Search Element 3
Search Found
Accept six elements in the list and a search element form the user and find whether the element is present in the list by binary search method.
c=0
lst=[]
for i in range(6):
lst.append(int(input("Enter no :-")))
se=int(input("Search Element"))
l=0
h=5
while l<=h:
m=(l+h)//2
if lst[m] == se :
c=c+1
break;
if lst[m] < se :
l=m+1
if lst[m] > se :
h=m-1
if(c>0):
print("Search Found ")
else :
print("Search Not Found ")
Find the frequency of the given number in the list.
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
se=input("Enter Search Element")
for i in range(6):
if se==lst[i]:
print("Position of Element",i)
Accept six elements in the list and sort the elements in ascending order by bubble sort method.
lst=[]
for i in range(6):
lst.append(int(input("Enter no :-")))
print("Org Lst",lst)
for i in range(5):
for j in range(0,6-i-1,1):
if lst[j]>lst[j+1]:
t=lst[j]
lst[j]=lst[j+1]
lst[j+1]=t
print("Sorted Lst",lst)
Accept six elements in the list and sort the elements in descending order by bubble sort method.
lst=[]
for i in range(6):
lst.append(int(input("Enter no :-")))
print("Org Lst",lst)
for i in range(5):
for j in range(0,6-i-1,1):
if lst[j]<lst[j+1]:
t=lst[j]
lst[j]=lst[j+1]
lst[j+1]=t
print("Sorted Lst",lst)
lst=[1,2,3,4,5]
print(lst)
Output
[1, 2, 3, 4, 5]
Assign natural numbers from 1 to 5 and the vowels and display the list
lst=[1,2,3,4,5,'a','e','i','o','u']
print(lst)
Output
[1, 2, 3, 4, 5, 'a', 'e', 'i', 'o', 'u']
Assign natural numbers from 1 to 5 and the vowels and display only the vowels from the list
lst=[1,2,3,4,5,'a','e','i','o','u']
print(lst[5:])
Output
['a', 'e', 'i', 'o', 'u']
Assign natural numbers from 1 to 5 and the vowels and display only the natural numbers from the list
lst=[1,2,3,4,5,'a','e','i','o','u']
print(lst[:5])
Output
[1, 2, 3, 4, 5]
Assign name of five pet animals in the list and display the third element present in the list
lst=['cow','dog','cat','goat','hen']
print(lst[2])
Output
cat
Assign name of five pet animals in the list and display the third and fourth element present in the list
lst=['cow','dog','cat','goat','hen']
print(lst[2:4])
Output
['cat', 'goat']
Append three elements in the list and display the list
lst=[]
lst.append(input("Enter no :-"))
lst.append(input("Enter no :-"))
lst.append(input("Enter no :-"))
print(lst)
Output
Enter no :-1
Enter no :-2
Enter no :-3
['1', '2', '3']
Assign natural numbers from 1 to 5 in the list. Insert a new element in the list in the second position and display the list.
lst=[1,2,3,4,5]
n=int(input("Enter number to be inserted"))
lst.insert(1,n)
print(lst)
Output
Enter number to be inserted 10
[1, 10, 2, 3, 4, 5]
Assign natural numbers from 1 to 5 in the list. Delete the last element of the list.
lst=[1,2,3,4,5]
print("Original list ",lst)
lst.pop()
print("Final list after Deletion",lst)
Output
Original list [1, 2, 3, 4, 5]
Final list after Deletion [1, 2, 3, 4]
Assign natural numbers from 1 to 5 in the list. Delete the element from the given position of the list.
lst=[1,2,3,4,5]
print("Original list ",lst)
n=int(input("Enter the position"))
lst.pop(n-1)
print("Final list after Deletion",lst)
Output
Original list [1, 2, 3, 4, 5]
Enter the position 3
Final list after Deletion [1, 2, 4, 5]
Accept six elements in the list and display the list.
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
print(lst)
Output
Enter no :-1
Enter no :-2
Enter no :-q
Enter no :-r
Enter no :-"cat"
Enter no :-"dog"
['1', '2', 'q', 'r', '"cat"', '"dog"']
Accept six elements in the list and display the list in the reverse order as inputted
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
print("Original list ",lst)
lst.reverse()
print("Reversed list ",lst)
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Enter no :-6
Original list ['1', '2', '3', '4', '5', '6']
Reversed list ['6', '5', '4', '3', '2', '1']
Accept six elements in the list and display the list in the ascending sorted order
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
print("Original list ",lst)
lst.sort()
print("Sorted list ",lst)
Output
Enter no :-5
Enter no :-6
Enter no :-3
Enter no :-4
Enter no :-8
Enter no :--4
Original list ['5', '6', '3', '4', '8', '-4']
Sorted list ['-4', '3', '4', '5', '6', '8']
Accept six elements in the list and display the list in the descending sorted order
lst=[]
lst1=[]
for i in range(6):
lst.append(input("Enter no :-"))
print("Original list ",lst)
lst.sort(reverse=True)
print("Descending list",lst)
Output
Enter no :-6
Enter no :-7
Enter no :-1
Enter no :-2
Enter no :-4
Enter no :-6
Original list ['6', '7', '1', '2', '4', '6']
Descending list ['7', '6', '6', '4', '2', '1']
Accept six elements in the list and display the first three element of the list.
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
for i in range(3):
print(lst[i])
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Enter no :-6
1
2
3
Accept six elements in the list and display the last three element of the list.
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
for i in range(5,2,-1):
print(lst[i])
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Enter no :-6
6
5
4
Sum of all the elements accepted in a list. The size of the list is five.
s=0
lst=[]
for i in range(5):
lst.append(int(input("Enter no :-")))
for i in range(5):
s=s+lst[i]
print("Sum of Array elements ",s)
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Sum of List elements 15
Sum of Odd and Even elements present in the List. The size of the list is five.
s1=s2=0
lst=[]
for i in range(5):
lst.append(int(input("Enter no :-")))
for i in range(5):
if lst[i]%2 == 0 :
s1=s1+lst[i]
else :
s2=s2+lst[i]
print("Sum of Even elem ",s1)
print("Sum of Odd elem ",s2)
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Sum of Even elem 6
Sum of Odd elem 9
Sum of Odd and Even position present in the List. The size of the list is five.
s1=s2=0
lst=[]
for i in range(5):
lst.append(int(input("Enter no :-")))
for i in range(5):
if i%2 == 0 :
s1=s1+lst[i]
else :
s2=s2+list[i]
print("Sum - Even position ",s1)
print("Sum - Odd position ",s2)
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Sum - Even position 9
Sum - Odd position 6
Accept six elements in the list and find the largest and smallest element present in the list.
a=[]
for i in range(6):
a.append(int(input("Enter no :-")))
l=a[0]
s=a[0]
for i in range(1,6,1):
if a[i]>l:
l=a[i]
if a[i]<s:
s=a[i]
print("Largest ",l)
print("Smallest ",s)
Output
Enter no :-6
Enter no :-8
Enter no :-1
Enter no :-2
Enter no :-5
Enter no :-4
Largest 8
Smallest 1
Accept five elements in list A and three elements in list B and merge them to a single list one after the another
lst=[]
lst1=[]
lst2=[]
for i in range(5):
lst.append(int(input("Enter no :-")))
for i in range(5):
lst1.append(int(input("Enter no :-")))
lst2=lst+lst1
print(lst2)
Output
Enter no :-1
Enter no :-2
Enter no :-3
Enter no :-4
Enter no :-5
Enter no :-6
Enter no :-7
Enter no :-8
Enter no :-9
Enter no :-10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Accept six elements in the list and a search element form the user and find whether the element is present in the list by linear search method.
c=0
lst=[]
for i in range(6):
lst.append(int(input("Enter no :-")))
se=int(input("Search Element"))
for i in range(6) :
if lst[i] == se :
c=c+1
if(c>0):
print("Search Found ")
else :
print("Search Not Found ")
Output
Enter no :-4
Enter no :-5
Enter no :-8
Enter no :-9
Enter no :-3
Enter no :-7
Search Element 3
Search Found
Accept six elements in the list and a search element form the user and find whether the element is present in the list by binary search method.
c=0
lst=[]
for i in range(6):
lst.append(int(input("Enter no :-")))
se=int(input("Search Element"))
l=0
h=5
while l<=h:
m=(l+h)//2
if lst[m] == se :
c=c+1
break;
if lst[m] < se :
l=m+1
if lst[m] > se :
h=m-1
if(c>0):
print("Search Found ")
else :
print("Search Not Found ")
Find the frequency of the given number in the list.
lst=[]
for i in range(6):
lst.append(input("Enter no :-"))
se=input("Enter Search Element")
for i in range(6):
if se==lst[i]:
print("Position of Element",i)
Accept six elements in the list and sort the elements in ascending order by bubble sort method.
lst=[]
for i in range(6):
lst.append(int(input("Enter no :-")))
print("Org Lst",lst)
for i in range(5):
for j in range(0,6-i-1,1):
if lst[j]>lst[j+1]:
t=lst[j]
lst[j]=lst[j+1]
lst[j+1]=t
print("Sorted Lst",lst)
Accept six elements in the list and sort the elements in descending order by bubble sort method.
lst=[]
for i in range(6):
lst.append(int(input("Enter no :-")))
print("Org Lst",lst)
for i in range(5):
for j in range(0,6-i-1,1):
if lst[j]<lst[j+1]:
t=lst[j]
lst[j]=lst[j+1]
lst[j+1]=t
print("Sorted Lst",lst)
No comments:
Post a Comment