What is the "local variable" in python?Please tell me specifically.

Asked 2 years ago, Updated 2 years ago, 273 views

I just started python, and I still don't understand local variables.
Therefore, I would appreciate it if you could let me know if you know it specifically.
Thank you for your cooperation.

python

2022-09-30 22:00

1 Answers

Summary and short description: For example, when defining a function, variables that can be referenced in it but cannot be referenced outside the function are referred to as "local variables" to mean "function local variables."

Specifically, the variable a can be referenced outside the function piyo, while the variable b cannot be referenced outside the function piyo.

a=42

def piyo():
    b = 100
    print(a+b)

print(a)# This is OK

print(b)# This is the error b is not defined.

By using this mechanism, you can organize the interval between variables and make reading and writing programs a little easier.

For more information, there are many books and websites in the world that will be Python tutorials, so you should read them.I think it's faster to read the materials that teach you systematically than to solve them in a question-and-answer format.


2022-09-30 22:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.