Python Variable Assignment Statements and Naming Rules & Conventions

Python Variable Assignment Statements and Naming Rules & Conventions

Think of a number. Any number. Now, before you forget that number, let’s store it for later. When you think of a number, you are holding that value in your head. If you want to remember it later and your memory is like mine, you write it down on a piece of paper. And if it’s really important, you will put it in a safe place. In computer science, that safe place is a variable. They’re called variables because, well, they’re “capable of being varied or changed”. You can name a variable (almost) anything you want and you can change the value willy-nilly.

I thought of the number 5, so at the Python prompt, I will create a variable with the clever name “number”, and enter my value:

Now stop thinking of that number. We’ll come back to it later. If you’re just joining us, you may want to start at the beginning with our introduction to the Python programming language.

Python Variable Assignment Statements

In the above illustration, Attlee, Truman, and Stalin are each thinking of a number. We can treat their names as variables and their respective numbers as values, like so:

This is called an assignment statement. We use the equal sign (=) to assign a value to a variable. It’s like saying, “Attlee is 7”. If I want to know what number Attlee is thinking about, I simply enter his name at the prompt:

Now it’s your turn. Try it with the other heads of state.

Stalin is being a wisenheimer. ‘a number’ is a string, not a number, and we haven’t covered strings yet. You can change the value of a variable by assigning it a new value. Let’s reassign Stalin’s value to a float. How about pi?

My head is too full of values to remember anything more than 3.14. Fortunately, we can easily assign the value of pi to a variable using the math module.

As easy as pie.

Variable Naming Conventions and Rules

You may be familiar with algebraic equations, such as the quadratic:

ax² + bx + c = 0

In mathematics, variables are generally single letters like x, y, and z, or Greek symbols like π or θ. Mathematicians often use variables when they don’t know a specific value but are working towards finding it. It’s different in Python. You must assign a value to a variable before you can use it, even if that value is zero or empty. If, for example, I call the variable Guido before assigning it a value:

I will get the following error:

Variable assignment works left to right.

Is acceptable. As is:

And even:

But the following will give you a nasty error.

The first two gave you an error that read:

But the last one gave you an error that read:

That’s because False is a reserved word in Python. It’s what computer scientists refer to as a Boolean value, and you can’t use it as a variable identifier. There’s a Python module called keyword. It has a function called kwlist. Importing keyword and calling kwlist will return a list of Python’s keywords. Try it:

Those are all the words you can’t use. But that’s okay. When it comes to variable names, the sky is the limit! Well, almost. There are some rules you need to follow and some conventions you ought.

The Rules

  • Variables names must start with a letter or an underscore, such as:
    • _underscore
    • underscore_
  • The remainder of your variable name may consist of letters, numbers and underscores.
    • password1
    • n00b
    • un_der_scores
  • Names are case sensitive.
    • case_sensitive, CASE_SENSITIVE, and Case_Sensitive are each a different variable.

The Conventions

  • Readability is very important. Which of the following is easiest to read? I’m hoping you’ll say the first example.
    • python_puppet
    • pythonpuppet
    • pythonPuppet
  • Descriptive names are very useful. If you are writing a program that adds up all of the bad puns made in this book, which do you think is the better variable name?
    • total_bad_puns
    • super_bad
  • Avoid using the lowercase letter ‘l’, uppercase ‘O’, and uppercase ‘I’. Why? Because the l and the I look a lot like each other and the number 1. And O looks a lot like 0.

In our next lesson we will put all of this to good use making variable sandwiches. Mmm, just like Mom used to make.

Python Variable Assignment Statements and Naming Rules & Conventions was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid
×