1、类型
数字型:
int # 整型 id = 1
float # 浮点型 num = 11.1
bool # 布尔型 flag = True
真 True 非0即真
假 False 0
complex # 复数型
非数字型:
字符串 s = "hello world"
列表 list1 = [1,2,3]
元组 tuple1 = (1,2,3)
字典 dict1 = {"name":"lisi","age":16}
集合 set1 = {1,2,3}
(*b,) = 1, 2, 3 # [*b] = 1,2,3
a, *c = 1, 2, 3
print(a)
print(type(b)) # list
1.1、字符串
2、命名
1、字母、数字、_
2、不能数字开头
推荐:一般以_分割单词,如 name_list
3、变量间计算
数字间可以直接计算
变量bool型,True对应数字1,False对应数字0
字符串变量之间使用 + 号进行拼接
字符串和整数之间 使用 * 号可以重复拼接字符串
注意:字符串不能和整数相加,也不能和浮点数相乘
4、列表
4.1、定义
name_list = ["lizi", "wangwu", "lisi"]
num_list = [1, 2, 3, 4]
a = list() # 空列表
4.2、使用
num_list = [5, 8, 3, 10]
num_list.sort() # 升序排序
num_list.sort(reverse=True) # 降序排序
num_list.reverse() # 反转
len(num_list)
num_list.append(2)
num_list.remove(3)
print(num_list)
4.3、迭代遍历
num_list = [5, 8, 3, 10]
for num in num_list:
print(num)
4.4、列表推导式
# 不带条件
list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = [i[-1] for i in list1]
print(new_list) # [3, 6, 9]
# 带if条件
list1 = [{"name": "lisi", "age": 14}, {"name": "ww", "age": 14}, {"name": "zs", "age": 20}]
new_list = [18 if dict1["age"] > 18 else dict1["age"] + 1 for dict1 in list1]
print(new_list) #[15, 15, 18]
# if else 条件
list1 = [{"name": "lisi", "age": 14}, {"name": "ww", "age": 14}, {"name": "zs", "age": 20}]
new_list = [ dict1 for dict1 in list1 if dict1["age"] >18 ]
print(new_list) #[{'name': 'zs', 'age': 20}]
result = [(x, y) for x in range(1, 5) if x % 2 == 0 for y in range(1, 10) if y % 2 != 0]
print(result) #[(2, 1), (2, 3), (2, 5), (2, 7), (2, 9), (4, 1), (4, 3), (4, 5), (4, 7), (4, 9)]
5、元组
5.1、定义
#元组定义好后不能修改
num_tuple = (1, True, 1.45, "hello")
num_tuple = (12,) #定义单个元素的元组,后面需要加一个,号
num_tuple = () # 空元组 a = tuple()
5.2、使用
num_tuple = (1, 1.45, True, "hello")
print(num_tuple.count(1)) # 注意True 也是数字1
#格式字符串
str1 = ("小明", 14, 22.2)
print("%s %d %f" % str1)
#列表和元组互相转换
num_tuple = (1, 1.45, True, "hello")
num_list = list(num_tuple)
print(type(num_list))
print(type(tuple(num_list)))
#元组因为一般存储不同类型的数据,所以迭代遍历使用的较少,列表一般存储相同类型的数据
6、字典
6.1、定义
dict1=dict() # 空字典
user1 = {
"name": "lisi",
"age": 18,
"male": True,
"weight": 76.5
}
print(user1)
注意:字典是无序的,key不能相同
6.2、使用
user1["high"] = 222 # 增
user1.pop("male") # 删
user1["name"] = "zs" # 改
print(user1["name"]) # 查
6.2、迭代遍历
user1 = {
"name": "lisi",
"age": 18,
"male": True,
"weight": 76.5
}
for k in user1:
print("%s = %s" %(k, user1[k]))
6.3、字典推导式
7、集合
7.1、定义
set1 = set() # 空集合
set2 = {1,2,3}
set1.add(1)
print(set1)
print(set2)
7.2、使用
num_list = [1, 3, 4, 5]
set1 = set(num_list)
set1.add(0)
set1.add(1)
set1.remove(3)
set1.pop()
set1.discard(3)
s1 = {1, 2, 3}
s2 = {3, 4, 5}
s3 = s1.difference(s2) # 差集{1,2} 等于s1-s2
s3 = s1.intersection(s2) # 交集{3} s1 & s2
s3 = s1.union(s2) # 并集 {1,2,3,4,5} s1 | s2
print(s3)
7.3、集合推导式
8、for迭代遍历
user1 = {
"name": "lisi",
"age": 18,
"male": True,
"weight": 76.5
}
user2 = {
"name": "lisi2",
"age": 18,
"male": True,
"weight": 76.5
}
user3 = {
"name": "lisi3",
"age": 18,
"male": True,
"weight": 76.5
}
user = [user1, user2, user3]
for u in user:
if u["name"] == "lisi4":
print(u)
print("找到了")
break
else:
print("没有找到!")
注意:for可以跟else组合,如果通过break退出for循环,就不会执行else语句,正常循环结束会执行else语句