Home Backend Development Python Tutorial The difference between global variables and local variables in Python (detailed code explanation)

The difference between global variables and local variables in Python (detailed code explanation)

Mar 16, 2019 pm 02:16 PM
python global variables local variables

The difference between global variables and local variables in Python (detailed code explanation)

#Global variables are variables defined and declared outside the function and we need to use them inside the function.

#这个函数使用全局变量s
def f():  
    print s  
  
# 全局作用域
s = "I love Python"
f()
Copy after login

Output:

I love Python
Copy after login

If a variable with the same name is defined within a function scope, then it will only print the value given within the function instead of the global value.

# 这个函数有一个与s同名的变量。
def f():  
    s = "Me too."
    print s  
  
# 全局作用域
s = "I love Python" 
f() 
print s
Copy after login

Output:

Me too
I love Python
Copy after login

Before we call the function f(), the variable s is defined as the string "I love Python". The only statement in f() is the "print s" statement. Since there is no local s, the value of global s will be used.

The question is, what happens if we change the value of s inside function f()? Will it affect the overall situation?

We test it in the following code:

def f():  
    print s 
  
    # 如果我们在下面评论,这个程序不会显示错误。
    s = "Me too."
  
    print s 
  
#全局作用域
s = "I love Python" 
f() 
print s
Copy after login

Output:

Line 2: undefined: Error: local variable 's' referenced before assignment
Copy after login

In order for the above program to work, we need to use the "global" keyword. If we want to assign/change them, we just need to use the global keyword in the function. Printing and accessing do not require globals.

Python "assumes" we want a local variable, so the first print statement throws this error message because of the assignment inside f(). Any variable changed or created inside a function is local if it has not been declared as a global variable. To tell Python that we want to use global variables, we must use the keyword "global"

As shown in the following example:

# 这个函数修改全局变量's' 
def f(): 
    global s 
    print s 
    s = "Look for Python"
    print s  
  
#全局作用域
s = "Python is great!" 
f() 
print s
Copy after login

Output:

Python is great!
Look for Python.
Look for Python.
Copy after login

A nice Example:

a = 1
  
# 使用global,因为没有本地'a' 
def f(): 
    print 'Inside f() : ', a 
  
#变量“a”被重新定义为局部变量
def g():     
    a = 2
    print 'Inside g() : ',a 
  
#使用全局关键字修改全局'a'
def h():     
    global a 
    a = 3
    print 'Inside h() : ',a 
  
# 全局作用域
print 'global : ',a 
f() 
print 'global : ',a 
g() 
print 'global : ',a 
h() 
print 'global : ',a
Copy after login

Output:

global :  1
Inside f() :  1
global :  1
Inside g() :  2
global :  1
Inside h() :  3
global :  3
Copy after login

Related recommendations: "Python Tutorial"

The above is the detailed content of The difference between global variables and local variables in Python (detailed code explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

See all articles