08-Variables and reserved words

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

  1. What is an identifier?

    Variables, functions, classes, modules, and other objects are namedidentifier

  2. 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

  1. what is a variable

    A variable is a labeled box in memory.

  2. Definition and use of variables

    • Variables consist of three parts

      1. Identification: represents the memory address where the object is stored, using id( obj )Obtain
      2. Type: The data type of the object, use type( obj )Obtain
      3. 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

  3. 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'>
值: 桂桂

id2537544939152  type<class 'int'>  值: 1234
id2537551076816  type<class 'int'>  值: 5678

Guess you like

Origin blog.csdn.net/qq_51248309/article/details/133093810