Python variable explanation

Reprinted to: https://mp.weixin.qq.com/s/WPTUzgN_g3eRYL4-NTpN8w

1 definition of variables

A variable is actually equivalent to a "name", which can also be understood as a label

2 function

When we go shopping in the supermarket, we often store some of the things we carry with us in the lockers of the supermarket. There are so many small boxes in the lockers, how do we know which small box we put our things in? Do you find that there is a number on each small grid? When we take things, we just take the key of the locker to find the small grid with the corresponding number?

Similarly, there are so many values and objects in our program (of course, everything is an object in python). Where do we put them? In fact, they are placed in memory, and we can also understand that there are many small grids in memory. There is a memory address on each small grid, and our variable points to this memory address.

3 naming of variables

In fact, the naming of a variable is equivalent to giving a name to the variable, but the name can't be confused. It has certain rules. Before talking about the naming rules of variables, let's talk about what is the keyword of Python?

3.1 python keyword

  • Python keywords are some words that have been given a specific meaning in Python. If we use these keywords as our variable names, some programs will report errors at this time

  • What are the keywords of Python?

  

  • How to view Python keywords? We can view it through the following command:

>>> import keyword
>>> keyword.kwlist

    

Example:

#We use if As a variable name, and if Print the value of this variable
if = "Zhang San"
print(if)
Execution results:
if = "Zhang San"
       ^
SyntaxError: invalid syntax
 At this time, a syntax error will be reported

3.2} variable naming rules

  • By letter, underscore "" And numbers, and the first letter cannot be numeric;

  • Cannot be a keyword of Python;

  • It is case sensitive. In Python, it will be strictly case sensitive. If the case format of two identical words is different, the meaning will be completely different;

  • The variable name must be meaningful. Others will know what it is at a glance, that is, considering the name and meaning, which can improve the readability of the code;

  • In python, although you can name with Chinese characters without reporting errors, it is recommended that you do not name with Chinese characters;

  • Hump type (such as UserName or userName) and underline (user_name) are recommended for naming;

  • Constants are named by using uppercase

4 assignment of variables

Each value of a variable will be stored in memory. At this time, a space will be opened in memory, and the size of each space is determined by the type of variable, but it is not necessary to declare the type of variable in python;

Each variable must be assigned before use, and will be created only after the variable is assigned.

4.1 preparation rules:

Variable name =  value
 For example: name = "Zhang San"
Explanation: assign "Zhang San" to the variable name

4.2 assign the same value to multiple variable names

a,b,c=1
Explanation: assign 1 to a and b as well as c

4.3} assigning values to multiple variables

a,b,c = 1,2,3
Explanation: assign 1 to a,Assign 2 to b,Assign 3 to c

5 local and global variables

5.1 local variables

  • Local variables are scoped: they can only be defined inside a function and can only be used inside a function

#Define variables inside functions name_in
def getName_1():
    name_in = "Li Si"
    print("local variable name->{}".format(name_in))


getName_1()
Execution results:
local variable name->Li Si
  • Different functions can define local variables with the same name, but they will not affect each other



#stay getName_1 and getName_2 Methods, variables are defined name_in
def getName_1():
    name_in = "Li Si"
    print("local variable name->{}".format(name_in))


def getName_2():
    name_in = "Wang Wu"
    print("local variable name->{}".format(name_in))


getName_1()
getName_2()
Execution results:
local variable name->Li Si
 local variable name->Wang Wu


#From the results, it is not difficult to find that although it is defined in both functions name_in,However, the final printed results are the variable values assigned to them

 

  • Function of local variable: it is used inside the function to temporarily save the data needed inside the function

  • Life cycle of local variables

    • The so-called life cycle is the process from variable creation to system recycling

    • Local variables are created only when the function is executed. After execution, the local variables will be recycled by the system

5.2 global variables

  • Global variables are variables defined outside the function

  • This variable can be used inside all functions

  • #Define a global variable
    name_out = "Zhang San"
    
    
    def getName_1():
        name_in = "Li Si"
        print("global variable name->{}".format(name_out))
        print("local variable name->{}".format(name_in))
    
    
    getName_1()
    
    
    Execution results:
    global variable name->Zhang San
     local variable name->Li Si

Keywords: Python

Added by delphi123 on Tue, 25 Jan 2022 14:33:14 +0200