0% found this document useful (0 votes)
4 views3 pages

Operations With Tuples and Sets in Python

This code demonstrates basic operations on tuples and sets in Python.

Uploaded by

llavadomau29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Operations With Tuples and Sets in Python

This code demonstrates basic operations on tuples and sets in Python.

Uploaded by

llavadomau29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

# 1.

Tuple of fruits

fruits = ("Mango", "Apple", "Orange", "Banana", "Pineapple")

print("First:", fruits[0])

print("Last:", fruits[len(fruits) - 1])

# 2. Length and sum of numbers

numbers = (5, 10, 15, 20, 25, 30)

print("Length =", len(numbers))

print("Sum =", sum(numbers))

# 3. Convert to list, add 50, back to tuple

data = (10, 20, 30, 40)

new_data = list(data)

new_data.append(50)

data = tuple(new_data)

print("Updated:", data)

# 4. Set operations

setA = {1, 2, 3, 4, 5}

setB = {4, 5, 6, 7, 8}

print("Union:", setA.union(setB))

print("Intersection:", setA.intersection(setB))

print("Difference:", setA.difference(setB))
# 5. Manipulate student set

students = {"Alice", "Bob", "Charlie"}

students.add("Diana") # Add new

students.discard("Bob") # Remove safely

message = "Charlie is in the set." if "Charlie" in students else "Charlie is not in the set."

print(message)

print("Students:", students)

# 6. List operations

nums = [1, 2, 3, 4, 5]

nums.append(100) # Add at end

del nums[1] # Remove 2nd element

nums.sort()

print("Final list:", nums)

# 7. User input for list

values = [int(input("Enter number: ")) for _ in range(5)]

print("Largest:", max(values))

print("Smallest:", min(values))

print("Average:", sum(values) / 5)

items = [10, 20, 20, 30, 40, 10, 50, 60, 60]

# Step 1: Convert list -> set


unique = set(items)

# Step 2: Convert set -> list

unique_list = sorted(list(unique))

# Step 3: Print

print("Sorted unique list:", unique_list)

# Step 4: Convert to tuple

final_tuple = tuple(unique_list)

print("Final tuple:", final_tuple)

You might also like