IKH

Set comprehension

  • Set comprehension is possible.

Example

Python
s={x*x for x in range(5)}
print(s)

Output

PowerShell
{0, 1, 4, 9, 16}

Example

Python
s={2**x for x in range(2,10,2)}
print(s)

Output

PowerShell
{16, 256, 64, 4}
  • set objects won’t support indexing and slicing.

Example

Python
s={10,20,30,40}
print(s[0])
print(s[1:3])

Output

PowerShell
TypeError: 'set' object is not subscriptable

Question

Write a program to eliminate duplicates present in the list?

Python
l=eval(input("Enter List of values: "))
s=set(l)
print(s)

Output

PowerShell
Enter List of values: [10,20,30,10,20,40]
{40, 10, 20, 30}

2nd Method

Python
l=eval(input("Enter List of values: "))
l1=[]
for x in l:
    if x not in l1:
        l1.append(x)
print(l1)

Output

PowerShell
Enter List of values: [10,20,30,10,20,40]
[10, 20, 30, 40]

Question

Write a program to print different vowels present in the given word?

Python
w=input("Enter word to search for vowels: ")
s=set(w)
v={'a','e','i','o','u'}
d=s.intersection(v)
print("The different vowel present in",w,"are",d)

Output

PowerShell
Enter word to search for vowels: Dictionary
The different vowel present in Dictionary are {'a', 'o', 'i'}

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error