Python placeholder '_' Top Ideas to Use it Perfectly

In Python, you can call the underscore ( _ ) operator placeholder. The prime purpose is you can mask the variable that you don't use in a function.

Underscore in Python makes you code clutter-free. The given ideas useful to apply in your project.
Python placeholder 

Purpose of Placeholder

The purpose is you can mask a few variables. This makes your code is readable. So in the future, you can replace placeholders with variable names you want.

How to Use Placeholder in a Function

I have explained in three steps how to use placeholder correctly.
  1. Code to create a Function
  2. Logic to use placeholder
  3. Logic to use two placeholders

1. Code to create a Function.


def function_that_returns_multiple_values(x): 
      return x*2, x*3, x+1 
      for i in range(0,5): 
           square, cube, added_one = function_that_returns_multiple_values(i) 
           print(square, cube)

In the above logic, in print, it returns only two variables. Therefore you can mask the third variable with an underscore.  Thus your code looks beautiful.

2. Logic to use Placeholder


def function_that_returns_multiple_values(x): 
      return x*2, x*3, x+1 
      for i in range(0,5): 
            square, cube, _ = function_that_returns_multiple_values(i) 
            print(square, cube)

3.. How to use two Placeholders

for i in range(0,5): 
     square, _, _ = function_that_returns_multiple_values(i)


Comments

Popular Posts

7 AWS Interview Questions asked in Infosys, TCS

Hyperledger Fabric: 20 Real Interview Questions

How to Fix Python Syntax Errors Quickly

Python 'getsizeof' Command the Real Purpose

How to Check Log File in Kafka

5 HBase Vs. RDBMS Top Functional Differences

Python Dictionary Vs List With Examples

Blue Prism complete tutorials download now

Linux Relative Vs. Absolute Path Top Differences

How to Use the ps Command in Linux