0% found this document useful (0 votes)
1 views4 pages

Python Practice Programs

The document contains 15 Python practice programs focused on Lists, Tuples, and Sets. Each program demonstrates various operations such as appending, inserting, merging, and counting elements in lists; creating and manipulating tuples; and performing set operations like union and intersection. It serves as a practical guide for learning and applying fundamental Python 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)
1 views4 pages

Python Practice Programs

The document contains 15 Python practice programs focused on Lists, Tuples, and Sets. Each program demonstrates various operations such as appending, inserting, merging, and counting elements in lists; creating and manipulating tuples; and performing set operations like union and intersection. It serves as a practical guide for learning and applying fundamental Python 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

Python Practice Programs

Lists, Tuples & Sets — 15 Programs

Lists

1. List Methods
numbers = list(map(int, input("Enter numbers separated by space: ").split()))
print("Original:", numbers)

val = int(input("Enter value to append: "))


[Link](val)
print("After append:", numbers)

pos = int(input("Enter position to insert at: "))


val = int(input("Enter value to insert: "))
[Link](pos, val)
print("After insert:", numbers)

extra = list(map(int, input("Enter numbers to extend with: ").split()))


[Link](extra)
print("After extend:", numbers)

val = int(input("Enter value to remove: "))


[Link](val)
print("After remove:", numbers)

[Link]()
print("After pop:", numbers)

2. List Functions
marks = list(map(int, input("Enter marks separated by space: ").split()))

print("Length:", len(marks))
print("Maximum:", max(marks))
print("Minimum:", min(marks))
print("Sum:", sum(marks))
print("Average:", sum(marks)/len(marks))

3. Sorting
nums = list(map(int, input("Enter numbers separated by space: ").split()))

print("Original:", nums)
print("Ascending:", sorted(nums))
print("Descending:", sorted(nums, reverse=True))

[Link]()
print("Reversed:", nums)
4. Search an Element
students = input("Enter names separated by space: ").split()
target = input("Enter name to search: ")

if target in students:
print("Element Found")
else:
print("Element Not Found")

5. Count Occurrences
data = list(map(int, input("Enter numbers separated by space: ").split()))
unique = set(data)

for item in unique:


print(item, "->", [Link](item))

6. Merge Two Lists


list1 = input("Enter first list of names: ").split()
list2 = input("Enter second list of names: ").split()

merged_plus = list1 + list2


print("Using +:", merged_plus)

[Link](list2)
print("Using extend():", list1)

Tuples

7. Student Tuple
name = input("Enter name: ")
age = int(input("Enter age: "))
course = input("Enter course: ")
college = input("Enter college: ")

student = (name, age, course, college)

print("Name:", student[0])
print("Age:", student[1])
print("Course:", student[2])
print("College:", student[3])

8. Tuple Functions
roll_numbers = tuple(input("Enter roll numbers separated by space: ").split())

target = input("Enter roll number to count/find index: ")

print("Length:", len(roll_numbers))
print("Maximum:", max(roll_numbers))
print("Minimum:", min(roll_numbers))
print("Count of", target, ":", roll_numbers.count(target))
print("Index of", target, ":", roll_numbers.index(target))

9. Unpacking
name = input("Enter name: ")
roll_no = input("Enter roll number: ")
course = input("Enter course: ")
college = input("Enter college: ")

student_record = (name, roll_no, course, college)

name, roll_no, course, college = student_record

print("Name:", name)
print("Roll No:", roll_no)
print("Course:", course)
print("College:", college)

10. Nested Tuple


n = int(input("Enter number of students: "))

students_data = []
for i in range(n):
name = input(f"Enter name {i+1}: ")
roll_no = input(f"Enter roll number {i+1}: ")
students_data.append((name, roll_no))

students_data = tuple(students_data)

for record in students_data:


for value in record:
print(value)
print("---")

Sets

11. Union, Intersection, Difference, Symmetric Difference


set1 = set(input("Enter first set of names: ").split())
set2 = set(input("Enter second set of names: ").split())

print("Union:", set1 | set2)


print("Intersection:", set1 & set2)
print("Difference (set1 - set2):", set1 - set2)
print("Symmetric Difference:", set1 ^ set2)

12. Set Methods


students = set(input("Enter initial names for the set: ").split())
print("Original:", students)
val = input("Enter a name to add: ")
[Link](val)
print("After add():", students)

extra = input("Enter names to update with: ").split()


[Link](extra)
print("After update():", students)

val = input("Enter a name to remove: ")


[Link](val)
print("After remove():", students)

val = input("Enter a name to discard: ")


[Link](val) # no error even if not present
print("After discard():", students)

13. Subset / Superset


set_a = set(input("Enter first set: ").split())
set_b = set(input("Enter second set: ").split())

print("Is set_a subset of set_b?", set_a.issubset(set_b))


print("Is set_b superset of set_a?", set_b.issuperset(set_a))

14. List to Set (Unique Values)


roll_list = input("Enter roll numbers separated by space: ").split()

unique_rolls = set(roll_list)
print("Unique values:", unique_rolls)

15. Common Elements Between Two Lists


list1 = input("Enter first list of names: ").split()
list2 = input("Enter second list of names: ").split()

set1 = set(list1)
set2 = set(list2)

common = set1 & set2


print("Common elements:", common)

You might also like