Set of odd numbers

Python Practice › Set Programs › Program 44

Set of odd numbers

EasySet Programs · 44 of 48

Write a Python program to create a set of odd numbers from 1 to 20.

Output
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}

Approach

  1. set(range(1, 21, 2)).

Solution

Python
print(set(range(1, 21, 2)))
Output
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Set of even numbers

Python Practice › Set Programs › Program 43

Set of even numbers

EasySet Programs · 43 of 48

Write a Python program to create a set of even numbers from 1 to 20.

Output
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

Approach

  1. A set comprehension.

Solution

Python
print({n for n in range(1, 21) if n % 2 == 0})
Output
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Set to a dictionary

Python Practice › Set Programs › Program 42

Set to a dictionary

EasySet Programs · 42 of 48

Write a Python program to convert a set into a dictionary.

Input
{"a", "b", "c"}
Output
{'a': 0, 'b': 0, 'c': 0}
{'a': 97, 'b': 98, 'c': 99}

Approach

  1. dict.fromkeys(s, value) or a comprehension.

Solution

Python
s = {"a", "b", "c"}print(dict.fromkeys(sorted(s), 0))print({x: ord(x) for x in sorted(s)})
Output
{'a': 0, 'b': 0, 'c': 0}
{'a': 97, 'b': 98, 'c': 99}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

“Index” of an element in a set

Python Practice › Set Programs › Program 41

"Index" of an element in a set

EasySet Programs · 41 of 48

Write a Python program to find the position of an element in a set.

Input
{30, 10, 20}, element 20
Output
[10, 20, 30] 1

Approach

  1. Sets have no index. Sort into a list first, then use index().

Solution

Python
s = {30, 10, 20}ordered = sorted(s)print(ordered, ordered.index(20))
Output
[10, 20, 30] 1
Note

A set is unordered, so a position only means something after you convert it to a sorted list.

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Set elements found in a string

Python Practice › Set Programs › Program 40

Set elements found in a string

EasySet Programs · 40 of 48

Write a Python program to find which elements of a set appear in a string.

Input
{"py", "java", "test"}, "python testing"
Output
['py', 'test']

Approach

  1. [w for w in s if w in text].

Solution

Python
words = {"py", "java", "test"}text = "python testing"print(sorted(w for w in words if w in text))
Output
['py', 'test']

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Intersection of several sets

Python Practice › Set Programs › Program 39

Intersection of several sets

EasySet Programs · 39 of 48

Write a Python program to find the items shared by several sets.

Input
{1, 2, 3}, {2, 3, 4}, {3, 4, 5}
Output
{3}

Approach

  1. set.intersection(*sets).

Solution

Python
sets = [{1, 2, 3}, {2, 3, 4}, {3, 4, 5}]print(set.intersection(*sets))
Output
{3}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Difference with several sets

Python Practice › Set Programs › Program 38

Difference with several sets

EasySet Programs · 38 of 48

Write a Python program to remove from one set the items of several others.

Input
{1, 2, 3, 4, 5}, {2}, {4, 5}
Output
{1, 3}

Approach

  1. difference() accepts several sets.

Solution

Python
print({1, 2, 3, 4, 5}.difference({2}, {4, 5}))
Output
{1, 3}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Create a frozenset

Python Practice › Set Programs › Program 37

Create a frozenset

EasySet Programs · 37 of 48

Write a Python program to create a frozenset and show that it cannot be changed.

Output
frozenset({1, 2, 3})
Error: 'frozenset' object has no attribute 'add'

Approach

  1. frozenset(iterable); it has no add method.

Solution

Python
fs = frozenset([3, 1, 2])print(fs)try:    fs.add(4)except AttributeError as e:    print("Error:", e)
Output
frozenset({1, 2, 3})
Error: 'frozenset' object has no attribute 'add'

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Is a value a frozenset?

Python Practice › Set Programs › Program 36

Is a value a frozenset?

EasySet Programs · 36 of 48

Write a Python program to check whether a value is a frozenset.

Input
frozenset({1, 2}), {1, 2}
Output
frozenset True
set False

Approach

  1. isinstance(x, frozenset).

Solution

Python
for x in [frozenset({1, 2}), {1, 2}]:    print(type(x).__name__, isinstance(x, frozenset))
Output
frozenset True
set False

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Are two sets equal?

Python Practice › Set Programs › Program 35

Are two sets equal?

EasySet Programs · 35 of 48

Write a Python program to check whether two sets are equal.

Input
{1, 2, 3}, {3, 2, 1}
Output
True False

Approach

  1. == compares contents; order does not matter.

Solution

Python
print({1, 2, 3} == {3, 2, 1}, {1, 2} == {1, 2, 3})
Output
True False

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs