directory

  • A preface.
  • Python local variables
  • Python global variables
  • Python local variables and global variables
  • Five. Key summary
  • Six. Guess you like it

A preface.

Recommended path for learning Python: Python Learning Directory >> Python Basics

In Python, a variable has a life cycle. Once the life cycle is over, the program automatically clears up temporary space and frees up memory.

Python local variables

In general, variables declared inside functions or classes are local variables by default. Once the function ends, the life cycle of the variable ends, and the occupied space is automatically freed.

def func1(): Print (a) print(a) print(a) print(a) print(a) print(a)Copy the code

NameError: Name ‘a’ is not defined, cause: Local variables defined inside a function can only be used inside a function.

Python global variables

Global and local variablesIn contrast, variables declared outside a function or class default to global variables. Global variables have a lifetime that lasts until the end of the program, longer than local variables.

#! usr/bin/env python # ! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file: Python local variable and global variable. Py @time :2021/3/24 00:37 @motto: A thousand miles without a single step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! Func1 () a = 2 def func1(): print(a) # print(a) # print(a)Copy the code

Python local variables and global variables

1. The life cycle of global variables is until the end of the program, while the life cycle of local variables is the end of the function;

2. If you need to modify the value of a global variable inside the function, add the global keyword before the global variable. Otherwise, the global variable defaults to a local variable defined inside the function.

#! usr/bin/env python # ! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file: Python local variable and global variable. Py @time :2021/3/24 00:37 @motto: A thousand miles without a single step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! Def func1() def func1(): A = 1 print(a) # print(a) # print(a) # Func1 () # print(a) a print(a)Copy the code

Why should the last value be 2 when it should be 1?

Because without line 17, by default a is just a normal local variable inside the function, so print inside the function prints 1 to the console; The value of the local variable a is at the end of its life cycle. The print function after fun1 outputs the value of the global variable A, 2.

If a global variable is declared as a global variable, it can be modified successfully. Uncomment line 17, console output:

1
1
Copy the code

Five. Key summary

  • 1. Pay attention to the life cycle of local and global variables;
  • If you want to change the value of a global variable inside a function, add the global keyword.

Six.Guess you like

  1. Introduction of Python
  2. Python Pycharm Anacanda difference
  3. Python2.x and python3. x, how to choose?
  4. Python Configuration Environment
  5. Introduction to Python Hello World
  6. Python code comments
  7. Python Chinese encoding
  8. What is Anaconda? Anconda download the installation tutorial
  9. Pycharm prompt: this license **** has been cancelled
  10. Pycharm Sets the development template/font size/background color
  11. The Python list
  12. The Python tuple tuple
  13. Python dictionary dict

Python local variables and global variables

This article is published by the blog – Ape Say Programming Ape Say programming!