In this tutorial, we get to know What is the purpose of the single underscore “_” variable in Python where we have a number of different uses as per requirements use it and here we will see all the possible examples of where we can use it.
Use of Underscore
There are different uses of single underscore which are followed here.
To Ingnor the Variable:
Underscore can be used for ignoring the variable written. Take an example, when we want to use a certain function in your program and that function returns some value that you do not want in that case we can use underscore before that so that we can ignore the value of that return type and can go with the use of it as simple.
For example, get us to take an example code where we can see how we can use it:
_, y = some_function() # here `_` is used to ignore the first returned value
Here some function is a function that returns some values and we do not want to we can store it in a variable and can simply ignore that variable by using an underscore before the name of the variable.
For Throwaway The variable in a Loop
When we are iterating in a sequence of items and we do not want to use the index or the value of the index to iterate it through the index number in such case we can use the sign of a single underscore which will do the same as we do with taking an example as a variable for iterating through the loop.
For an example of the same, we have given the example below where we can easily understand how it works and its functionality.
for _ in range(10): do_ something()
To indicate private unused variable
In python single underscore is also used for making the variable private or local. In simple meaning, if we use a single underscore before any variable that it is said that that variable is considered as private or local inside a loop for taking an example of the same and understanding it in a better way look at the example given below.
class MyClass: def __init__( self): self._ my_ private_ variable = 42 # here `_my_ private_ variable` is used to indicate that it's a private variable
Used As PlaceHolder
It is also used to store some result of the latest operation in simple means when we perform any direct mathematical calculations and print it on the console the result is automatically assigned or stored in an unidentified variable and that variable is nothing but a single underscore.
And we can use that underscore as result for any other expression just after that, For understanding it in a better way follow the example given below and learn.
>>> 2 + 2 4 >>> _ * 3 12 >>> _ ** 2 144
To learn more about What is the purpose of the single underscore “_” variable in Python visit: Single Underscore.
To learn more about python solutions and tutorials of problems faced during solving the problems and learning by clearing the concepts visit: Python Tutorials And Problems.
To learn more about different other programming languages and solutions to the problems related to programming visit: beta python programming languages Solutions.