08-Variables and reserved words
1: reserved words
使用以下代码查看Python中的保留字
Demo:
import keyword
print(keyword.kwlist)
Output:
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
These reserved words generally cannot be used as identifiers
2: Identifier
-
What is an identifier?
Variables, functions, classes, modules, and other objects are namedidentifier
-
Identifier naming rules
- Can only be named with letters, numbers, and underscores
- Cannot start with a number
- Cannot be a reserved word
- Strictly case sensitive
Three: Variables
-
what is a variable
A variable is a labeled box in memory.
-
Definition and use of variables
-
Variables consist of three parts
- Identification: represents the memory address where the object is stored, using id( obj )Obtain
- Type: The data type of the object, use type( obj )Obtain
- Value: The specific data stored in the object, use print( obj )print out directly
-
name = "Gui Gui"
Analysis:
*name: * identifier, which is the variable name
*=: *assignment operator
*Guigui:*give the value of the variable
-
-
Multiple assignment of variables
- Variables can be assigned multiple times, and new values will overwrite old ones.
Demo:
# 变量
name = "桂桂";
print("内存地址:",id(name))
print("类型:",type(name))
print("值:",name)
# 多次赋值
age = 1234;
print("id:",id(age)," type:",type(age)," 值:",age)
age = 5678;
print("id:",id(age)," type:",type(age)," 值:",age)
Output:
内存地址: 2537550061808
类型: <class 'str'>
值: 桂桂
id: 2537544939152 type: <class 'int'> 值: 1234
id: 2537551076816 type: <class 'int'> 值: 5678