Python names are how Python is able to identify functions, variables and modules. Names are made up of letters, underscores and digits, but may only start with letters and underscores. We'll see later when we cover classes that those that start with two underscores have a special meaning.
Within different context and scopes, names can have entirely different meanings. The space in which a name is mapped to an object thing is called the namespace.
Local namespaces are created when a function is called, and deleted when the function returns a value or raises an exception that is not handled within the function. Unless a variable is declared with the
global keyword, it is defaulted to the local namespace.
Within a global namespace, a name has the same meaning everywhere. In Python, namespaces are implemented in the dictionary datatype, which we'll see later.
Be cautious when creating a name to make sure it's not already taken. If you import or use a name that was already in use, the previous name will be overwritten. This will cause unexpected errors and bugs all over your program so take caution!
Assigning names is easy. Simply use the
= operator, with the name on the left-hand side.
>>> sample = "test" >>> a = b = c = 5 # Assigns all three variables with the number 5.
You can easily modify an already existing variable, with an augmented assignment operator.
>>> i = 5 >>> i += 5 # Same as i = i + 5
Other augmented assignments include
%= (modulo, remainder),
Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz's popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It's an ideal way to begin, whether you're new to programming or a professional developer versed in other languages.$ Check price
Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.$$ Check price