In this article List Examples in Python we give the information about Basic example of list, tuple, dictionary and set with output.

List Examples in Python :

1. Write a program to store five fruits in a list entered by the user.

Ans:-

fruits = []

f1 = input(“Enter the 1st fruit name: “)

fruits.append(f1)

f2 = input(“Enter the 2nd fruit name: “)

fruits.append(f2)

f3 = input(“Enter the 3rd fruit name: “)

fruits.append(f3)

f4 = input(“Enter the 4th fruit name: “)

fruits.append(f4)

print(fruits)

#Using for loop:

fruits = []
for i in range(5):
    f = input(“Enter the fruit names: “)
    fruits.append(f)
print(fruits)

2. Write a program to accept marks of 4 students and display them in a sorted

Ans:-

marks = []

f1 = int(input(“Enter the 1st subject marks: “))

marks.append(f1)

f2 = int(input(“Enter the 2nd subject marks: “))

marks.append(f2)

f3 = int(input(“Enter the 3rd subject marks: “))

marks.append(f3)

f4 = int(input(“Enter the 4th subject marks: “))

marks.append(f4)

marks.sort()

print(marks)

# Using for loop:

# Accept marks of 4 students
marks = []
for i in range(4):
    mark = int(input(f”Enter marks for student {i+1}: “))
    marks.append(mark)
# Sort the marks in ascending order
for i in range(len(marks)):
    for j in range(i + 1, len(marks)):
        if marks[i] > marks[j]:
            marks[i], marks[j] = marks[j], marks[i]
# Display sorted marks
print(“Sorted marks:”)
for mark in marks:
    print(mark)

3. Check that a tuple cannot be changed in python.

Ans:-

a=(55,”Sham”,1.8, 46,”Yash”)

a[4]=”Ganesh”

O/P: – TypeError: ‘tuple’ object does not support item assignment

4. Write a program to sum a list with 5 numbers.

 Ans:-

Compile Time:

l=[1,5,2,4,7]

print(sum(l))

Runtime:

l=[]

n1=(int(input(“Enter the 1st number: “)))

l.append(n1)

n2=(int(input(“Enter the 2nd number: “)))

l.append(n2)

n3=(int(input(“Enter the 3rd number: “)))

l.append(n3)

n4=(int(input(“Enter the 4th number: “)))

l.append(n4)

n5=(int(input(“Enter the 5th number: “)))

l.append(n5)

print(sum(l))

# Using for loop:

# Define the list of numbers
numbers = [10, 20, 30, 40, 50]

# Initialize the sum variable
total = 0

# Iterate through the list and sum the numbers
for num in numbers:
total += num

# Print the result
print(“Sum of the list:”, total)

5. Write a program to count the number of zeros in the following tuple.

 A= (7, 8, 0, 8, 8, 0, 9, 9, 0)

 Ans.:-

A= (7, 8, 0, 8, 8, 0, 9, 9, 0)

n=A.count(0)

print(n)

6. Write a program to create a dictionary of Marathi words with values as their English translation. Provide user with an option to look it up!

 Ans.:-

words= {

“Aai”:”Mother”,

“Baba”: “Father”,

“Dada”: “Brother”,

“Kaka”: “Uncle”

}

word=input(“Enter the word you want meaning of : “)

print(words[word])

7. Write a program to show empty set, list, tuple and dict.

 Ans.:-

s=set()

print(type(s))

li=[]

print(type(li))

a=()

print(type(a))

p={}

print(type(p))

8. Write a program to input ten numbers from the user and display all the unique numbers (once)

 Ans.:-

s=set()

for n in range(10):

n=int(input(“Enter the numbers: “))

s.add(n)

print(s)

9. Can we have a set with 12 (int) and ‘12’ (str) as a value in it?

Ans:-

s=set()

s.add(12)

s.add(“12”)

print(s)

O/P:

Yes

{12, ’12’}

10. What will be the length of following set’s:

s= set()

s.add(15)

s.add(15.0)

s.add(’15’) # length of s after these operations?

print(s)

print(len(s))

O/P:

{’15’, 15}
2

11. # Create an empty dictionary. Allow 5 friends to enter their favorite language

# as value and use key as the names. 

Ans:

d={}

for i in range(5):

name=input(“Enter friends name: “)

lang=input(“Enter language name: “)

d.update({name:lang})

print(d)

12. If the names of 3 friends as same, what will happen to the above program?

# The values entered later will be updated

13. It languages of three friends are same, want will happen to the above program?

# Nothing will happen. the value can be same

14. Can you change the values inside a list which is contained in set  S?

S={6,3,1,8,”Jay”, [4,5]}

Ans: 

No, you cannot change the values inside a list that is contained in a set because a set cannot contain a list in the first place.

Reasons:

  1. Sets in Python only store immutable (hashable) objects.
  2. Lists are mutable, meaning their contents can be changed, so they are unhashable and cannot be added to a set.
Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *