Role of Underscore(_) in Python
In this tutorial, you're going to learn about the uses of underscore(_) in python.
Many of the Python Developers don't know about the functionalities of underscore(_) in Python. It helps users to write Python code productively.
Underscore(_) is a unique character in Python.
If you're a Python programmer, you probably familiar with the following syntax:
- for _ in range(100)
- _ = 2
It has some special meaning in different conditions. Let's see all of those.
You will find max six different uses of underscore(_). If you want you can use it for different purposes after you have an idea about underscore(_).
6 + 4
_ # stores the result of the above expression
_ + 6
a = _ # assigning the value of _ to another variable a
2. Ignoring Values
Underscore(_) is also used to ignore the values. If you don't want to use specific values while unpacking, just assign that value to underscore(_).
Ignoring means assigning the values to special variable underscore(_). We're assigning the values to underscore(_) given not using that in future code.
See the example
## ignoring a value a, _, b = (1, 2, 3) # a = 1, b = 3 print(a, b) ## ignoring multiple values ## *(variable) used to assign multiple value to a variable as list while unpacking ## it's called "Extended Unpacking", only available in Python 3.x a, *_, b = (7, 6, 5, 4, 3, 2, 1) print(a, b)
## lopping ten times using _ for _ in range(5): print(_) ## iterating over a list using _ ## you can use _ same as a variable languages = ["Python", "JS", "PHP", "Java"] for _ in languages: print(_) _ = 5 while _ < 10: print(_, end = ' ') # default value of 'end' id '\n' in python. we're changing it to space _ += 1
4. Separating Digits Of Numbers
If you have a long digits number, you can separate the group of digits as you like for better understanding.
Ex:- million = 1_000_000
Next, you can also use underscore(_) to separate the binary, octal or hex parts of numbers.
Ex:- binary = 0b_0010, octa = 0o_64, hexa = 0x_23_ab
Execute all the above examples to see the results.
## different number systems ## you can also check whether they are correct or not by coverting them into integer using "int" method million = 1_000_000 binary = 0b_0010 octa = 0o_64 hexa = 0x_23_ab print(million) print(binary) print(octa) print(hexa)
5. Naming Using Underscore(_)
Underscore(_) can be used to name variables, functions and classes, etc..,
- Single Pre Underscore:- _variable
- Signle Post Underscore:- variable_
- Double Pre Underscores:- __variable
- Double Pre and Post Underscores:- __variable__
Single Pre Underscore is used for internal use. Most of us don't use it because of that reason.
See the following example.
class Test: def __init__(self): self.name = "datacamp" self._num = 7 obj = Test() print(obj.name) print(obj._num)
single pre underscore doesn't stop you from accessing the single pre underscore variable.
But, single pre underscore effects the names that are imported from the module.
Let's write the following code in the my_funtions file.
## filename:- my_functions.py
def func(): return "datacamp"
def _private_func(): return 7
Now, if you import all the methods and names from my_functions.py, Python doesn't import the names which starts with a single pre underscore.
from my_functions import * func()