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

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. 

 

Creating variables

What can you put inside a variable?

Anything.

You can use a variable to store any value of any of the already presented kinds, and many more of the ones we haven't shown you yet.

The value of a variable is what you have to put into it. It can vary as often as you need or want. It can be an integer one moment, and a float a moment later, eventually becoming a string.

Let's talk now about two important things p how variables are created, and how to put values inside them (or rather - how to give or pass values to them).

REMEMBER

A variable comes into existence as a result of assigning a value to it. Unlike in other languages, you don't need to declare it in any special way.

If you assign any value to a nonexistent variable, the variable will be automatically created. You don't need to do anything else.

The creation (or otherwise -its syntax) is extremely simple: just use the name of the desired variable, then the equal sign (=) and the value you want to put into the variable.

You can put anything inside a variable. 

Take a look at the snippet:

var = 1

print(var)

It consists of two simple instructions:

  • The first of them creates a variable named var, and assigns a literal with an integer value equal to 1. 
  • The second prints the value of the newly created variable to the console.

Note: print() has yet another side to it - it can handle variables too. Do you know what the output of the snippet will be? 

1

Correct and incorrect variable names

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

Description

Correct and incorrect variable names
No tags specified