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:
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:
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