
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How Does Underscore "_" Work in Python Files
In Python, the underscore (_) is a special character with different meanings based on how and where it is used. It plays a significant role in readability, naming conventions, and interpreter behavior. In this article, we will discuss different scenarios of using the Underscore(_) in Python.
Single Underscore _ in Python
The single underscore (_) is used in Python for special purposes, which depend on the context. These are one of the most versatile and commonly seen symbols in clean and Pythonic code.
Use Cases of _ (Single Underscore)
The single underscore(_) can be used in different use cases. Let's see them one by one with detailed examples -
Ignore Values in Loops or Unpacking
When we don't care about a particular value in a loop or while unpacking a tuple, then _ is used as a throwaway variable. Following is an example in which we used underscore(_) as a loop index -
fruits = ["apple", "banana", "orange"] for _ in range(5): print("Processing...") # We don't need the loop variable
Following is the output of the above code in which underscore(_) is used in index looping -
Processing... Processing... Processing... Processing... Processing...
Ignoring Values with _
It is always possible to use the underscore to ignore or avoid certain values when unpacking sequences or tuples in Python. Here is another example in which we use underscore(_) in ignoring a value during unpacking values -
name, _, age = ("Riyaansh", "middle_name", 30) print(name, age) # Output: Riyaansh 30
Following is the output of the above code -
Riyaansh 30
Single Underscore _ in Python REPL
In the Python Interactive Shell, i.e., REPL, the single underscore _ is a built-in variable that stores the result of the last evaluated expression. REPL stands for READ EVALUATE PRINT LOOP. It is the environment where we type Python code line by line and execute then returns results immediately just like the terminal or the Python prompt.
Example
Here is an example in which the Python interactive shell uses underscore(_) to store the result of the last executed expression -
>>> 3+7 10 >>> _+5 15
Translation Function Placeholder
In some Python projects, especially internationalized applications, the single underscore (_) is used as a function or module alias for text translation.
Example
Here, in this example, we are using the underscore(_) as the alias name for the math module and using that alias name in the later Python code to get all the functions available in that module -
import math as _ print(dir(_))
Following is the output of the above code -
Help on built-in module math: NAME math DESCRIPTION This module provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(x, /) Return the arc cosine (measured in radians) of x. The result is between 0 and pi. acosh(x, /) Return the inverse hyperbolic cosine of x. asin(x, /) Return the arc sine (measured in radians) of x. The result is between -pi/2 and pi/2. asinh(x, /) Return the inverse hyperbolic sine of x. atan(x, /) Return the arc tangent (measured in radians) of x. The result is between -pi/2 and pi/2. atan2(y, x, /) -- More --
Single Leading Underscore (_var)
A single leading underscore before a variable or method name, such as _var, is a naming convention in Python. It signals that the variable or method is intended for internal use only and should not be accessed directly outside of its class or module. It is just a convention and not enforced by the Python language, but still it is accessible.
Example
Following is the example, which helps us to understand the Single Leading Underscore by using it to indicate that a variable is intended for internal use within a class -
class BankAccount: def __init__(self, name, balance): self.name = name # Public attribute self._balance = balance # Intended to be "private" i.e., internal use def deposit(self, amount): self._balance += amount def get_balance(self): return self._balance account = BankAccount("Riyaansh", 20000) print(account.name) # Output: Riyaansh print(account.get_balance()) # Output: 20000 # Direct access to _balance is possible but not recommended print(account._balance) # Output: 20000 (should be treated as internal)
Below is the output of the above Python code -
Riyaansh 20000 20000
Single Trailing Underscore (var_)
A single trailing underscore after a variable or function name, such as var_, is used in Python to avoid naming conflicts with Python's reserved keywords or built-in names. It allows us to use names, otherwise, they are not invalid or ambiguous while maintaining readability and clarity in our code.
Example
Following is the example which helps us to understand the Single Trailing Underscore by using it to avoid conflict with the Python keyword class -
class_ = "Python" def_ = "This is not a real keyword, just an example" print(class_) # Output: Python print(def_) # Output: This is not a real keyword just an example
Below is the output of the above Python code, which used the single trailing underscore -
Python This is not a real keyword, just an example