Edward Can Remember!

Give Edward the ability to remember with the help of variables.

We saw that Edward can communicate with us by printing information of each block in his path. As he moves to a new block and we click the print_info() button, it prints the state of that block.

Edward with memory

With all the features that he has, is he even aware of what he can do? Once he moves on to a new block, does he remember whether he already potted a plant or removed trash before? He has no way of knowing it! He can’t remember if he has even done any work. If he had a way to store this information somewhere, he would have a purpose. Say, he knows he needs to pot at least one plant and remove trash. But right now he can’t know if he has already done that or not.

So how about we program Edward to remember as well? It would be like giving Edward the ability to memorize.

Look at the widget below, where we see a block called the “Memory Box” that depicts how Edward can remember if he previously potted any plant or removed trash. Try taking Edward to the trash, remove the trash, and place a plant. Move Edward to any other part of the world. Notice the change in Edward’s memory along each step.

Variables in Python

We’ve achieved the above using a programming element called variables. A variable is a named memory location where we can store data (e.g., a piece of text or a number).

In the widget above, trash_removed and plant_placed are variables that help Edward remember by storing the answers in them. If the trash has been removed, the variable trash_removed stores True and vice versa. In case he has placed a plant, Edward sets the plant_placed variable to True. This way Edward can keep track of the states of things.

Variable naming

Choosing the name of the variable is the programmer’s choice. Our variable names must be meaningful so that anyone reading our code can easily understand what the variables contain. For example, name could be used to store someone’s name, and age could store their age.

Variables are case-sensitive in Python, i.e., it makes a difference to Python if the letters of our variables are in upper or lower case. For example, ageAge, and AGE are considered three different variables. Whatever choice we make, it is important to be consistent throughout our code wherever that variable is used.

Say the age is 12, how do we assign the variable age the value 12?

Assigning a value to a variable

We can assign a value to a variable using the assignment operator = (equal sign). For example, we can assign the value 5 to a variable called num.

Python 3.10.4
num = 5

We can also assign the address 12280 NE District Way, Bellevue, WA 98005 to a variable called address. Once we’ve assigned the value to a variable, we can use this variable however many times in our code.

Python 3.10.4
address = "12280 NE District Way, Bellevue, WA 98005"

The variable will always come on the left-hand side of the assignment operator =.

Printing a variable

We assigned the address to variable address, but how can we verify whether it has been assigned or not? How do we know what is the value residing inside our variable? Surely, we can print the following:

Python 3.10.4
address = "12280 NE District Way, Bellevue, WA 98005"
print(address)

Now say we wanted to update the address to some other address, we can do that too by updating the same variable’s value. Look at the example below.

Python 3.10.4
print("Before updating: ")
address = "12280 NE District Way, Bellevue, WA 98005"
print(address)
print("After updating the same variable: ")
address = "123 Main St., Springfield, IL 62701, United States"
print(address)

That’s it! We’ve learned how important a variable is. But before we wrap up the concept, we’d like to highlight an important distinction here: printing a piece of text is different than printing a variable name. Look at the code below to understand what we mean.

Python 3.10.4
address = "12280 NE District Way, Bellevue, WA 98005"
print(address)
print("address")

Printing the variable name prints the contents of the variable, while printing text directly just outputs the text inside the print() function.

In your own words

Refer to the code below, and answer the questions that follow in your own words below to check your understanding of the concept of variables that you’ve learned so far.

We want to swap the values of player1_score and player2_score so that player1_score will be 300 and player2_score will be 150. If we do what we’ve done in lines 6–7, run the code to see the effect on the values of player1_score and player2_score.

Python 3.10.4
# We have two variables with the following initial scores
player1_score = 150
player2_score = 300
# Swapping scores
player1_score = player2_score
player2_score = player1_score
# After swapping:
print(player1_score)
print(player2_score)

In the notepad widget below, answer the following questions:

  1. What is happening in the code above, and in your opinion, why does this approach not work?

  2. How can you correctly swap the values of player1_score and player2_score?

Saved
Knowledge check on variables

Recap

Key takeaways:

  1. Variables: Variables are like containers that hold information in a program. They have names (like name, age, no_of_players, my_fav_quote, house_no) and can store different types of data, such as numbers, text, and yes-or-no values. Note that a variable name cannot have a space in it.

  2. Why use variables: Variables help us store data in our programs. They make our code more flexible and easier to understand. Instead of using the data directly, we can use the variable name to refer to it.

  3. Assigning values to variables: To assign a value to a variable, we use the equal sign (= known as assignment operator). For example, num = 10 means we’re storing the number 10 in the variable num.

  4. Naming variables: Choosing meaningful and descriptive names for variables is a good programming practice. It makes the code more readable and helps convey the purpose of the variable.

  5. Printing variables: We can see the value stored in a variable by using the print() function. For example, print(name) would show the value stored in the name variable.