Python有五种数据结构,分别是数字、字符串、列表、元组、字典。
1、数字
Python Number 数据类型用于存储数值。数据类型包括长整型、整型、浮点型、复数。
主要有两块要涉及到数字运算:
- 数学相关的函数Python 的math模块,和数学相关的函数基本都在math包里。
import math
# ceil函数返回数字的上入整数
print(math.ceil(4.1))
# 5
# floor函数返回数字的下舍整数
print(math.floor(4.9))
# 4
# fabs返回数字的绝对值
print(math.fabs(-10))
# 10.0
# sqrt返回数字的平方根
print(math.sqrt(9))
# 3.0
# exp返回e的多少次幂
print(math.exp(1))
# 2.718281828459045
- 随机数
random包可以产生随机小数和随机整数。
import random
# 产生[0,1)的随机小数
a = random.random()
print(a)
# 0.05989048226413196
# 产生一定范围的整数
b = random.randint(1, 10)
print(b)
# 2
2、字符串 ’ ’
# +号
a = 'hello '
b = 'world'
print(a+b)
# hello world
# *号
print(a*3)
# hello hello hello
# 截取字符串 左开右闭
print(a[1:3])
# el
# 判断字符 in 、not in
print('e' in a)
# True
print('e' not in a)
# False
# 1、2、3个引号仍然输出
print('''11111''')
# 11111
3、列表 []
# 列表
# 类似其他语言中的数组
#声明列表
names = ['jack','tom','tonney','superman','jay']
print(names[0])
#jack
print(names[-1])
# jay
# 遍历列表
for name in names:
if name == 'tom':
print("存在tom")
else:
print("没有tom")
# 没有tom
# 存在tom
# 没有tom
# 没有tom
# 没有tom
# or
if 'tom' in names:
print("存在tom")
else:
print("没有tom")
# 存在tom
# 列表元素添加,列表末尾追加
num = ['1', '2', '3']
num.append('4')
print(num)
# ['1', '2', '3', '4']
# 合并列表
num1 = ['5', '6']
num.extend(num1)
print(num)
# ['1', '2', '3', '4', '5', '6']
num1.extend(num)
print(num1)
# ['5', '6', '1', '2', '3', '4']
# 指定位置添加
num.insert(1, '1.5')
print(num)
# ['1', '1.5', '2', '3', '4', '5', '6']
# 指定某个元素替换成另一个元素
# 先遍历列表,完成替换
fruits = ['apple','pear','香蕉','pineapple','草莓']
for fruit in fruits:
if 'apple' in fruit:
fruit = '苹果'
print(fruits)
# ['apple', 'pear', '香蕉', 'pineapple', '草莓']
for i in range(len(fruits)):
if 'apple' in fruits[i]:
fruits[i] = '苹果'
break
print(fruits)
# ['苹果', 'pear', '香蕉', 'pineapple', '草莓']
# 列表删除
del(fruits[0])
print(fruits)
# ['pear', '香蕉', 'pineapple', '草莓']
fruits.remove('pear')
print(fruits)
# ['香蕉', 'pineapple', '草莓']
print(fruits.pop(-1))
# 草莓
# 列表中截取元素叫做切片
pick = ['1', '2', '3', '4']
print(pick[0:1])
print(pick[0:3:2])
# ['1']
# ['1', '3']
# 对列表中的元素进行排序
import random
random_list=[]
for i in range(10):
ran = random.randint(1, 20)
if random not in random_list:
random_list.append(ran)
print(random_list)
random_list1 = []
i = 0
while i<10:
ran = random.randint(1, 20)
if ran not in random_list1:
random_list1.append(ran)
i+=1
print(random_list1)
# sorted默认是升序
new_list = sorted(random_list1)
print(new_list)
new_list1 = sorted(random_list1, reverse=True)
print(new_list1)
# [2, 5, 6, 7, 9, 14, 15, 16, 18, 20]
# [20, 18, 16, 15, 14, 9, 7, 6, 5, 2]
4、元组()
# 元组
# 当元组只有一个元素时,要在元组后面加一个逗号,否则就是一个数了,不是元组了
tuple1 =('hello',)
print(tuple1)
# ('hello',)
tuple1 =('hello')
print(tuple1)
# hello
# 元组是不能修改的,因此不能往元组里面加入元素
# 作为容器时,通过tuple()将列表变为元组
import random
random_list = []
for i in range(10):
ran = random.randint(1, 20)
random_list.append(ran)
print(random_list)
random_tuple = tuple(random_list)
print(random_tuple)
# [15, 3, 2, 13, 14, 17, 8, 3, 18, 13]
# (15, 3, 2, 13, 14, 17, 8, 3, 18, 13)
# random_tuple[::-1]最后一个-1表示将列表的元素
print(random_tuple[::-1])
5、字典 {‘’:‘’,‘’:‘’}
dict1 = {'name':'贾玲', 'weight':60, 'age':30}
print(dict1['name'])
# 贾玲
dict2 = ['name', '沈腾', 'weight','50']
dict3 = {'age':'40'}
# 列表可以转为了元组,但是有求就是列表中的元素是成对的
dict4 = dict([('name','jialing'),('weight',40)])
print(dict4)
# {'name': 'jialing', 'weight': 40}
# 字典里的函数 items() keys() values()
dict5 = {'杨超越':165,'虞书欣':166,'上官喜爱':164}
print(dict5.items())
for key,value in dict5.items():
if value > 165:
print(key)
# dict_items([('杨超越', 165), ('虞书欣', 166), ('上官喜爱', 164)])
# 虞书欣
result = dict5.values()
print(result)
# dict_values([165, 166, 164])
# 若不存在“赵小棠”,会报错KeyError
# print(dict5['赵小棠'])
# KeyError: '赵小棠'
print(dict5.get('杨超越'))
# 165
del dict5['杨超越']
print(dict5)
result1 = dict5.pop('虞书欣')
print(result1)
print(dict5)
# {'虞书欣': 166, '上官喜爱': 164}
# 166
# {'上官喜爱': 164}