4Variables

What's your favorite color? If you recall from the previous chapter, you can have IDLE output any phrase you want using print(). How about giving that a try now to see your favorite color printed in the interpreter?

>>> print('blue')
blue

Now, what would you do if you were asked to print your favorite color five times in a row? One option would be to create five print() statements that include your favorite color. Although you could do that, after a while you'll start to realize that typing your favorite color over and over again could become pretty tiring, or you may even accidentally spell your favorite color wrong.

Imagine being asked to write your favorite color 20 times or even 100 times! Sure, you could type the same line of code over and over, however, there's a better way to reuse the same word/phrase in your code.

What Is a Variable?

A variable is a name that represents a value, such as a number or piece of text (called a string).

“The syntax code for a variable as a name that represents a value, such as a number or piece of text (called a string).”

When you create a variable, you'll want to choose a name that is unique, is specific, is related to what the value represents, and doesn't start with a number or special character. You'll also want to avoid using keywords that already serve a purpose in Python. To review a list of Python keywords, visit docs.python.org/3/reference/lexical_analysis.html#keywords.

The variable names shown here are examples of ways that you can ...

Get Bite-Size Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.