w3resource

Python Exercise: Find the length of a set

Python sets: Exercise-15 with Solution

Write a Python program to find the length of a set.

Sample Solution:-

Python Code:

#Create a set
seta = set([5, 10, 3, 15, 2, 20])
#Find the length use len()
print(len(seta))

Sample Output:

6

Pictorial Presentation:

Python Sets: Find the length of a set.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find maximum and the minimum value in a set.
Next: Write a Python program to check if a given value is present in a set or not.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Python: Annotated Assignment Statement

This might not seem as impressive as some other tricks but it's a new syntax that was introduced to Python in recent years and just good to be aware of.

Annotated assignments allow the coder to leave type hints in the code. These don't have any enforcing power at least not yet. It's still nice to be able to imply some type hints and definitely offers more options than only being able to comment regarding expected types of variables.

day: str = 'Monday'
print(day)
lst: list = [1,2,3,4]
print(lst)

Output:

Monday
[1, 2, 3, 4]

Or the same thing in a shorter way:

day= 'Monday' #str
print(day)
lst= [1,2,3,4] # list
print(lst)

Output:

Monday
[1, 2, 3, 4]