01. Names and Namespaces

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 and Global namespaces

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.

Overwriting names

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

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.

Augmented assignments

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 += (addition), -= (subtraction), *= (multiplication), /= (division), //= (flooring), %= (modulo, remainder), **= (exponent).

Learn to be a Pythonista!

Python Playground

Learn to be a Pythonista! Try Python

Python Playground is a collection of fun programming projects that will inspire you to new heights. You'll manipulate images, build simulations, and interact with hardware using Arduino & Raspberry Pi. With each project, you'll get familiarized with leveraging external libraries for specialized tasks, breaking problems into smaller, solvable pieces, and translating algorithms into code.

$ Check price
29.9529.95Amazon 4 logo(14+ reviews)

More Python resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources