变量
变量的概念基本上和初中代数的方程变量是一致的,只是在计算机程序中,变量不仅可以是数字,还可以是任意数据类型。变量在程序中就是用一个变量名表示了,变量名必须是大小写英文、数字和_的组合,且不能用数字开头。
常量
所谓常量就是不能变的变量,比如常用的数学常数π就是一个常量。在Python中,通常用全部大写的变量名表示常量:
PI = 3.14159265359
数据类型
- 整数
- 浮点数
- 字符串
‘’ 和 ""没有区别,如果要输出 ‘或者"可以用转义字符 \,另外 ‘’’’’’ 可以输入换行
Python对bytes类型的数据用带b前缀的单引号或双引号表示:
x = b'adc'
- 布尔值
和java不一样,python中首字母大写 True False, 布尔值可以用and、or和not运算 - 空值
空值是Python里一个特殊的值,用None表示。None不能理解为0,因为0是有意义的,而None是一个特殊的空值。此外,Python还提供了列表、字典等多种数据类型,还允许创建自定义数据类型,我们后面会继续讲到。 - list
Python内置的一种数据类型是列表:list。list是一种有序的集合,可以随时添加和删除其中的元素。
list包含以下几种方法:
len() 长度
append() 追加
insert() 插入
pop() 删除末尾元素
pop(n) 删除指定索引元素
studentList = ['王越', '贾子君']
print("studentList的值为:{}".format(studentList))
print("studentList的长度为:{}".format(len(studentList)))
print("studentList的第一个元素是:{}".format(studentList[0]))
studentList.append("尚珊珊")
print("studentList追加后的值为:{}".format(studentList))
studentList.insert(1, 'haha')
print("studentList追加后的值为:{}".format(studentList))
studentList.pop()
print("studentList删除最后一个元素后的值为:{}".format(studentList))
studentList.pop(0)
print("studentList删除第一个元素后的值为:{}".format(studentList))
输出:
studentList的值为:['王越', '贾子君']
studentList的长度为:2
studentList的第一个元素是:王越
studentList追加后的值为:['王越', '贾子君', '尚珊珊']
studentList追加后的值为:['王越', 'haha', '贾子君', '尚珊珊']
studentList删除最后一个元素后的值为:['王越', 'haha', '贾子君']
studentList删除第一个元素后的值为:['haha', '贾子君']
- tuple 通俗点,就是不可变的list