Working with data

Day 11: Exercise Solutions

Python Guru with a screen instead of a face, typing on a computer keyboard with a light green background to match the day 11 image.

Here are our solutions for the day 11 exercises in the 30 Days of Python series. Make sure you try the exercises yourself before checking out the solutions!

1) Create an empty set and assign it to a variable.

Remember that we can't create an empty set using {}, because Python treats this as an empty dictionary.

Instead, we have to write set().

Let's just call our set s for now:

s = set()

2) Add three items to your empty set using either several add calls, or a single call to update.

I'm just going to add numbers to our set. Using the add method, we could do something like this.

s = set()

s.add(1)
s.add(2)
s.add(3)

Or we could use a loop to grab items from a range object:

s = set()

for number in range(1, 4):
    s.add(number)

We could also use the update method like so:

s = set()
s.update(range(1, 4))

These are all basically equivalent.

3) Create a second set which includes at least one common element with the first set.

For this second set, I'm going to include several values of different types:

random_values = {"r", 1, ("Python", "C", "Rust")}

4) Find the union, symmetric difference, and intersection of the two sets. Print the results of each operation.

For this exercise, I'm just going to print the results of the operations directly, since each method call will give us a new set.

s = set()
s.update(range(1, 4))

random_values = {"r", 1, ("Python", "C", "Rust")}

print(s.union(random_values))
print(s.symmetric_difference(random_values))
print(s.intersection(random_values))

The output in my case is:

{1, 2, 3, ('Python', 'C', 'Rust'), 'r'}
{2, 3, 'r', ('Python', 'C', 'Rust')}
{1}

5) Create a sequence of numbers using range, then ask the user to enter a number. Inform the user whether or not their number was within the range you specified.

In this case, I'm going to use range(27, 54), which includes all the integers from 27 to 53. The specific range doesn't matter all that much.

numbers = range(27, 54)

Now we need to get the user to enter a number. Remember that this number is going to come back in string form, so we're going to need to convert it to an integer.

numbers = range(27, 54)
user_number = int(input("Enter a number: "))

Now we just need to use the in keyword to verify if the number is in this range:

numbers = range(27, 54)
user_number = int(input("Enter a number: "))

if user_number in numbers:
    print("Your number is in the valid range.")
else:
    print("Your number is outside of the valid range.")

The extension task is telling the user if their guess was too high or too low.

If the user's number isn't within the specified series of numbers, we can check the user's number against the first value in the range. If it's lower than this number, the user's value was too low. Otherwise it must have exceeded the highest value in the range.

numbers = range(27, 54)
user_number = int(input("Enter a number: "))

if user_number in numbers:
    print("Your number is in the valid range.")
else:
    if user_number < numbers[0]:
        print("Your number is too low.")
    else:
        print("Your number is too high.")