variable

  declare variable

# Author :yanpengtao
# _*_coding:utf-8_*_

name = "Yanpengtao"
print("My name is :", name)
C: \ day1 \ venv \ Scripts \ python.exe G: /PythonStudy/S14/day1/var.py
My name is : Yanpengtao

  

The above code declares a variable, the variable name is: name, the value of the variable name is: "Yanpengtao", and the variable is output as "Yanpengtao"

Rules for variable definition:

  • Variable names can only be any combination of letters, numbers or underscores
  • The first character of a variable name cannot be a number
  • The following keywords cannot be declared as variable names
    ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', ' except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or' , 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

Assignment of variables:

# Author :yanpengtao
# _*_coding:utf-8_*_

name = "Yanpengtao"
print("My name is :", name)
name2 = name
print("name is :", name, "name2 is :", name2)
name = "Fengjie"
print("name is :", name, "name2 is :", name2)
C: \ day1 \ venv \ Scripts \ python.exe G: /PythonStudy/S14/day1/var.py
My name is : Yanpengtao
name is : Yanpengtao name2 is : Yanpengtao
name is : Fengjie name2 is : Yanpengtao

  The above code first defines the name variable, assigns the value to "Yanpengtao", and then assigns the name to name2. At this time, the values ​​of name and name2 are the same, and then the value of name is changed to "Fengjie". At this time, the output name is " Yanpengtao", and the name is still "Yanpengtao", so what's going on, the following analysis:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324931450&siteId=291194637