{}
See how a CS professor is using our compiler for class assignment.
Try Programiz PRO for Educators!
Learn DSA with step-by-step code visualization.
Try Programiz PRO for Educators!
run-icon
main.py
# Online Python compiler (interpreter) to run Python online. # Write Python 3 code in this online editor and run it. # ===== Set Operations with it_companies ===== it_companies = {'Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'} # 1. Length of the set print("Length of it_companies:", len(it_companies)) # 2. Add 'Twitter' it_companies.add('Twitter') print("After adding 'Twitter':", it_companies) # 3. Insert multiple IT companies it_companies.update(['TCS', 'Infosys', 'Wipro']) print("After adding multiple companies:", it_companies) # 4. Remove a company (e.g., 'IBM') it_companies.remove('IBM') # Raises error if 'IBM' not present print("After removing 'IBM':", it_companies) # 5. Difference between remove and discard # remove() throws an error if item doesn't exist # discard() does not throw an error it_companies.discard('NonExistentCompany') # No error print("After discard (safe remove):", it_companies) # ===== Level 2 Exercises ===== A = {19, 22, 24, 20, 25, 26} B = {19, 22, 20, 25, 28, 27} # 1. Join A and B print("A union B:", A.union(B)) # 2. A intersection B print("A intersection B:", A.intersection(B)) # 3. Is A subset of B? print("Is A subset of B?", A.issubset(B)) # 4. Are A and B disjoint? print("Are A and B disjoint?", A.isdisjoint(B)) # 5. Join A with B and B with A print("A union B:", A.union(B)) print("B union A:", B.union(A)) # 6. Symmetric difference between A and B print("Symmetric difference:", A.symmetric_difference(B)) # 7. Delete sets A and B del A del B # ===== Level 3 Exercises ===== # 1. Compare list and set lengths ages = [22, 19, 24, 25, 26, 24, 25, 24] ages_set = set(ages) print("Length of ages list:", len(ages)) print("Length of ages set:", len(ages_set)) if len(ages) > len(ages_set): print("The list is longer (has duplicates).") else: print("The set is longer or equal (unique values).") # 2. Difference between string, list, tuple, set print("\n--- Data Type Differences ---") print("String: Ordered, immutable text. e.g., 'Hello'") print("List: Ordered, mutable, allows duplicates. e.g., [1, 2, 3]") print("Tuple: Ordered, immutable, allows duplicates. e.g., (1, 2, 3)") print("Set: Unordered, mutable, no duplicates. e.g., {1, 2, 3}") # 3. Count unique words in a sentence sentence = "I am a teacher and I love to inspire and teach people." words = sentence.lower().replace(".", "").split() unique_words = set(words) print("\nUnique words in sentence:", unique_words) print("Number of unique words:", len(unique_words))
Output