Hướng dẫn what are local variables and global variables in python *? - biến cục bộ và biến toàn cục trong python * là gì?

Các biến toàn cầu là các biến không được xác định bên trong bất kỳ hàm nào và có phạm vi toàn cầu trong khi các biến cục bộ là các biến được xác định bên trong một hàm và phạm vi của nó chỉ giới hạn ở hàm đó. Nói cách khác, chúng ta có thể nói rằng các biến cục bộ chỉ có thể truy cập bên trong hàm trong đó nó được khởi tạo trong khi các biến toàn cầu có thể truy cập trong toàn bộ chương trình và bên trong mọi hàm. Các biến cục bộ là các biến được khởi tạo bên trong một hàm và chỉ thuộc về hàm cụ thể đó. Nó không thể được truy cập ở bất cứ đâu bên ngoài chức năng. Hãy cùng xem cách tạo một biến cục bộ. are those which are not defined inside any function and have a global scope whereas local variables are those which are defined inside a function and its scope is limited to that function only. In other words, we can say that local variables are accessible only inside the function in which it was initialized whereas the global variables are accessible throughout the program and inside every function. Local variables are those which are initialized inside a function and belong only to that particular function. It cannot be accessed anywhere outside the function. Let’s see how to create a local variable.

Ví dụ: Tạo các biến cục bộCreating local variables

Python3

def f():

    

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

    

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

NameError: name 's' is not defined
6

Đầu ra

I love Geeksforgeeks

Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.

Example:

Python3

def f():

    

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

    

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
6
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
7

NameError: name 's' is not defined
6

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

Output:

NameError: name 's' is not defined

Đầu ra

Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.

Biến toàn cầuDefining and accessing global variables

Python3

def f():

    

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
Me too.
I love Geeksforgeeks
6
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
7

    

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

NameError: name 's' is not defined
6

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
UnboundLocalError: local variable 's' referenced before assignment
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
7

Đầu ra

Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks

Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.

Biến toàn cầu As there are no locals, the value from the globals will be used but make sure both the local and the global variables should have same name.

Đây là những cái được xác định bên ngoài bất kỳ chức năng nào và có thể truy cập trong suốt chương trình, tức là, bên trong và bên ngoài mọi chức năng. Hãy cùng xem cách tạo ra một biến toàn cầu.

Python3

def f():

    

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

    

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

    

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

NameError: name 's' is not defined
6

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

Đầu ra

Me too.
I love Geeksforgeeks

Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.

Biến toàn cầu

Example: 

Python3

def f():

Đây là những cái được xác định bên ngoài bất kỳ chức năng nào và có thể truy cập trong suốt chương trình, tức là, bên trong và bên ngoài mọi chức năng. Hãy cùng xem cách tạo ra một biến toàn cầu.

    

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
Me too.
I love Geeksforgeeks
6
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
7

    

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

NameError: name 's' is not defined
6

Output:

UnboundLocalError: local variable 's' referenced before assignment

Đầu ra

Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.

Biến toàn cầuglobal keyword in a function if we want to do assignments or change the global variable. global is not needed for printing and accessing. Python “assumes” that we want a local variable due to the assignment to s inside of f(), so the first statement throws the error message. Any variable which is changed or created inside of a function is local if it hasn’t been declared as a global variable. To tell Python, that we want to use the global variable, we have to use the keyword “global”, as can be seen in the following example: 

Đây là những cái được xác định bên ngoài bất kỳ chức năng nào và có thể truy cập trong suốt chương trình, tức là, bên trong và bên ngoài mọi chức năng. Hãy cùng xem cách tạo ra một biến toàn cầu.Using global keyword

Python3

def f():

Ví dụ: Xác định và truy cập các biến toàn cầu

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

    

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

Biến S được định nghĩa là biến toàn cầu và được sử dụng cả bên trong hàm cũng như bên ngoài hàm.

    

NameError: name 's' is not defined
4    6

