# 整型和浮点型数字
x = 10 # 整型
y = 2.5 # 浮点型
# 算术运算
print(x + y) # 12.5
print(x - y) # 7.5
print(x * y) # 25.0
print(x / y) # 4.0
# 转换为整型
a = int(2.5)
b = int('10')
print(a, b) # 2 10
# 转换为浮点型
c = float(10)
d = float('2.5')
print(c, d) # 10.0 2.5
# 字符串
s = 'Hello World'
# 拼接字符串
s1 = 'Hello'
s2 = 'World'
s3 = s1 + ' ' + s2
print(s3) # 'Hello World'
# 删减字符串
s4 = 'Python'
s5 = s4[1:]
print(s5) # 'ython'
# 字符串格式化
name = 'Alice'
age = 25
print('My name is {} and I am {} years old'.format(name, age))
# 字符串查找
s6 = 'Hello World'
print(s6.find('World')) # 6
# 字符串替换
s7 = 'Hello World'
print(s7.replace('Hello', 'Hi')) # 'Hi World'
# 字符串分割
s8 = 'a,b,c'
print(s8.split(',')) # ['a', 'b', 'c']
# 字符串大小写转换
s9 = 'Hello World'
print(s9.upper()) # 'HELLO WORLD'
print(s9.lower()) # 'hello world'
# 列表
list1 = [1, 2, 3, 4]
list2 = ['a', 'b', 'c']
# 访问和修改元素
list1[0] = 5
print(list1) # [5, 2, 3, 4]
# 切片
list3 = [1, 2, 3, 4, 5]
list4 = list3[1:3]
print(list4) # [2, 3]
# 追加元素
list5 = [1, 2, 3]
list5.append(4)
print(list5) # [1, 2, 3, 4]
# 删除元素
list6 = [1, 2, 3, 4]
del list6[0]
print(list6) # [2, 3, 4]
# 排序
list7 = [3, 4, 1, 2]
list7.sort()
print(list7) # [1, 2, 3, 4]
# 计数
list8 = [1, 2, 3, 2, 2]
print(list8.count(2)) # 3
# 扩展
list9 = [1, 2, 3]
list9.extend([4, 5])
print(list9) # [1, 2, 3, 4, 5]
# 元组
tuple1 = (1, 2, 3)
tuple2 = ('a', 'b', 'c')
# 访问元素
print(tuple1[0]) # 1
# 切片
tuple3 = (1, 2, 3, 4, 5)
tuple4 = tuple3[1:3]
print(tuple4) # (2, 3)
# 集合
set1 = {1, 2, 3}
set2 = {'a', 'b', 'c'}
# 并集
set3 = set1.union(set2)
print(set3) # {1, 2, 3, 'a', 'b', 'c'}
# 交集
set4 = set1.intersection(set3)
print(set4) # {1, 2, 3}
# 差集
set5 = set1.difference(set3)
print(set5) # set()
# 字典
dict1 = {'name': 'Alice', 'age': 25}
dict2 = {'s1': 80, 's2': 85}
# 访问和修改元素
print(dict1['name']) # 'Alice'
dict1['age'] = 26
print(dict1) # {'name': 'Alice', 'age': 26}
# 获取键、值、键值对
print(dict1.keys()) # dict_keys(['name', 'age'])
print(dict1.values()) # dict_values(['Alice', 26])
print(dict1.items()) # dict_items([('name', 'Alice'), ('age', 26)])
标签: urllib