Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

What are variables?

It seems fairly obvious that Python should allow you to encode literals carrying number and text values.

You already know that you can do some arithmetic operations with these numbers: add, subtract, etc. You'll be doing that many times. 

But it's quite a normal question to ask how to store the results of these operations, in order to use them in other operations., and so on.

How do you save the intermediate results, and use them again to produce subsequent ones?

Python will help you with that. It offers special "boxes" (containers) for that purpose, and these boxes are called variables - the name itself suggests that the content of these containers can be varied in (almost) any way.

What does every Python variable have?

  • a name
  • a value (the content of the container)

Let's start with the issues related to a variable's name.

Variables do not appear in a program automatically. As developer, you must decide how many and which variables to use in your programs. 

You must also name them. 

If you want to give a name to a variable, you must follow some strict rules:

  • the name of the variable must be composed of upper-case or lower-case letters, digits, and the character _ (underscore)
  • the name of the variable must begin with a letter
  • the underscore character is a letter
  • upper- and lower-case letters are treated as different (a little differently than in the real world - Alice and ALICE are the same first names, but in Python they are two different variable names, and consequently, two different variables)
  • the name of the variable must not be any of Python's reserved words (the keywords - we'll explain more about this soon).

Correct and incorrect variable names

Note that the same restrictions apply to function names. 

Python does not impose restrictions on the length of variable names, but that doesn't mean that a long variable name is always better than a short one. 

Here are some correct, but not always convenient variable names: 

MyVariable, i, t34, Exchange_Rate, counter, days_to_christmas, TheNameIsSoLongThatYouWillMakeMistakesWithIt, _.

Moreover, Python lets you use not only Latin letters but also characters specific to languages that use other alphabets. 

These variable names are also correct:

Adiós_Señora, sûr_la_mer, Einbahnstraße, переменная.

And now for some incorrect names:

10t (does not begin with a letter), Exchange Rate (contains a space)

NOTE:

The PEP 8 -- Style Guide for Python Code recommends the following naming convention for variables and function in Python:

  • variable names should be lowercase, with words separated by underscores to improve readability (e.g., var, my_variable)
  • function names follow the same convention as variable names (e.g., fun, my_function) 
  • it's also possible to use mixed case (e.g., myVariable), but only in contexts where that's already the prevailing style, to retain backwards compatibility with the adopted convention. 

Keywords

Take a look at the list of words that play a very special role in every Python program.

 

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

They are called keywords or (more precisely) reserved keywords. They are reserved because you mustn't use them as names: neither for your variables, nor functions, nor any other named entities you want to create.

The meaning of the reserved word is predefined, and mustn't be changed in any way. 

Fortunately, due to the fact that Python is case-sensitive, you can modify any of these words by changing the case of any letter, thus creating a new word, which is not reserved anymore.

For example - You can't name your variable like this:

import

You mustn't have variable named in such a way - it is prohibited. But you can do this instead:

Import

These words might be a mystery to you now, but you'll soon learn the meaning of them. 

 

What are variables?

David Khieu
Module by David Khieu, updated more than 1 year ago

Description

What are variables?
No tags specified