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
上面的代码先定义了name变量,赋值为“Yanpengtao”,然后将name赋值给name2,此时name和name2的值是相同的,再将name的值改变成“Fengjie”,此时输出name为“Yanpengtao”,而name仍然是"Yanpengtao",那这是怎么回事呢,下面进行分析: