Video Pending

Created by Zed A. Shaw Updated 2024-02-17 04:54:36
 

25: Dictionaries and Functions

In this exercise we're going to do something fun by combining functions with dicts. The purpose of this exercise is to confirm that you can combine different things in Python. Combination is a key aspect of programming, and you'll find that many "complex" concepts are nothing more than a combination of simpler concepts.

Step 1: Function Names are Variables

To prepare we first have to confirm that a function's name is just like other variables. Take a look at this code:

def print_number(x):
    print("NUMBER IS", x)

rename_print = print_number
rename_print(100)
print_number(100)

If you run this code you'll see that rename_print does the exact same thing as print_number, and that's because they are the same. The name of a function is the same as a variable, and you can reassign the name to another variable. It's the same as doing this:

x = 10
y = x

Play around with this until you get the idea. Make your own functions and then assign them to new names until you get that idea.

Previous Lesson Next Lesson

Register for Learn Python the Hard Way, 5th Edition (2023-2024)

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.