LƯU Ý: Vì không có người dân địa phương, giá trị từ toàn cầu sẽ được sử dụng nhưng đảm bảo cả hai biến cục bộ và toàn cầu nên có cùng tên.

NameError: name 's' is not defined
6

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

Đầu ra

Python is great! GFG
Look for Geeksforgeeks Python Section
Look for Geeksforgeeks Python Section

Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.

Biến toàn cầuUsing global and local variables

Python3

Đây là những cái được xác định bên ngoài bất kỳ chức năng nào và có thể truy cập trong suốt chương trình, tức là, bên trong và bên ngoài mọi chức năng. Hãy cùng xem cách tạo ra một biến toàn cầu.

def f():

    

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
11
NameError: name 's' is not defined
12

Ví dụ: Xác định và truy cập các biến toàn cầu

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

    

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
22
NameError: name 's' is not defined
12

Biến S được định nghĩa là biến toàn cầu và được sử dụng cả bên trong hàm cũng như bên ngoài hàm.

LƯU Ý: Vì không có người dân địa phương, giá trị từ toàn cầu sẽ được sử dụng nhưng đảm bảo cả hai biến cục bộ và toàn cầu nên có cùng tên.

Bây giờ, điều gì sẽ xảy ra nếu có một biến có cùng tên được khởi tạo bên trong một chức năng cũng như toàn cầu. Bây giờ câu hỏi đặt ra, biến cục bộ sẽ có một số ảnh hưởng đến biến toàn cầu hoặc ngược lại, và điều gì sẽ xảy ra nếu chúng ta thay đổi giá trị của một biến bên trong hàm f ()? Nó sẽ ảnh hưởng đến toàn cầu? Chúng tôi kiểm tra nó trong phần mã sau: & nbsp;

    

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
36
NameError: name 's' is not defined
12

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
40
NameError: name 's' is not defined
12

NameError: name 's' is not defined
6

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
40
NameError: name 's' is not defined
12

NameError: name 's' is not defined
47

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
40
NameError: name 's' is not defined
12

NameError: name 's' is not defined
52

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
40
NameError: name 's' is not defined
12

Đầu ra

global :  1
Inside f() :  1
global :  1
Inside g() :  2
global :  1
Inside h() :  3
global :  3

    ____101011

Python is great! GFG
Look for Geeksforgeeks Python Section
Look for Geeksforgeeks Python Section
1Shwetanshu Rohatgi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


Các biến cục bộ và các biến toàn cầu trong Python là gì?

Nếu bạn tạo một biến có cùng tên bên trong một hàm, biến này sẽ là cục bộ và chỉ có thể được sử dụng bên trong hàm. Biến toàn cầu có cùng tên sẽ vẫn như vậy, toàn cầu và với giá trị ban đầu.

Các biến cục bộ và biến toàn cầu là gì?

Một biến toàn cầu là một biến có thể truy cập được trên toàn cầu. Một biến cục bộ là một biến chỉ có thể truy cập vào phạm vi hiện tại, chẳng hạn như các biến tạm thời được sử dụng trong một định nghĩa hàm duy nhất. A local variable is one that is only accessible to the current scope, such as temporary variables used in a single function definition.

Các biến cục bộ trong Python là gì?

Các biến cục bộ trong Python là những biến được khai báo bên trong hàm.Ngoài ra, họ được cho là được xác định trong một phạm vi địa phương.Người dùng chỉ có thể truy cập một biến cục bộ bên trong hàm nhưng không bao giờ nằm ngoài nó.those variables that are declared inside the function. Alternatively, they are said to defined within a local scope. A user can only access a local variable inside the function but never outside it.

Các biến toàn cầu trong Python là gì?

Một biến toàn cầu trong Python thường được tuyên bố là đỉnh của chương trình.Nói cách khác, các biến được khai báo bên ngoài hàm được gọi là biến toàn cầu.Bạn có thể truy cập các biến toàn cầu trong Python cả bên trong và bên ngoài hàm.