What is the function of an underline (_) in Python?

Asked 2 years ago, Updated 2 years ago, 39 views

I saw this code on the Internet, and I wonder what __ is doing with for_in instead of the string "_".

mylist = [1,1]

if True:
    for _ in mylist:
        print("hello world!")
hello world
hello world

python underscore

2022-09-22 15:35

1 Answers

These days, the trendy programming language, Go, and classical languages like Erlang, the meaning of '_' is syntax, which is used when you don't have to assign a value to a variable. From the example source code, it's easy to think that you're throwing away the element value with _ instead of using variables because you don't take and write elements as many as the number of elements in the list.

Python is usually called a 'throwaway variable'. Related Links: http://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python

Link to Golang: http://stackoverflow.com/questions/24357028/meaning-of-underscore-blank-identifier-in-go


2022-09-22 15:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.