- To store an object, we assign it a name using the equal sign
- Names must not start with a number and they cannot contain special characters (except for underscores)
- When you choose names, be consistent and descriptive
You have probably heard of variables. In mathematics, a variable is a placeholder for something that is not fully defined just yet. In most programming languages, we say: “We define the variable”. In Python we rarely talk about variables. We instead talk about names. We would say: “We assign a name”. Specifically, we assign a name to an object. To do so, we use the equal sign. While the vocabulary is slightly different, the result is very similar in all programming languages. Let’s look at an example.
my_name = 'Daniel' my_name # 'Daniel'
Here we assign the name
my_name to the string object
'Daniel'. From now on we can use the name to refer to the object. We can perform operations on the name and Python will replace the name with the object it refers to.
my_name = 'Daniel' my_name * 5 # 'DanielDanielDanielDanielDaniel'
The multiplication operator applited to a string concatenates that array multiple times. Here we get the same string five times. Note, that we use the name, instead of the literal object. We can also reassign the same name to a different object.
my_name = 'Daniel' my_name # 'Daniel' my_name = 5 * my_name my_name # 'DanielDanielDanielDanielDaniel'
my_name refers to
'Daniel', then it refers to the result of the operation
my_name * 5, which is
'DanielDanielDanielDanielDaniel'. When we choose a name, there are very few rules that we must follow. However, there are many more rules that we definitely should follow. We take a look at the mandatory rules first.
When we choose a name, there are many things to consider. But there are also some things that we are not allowed to do. For example a name cannot start with a number.
1st_name = 5 # SyntaxError: invalid syntax first_name = 5 first_name # 5 name_1 = 5 name_1 # 5
In other places of the name, numbers are allowed. Special characters are not allowed at any place of a name.
name$one = 5 # SyntaxError: invalid syntax
The only special character that is allowed is the underscore. Underscores are conventionally used in Python to structure names visually.
division_and_multiplication = 5 / 10 * 2 division_and_multiplication # 1.0
Besides the visually structuring names, underscores also have special meaning when leading a name. For example, all objects in Python have special double underscore methods. We will learn all about them later.
Good Naming Practices
If you are just getting started with Python, you probably have a lot of things on your mind. Actually, if you follow the above rules, you can choose any name you want. However, it is very important to learn good naming practices in Python. Rule number one is consistency. Avoid wildly different ways to visually structure names within the same project. Don’t start with
my_name and end with
MyName. If you stay consistent within your projectsyou are already ahead of the curve. For beginners I recommend making names all lower case and structuring only using underscores. Also avoid numbers. Write
name_one instead of
name_1. Also, don’t be afraid of making names long. It is more important names are descriptive than short.
length_of_segment is much better than
los, even if there is more typing involved. Don’t be afraid to type. Be afraid of coming back to your code one year after you wrote it and trying to read completely unreadable, meaningless names.
Those are enough rules for now. If you want to adopt a perfect style right from the beginning you should read the official style guide for python called PEP8. There are generally no laws about Python style but this is the most authoritative document on coding style in Python and many people reading your code will expect you to follow the guidelines in this document. If you follow the few rules I describe above, you are already following the most important practices regarding naming.
To store objects we assign them a name. We can do that with the equal sign. The name is on the left, the object is on the right.
name = object Needless to say, we’ll be spending a lot of time assigning names. Once a name is assigned, we can use the name instead of the object. Python will replace the name with the object in any operation. There are very few mandatory rules when choosing a name. For one, names cannot start with a number. Second, special characters, except for the underscore (_) are forbidden in names. There are some more rules that are good to follow. The most important one is to be consistent. Do not arbitrarily change your style within a project. If you do this you are already doing well.