Python Programming: Variable Scope

RespectableKoto avatar
RespectableKoto
·
·
Download

Start Quiz

Study Flashcards

6 Questions

Biến toàn cục là biến được định nghĩa trong một hàm hoặc khối.

False

Để sửa đổi một biến toàn cục trong một hàm, từ khóa global phải được sử dụng.

True

Biến địa phương là biến được định nghĩa ngoài bất kỳ hàm hoặc khối nào.

False

Biến toàn cục có thể được truy cập từ bất kỳ đâu trong chương trình.

True

Nếu một biến được gán giá trị trong một hàm, nó sẽ được coi là một biến toàn cục.

False

Nếu một biến chỉ được tham chiếu trong một hàm, nó sẽ được coi là một biến toàn cục nếu nó tồn tại, hoặc sẽ xảy ra lỗi nếu nó không tồn tại.

True

Study Notes

Variable Scope in Python

Global Variables

  • A global variable is a variable that is defined outside of any function or block
  • Global variables are accessible from anywhere in the program
  • To modify a global variable inside a function, the global keyword must be used
  • Example:
x = 10  # global variable

def modify_x():
    global x
    x = 20
  • If the global keyword is not used, a new local variable will be created with the same name

Local Variables

  • A local variable is a variable that is defined inside a function or block
  • Local variables are only accessible within the scope of the function or block
  • Local variables are created when a function is called and destroyed when the function returns
  • Example:
def foo():
    x = 10  # local variable
    print(x)  # prints 10

print(x)  # Error: x is not defined
  • If a variable is assigned a value within a function, it becomes a local variable
  • If a variable is only referenced within a function, it will be treated as a global variable if it exists, or an error will occur if it does not exist

Phạm vi biến trong Python

Biến toàn cục (Global Variables)

  • Biến toàn cục là biến được định nghĩa ngoài bất kỳ hàm hoặc khối nào
  • Biến toàn cục có thể truy cập từ bất kỳ nơi nào trong chương trình
  • Để sửa đổi một biến toàn cục trong một hàm, phải sử dụng từ khóa global
  • Ví dụ: x = 10 (biến toàn cục) và def modify_x(): global x; x = 20

Biến cục bộ (Local Variables)

  • Biến cục bộ là biến được định nghĩa trong một hàm hoặc khối
  • Biến cục bộ chỉ có thể truy cập trong phạm vi của hàm hoặc khối
  • Biến cục bộ được tạo khi gọi hàm và bị hủy khi hàm trả về
  • Ví dụ: def foo(): x = 10; print(x) (in 10) và print(x) (lỗi: x không được định nghĩa)
  • Nếu một biến được gán giá trị trong một hàm, nó sẽ trở thành biến cục bộ
  • Nếu một biến chỉ được tham chiếu trong một hàm, nó sẽ được coi là biến toàn cục nếu nó tồn tại, hoặc sẽ xảy ra lỗi nếu nó không tồn tại

Learn about variable scope in Python, including global and local variables, how to modify global variables inside a function, and the importance of the global keyword.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser