Now that we can make Python display messages, let's look at another key element of programming; remembering data. In math, we learned that the letter x, or any letter, could represent a number. If we set x to 7, we could "remember" that value and carry it out into any equation such as y = x + 3. This is a valuable thing in programming!

Variables are similar on Python. Variables are simply a name we can assign to Python so it associates the information with 

 

Program 2.1.1 - Storing a Name

#creates a variable to store information
firstname = "Brian"

 

You can try running this program to see what happens, but nothing will happen from your perspective. The only thing we did was ask Python to remember that the variable firstname is holding "Brian". When we ran the program, it did exactly that. It remembered.

So your computer is keeping that association, but is not doing anything with it. Let's make that variable display in Python:

#creates a variable to store information
firstname = "Brian"
#displays the text inside of the variable firstname
print(firstname)

 

Now that we asked Python to print the association made with the variable, our text will show up:

 

This is how we make variables appear. In something like a video game when you choose your character's name, this is how the system remembers and displays your character's name. But what if we wanted to say hello to a person?

 

Program 2.1.2 - Combine Text with Variables

Let's make that firstname variable appear with the text "hello" before it.

#creates a variable to store information
firstname = "Brian"
#displays the text inside of the variable firstname
print("Hello"+firstname)

 

 

Okay, that was easy. However, the spacing was off. There are two ways of fixing this:

#creates a variable to store information
firstname = "Brian"
#displays the text inside of the variable firstname, SPACE AFTER THE WORLD HELLO
print("Hello " + firstname)

In this example I added a space after Hello but inside the quotes. I asked Python to actually print a space character to put some distance between the text and the variable.

or

#creates a variable to store information
firstname = "Brian"
#displays the text inside of the variable firstname, COMMA AFTER "HELLO"
print("Hello",firstname)

In this example, I used the comma. The comma can separate two different entities and a space is automatically added. We will see more of this in this chapter when trying to combine different types of data together. Right now we are only using regular old text.

Either method works just fine for now.

 

Program 2.1.3 - Combining All Sorts of Stuff

#creates a variable to store information
firstname = "Brian"
#adding a second variable for last name
lastname = "Fediuk"
#displays the text inside of the variable firstname
print("Hello "+firstname + " " + lastname)

By combining multiple variables together and proper spacing (either way works), we can build a sentence or complex print statement that incorporates multiple variables. There is no limit to what you can build!

 

 

Program 2.1.4 - Replacing Variables

It is also worth noting that you can "write over" variables if they appear later on.

 

#creates a variable to store information
firstname = "Brian"
#replace firstname variable contents
firstname = "John"
#displays the text inside of the variable firstname
print("Hello "+firstname)

 

 

 

Assignments for this Chapter:

  • Create variables for a person's first name, last name, street address, city, and zip code. Format the data so it displays nicely, on separate lines:

 

Chapter 2.2 - Advanced Use of Variables: Data Types