Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Local Variables in Python: Understanding Function-Level Scope | Understanding Variable Scope in Python
Intermediate Python

bookLocal 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.

123456
def greet(): message = "Aloha" print(message, "# local message") greet() print(message) # This line will result in an error
copy

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.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 3.7

bookLocal Variables in Python: Understanding Function-Level Scope

Deslize para mostrar o menu

A local variable is defined inside a function. You cannot access this variable outside the function's definition.

123456
def greet(): message = "Aloha" print(message, "# local message") greet() print(message) # This line will result in an error
copy

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.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2
some-alt