Local Variables in Python: Understanding Function-Level Scope
A local variable is defined inside a function. You cannot access this variable outside the function's definition.
123456def greet(): message = "Aloha" print(message, "# local message") greet() print(message) # This line will result in an error
The output is:
Aloha # local message
NameError: name 'message' is not defined
The message variable is not accessible in the global scope; it exists only inside the function. When the function exits, this variable ceases to exist.
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 4. Kapitel 2
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 3.7
Local Variables in Python: Understanding Function-Level Scope
Svep för att visa menyn
A local variable is defined inside a function. You cannot access this variable outside the function's definition.
123456def greet(): message = "Aloha" print(message, "# local message") greet() print(message) # This line will result in an error
The output is:
Aloha # local message
NameError: name 'message' is not defined
The message variable is not accessible in the global scope; it exists only inside the function. When the function exits, this variable ceases to exist.
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 4. Kapitel 2