六:变量、字符编码

来源:互联网 发布:安森垚 知乎 编辑:程序博客网 时间:2024/05/16 18:28

变量用于存储在计算机程序中引用和操作的信息。它们还提供了一种用描述性名称对数据进行标记的方法,这样我们的程序就可以更清楚地被读者和我们自己理解。把变量看作容纳信息的容器是有帮助的。他们唯一的目的是在内存中标记和存储数据。然后可以在整个程序中使用这些数据。

声明变量

1
2
3
#_*_coding:utf-8_*_
 
name = "Alex Li"

上述代码声明了一个变量,变量名为: name,变量name的值为:"Alex Li" 

变量定义的规则:

    • 变量名只能是 字母、数字或下划线的任意组合
    • 变量名的第一个字符不能是数字
    • 以下关键字不能声明为变量名
      ['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']
变量的赋值
1
2
3
4
5
6
7
8
name = "Alex Li"
 
name2 = name
print(name,name2)
 
name = "Jack"
 
print("What is the value of name2 now?")
原创粉丝点击