一、变量与简单数据类型
1.变量
定义:变量是可以赋给值的标签,也可以说变量指向特定的值。
命名:字母、数字和下划线。(数字不能打头,不能有空格)
使用时出现错误:①未赋值 ②拼写不正确
2.简单数据类型
2.1字符串
2.1.1定义
一系列字符。在python中,用引号括起的都是字符串(“”‘’都可)
2.1.2使用方法修改字符串大小写
(在python中,方法与函数不同,方法是python可对数据执行的操作。)
title()
对字符串中每一个单词首字母大写,其余小写
upper()
对字符串中每一个单词都大写
lower()
对字符串中每一个单词都小写
name = 'aBCdef'
print(name.title())
print(name.upper())
print(name.lower())
print(name)
运行结果
Abcdef
ABCDEF
abcdef
aBCdef
在name.title()
中,[.]让python对变量name执行方法title()
指定的操作。每个方法后面都跟着一对圆括号,这是因为方法通常需要额外的信息来完成其工作。这种信息是在圆括号中提供的。
在示例中可看出,三个方法均未对name产生实质影响。
2.1.3在字符串中使用变量(f ’ ')
要在字符串中插入变量的值,可在前引号加上字母f,再将要插入的变量放在花括号内。
(在’ '中,每一个变量都要一对花括号,还可增加其他字符)
frist_name = 'ada'
last_name = 'lovelace'
full_name = f'{frist_name} {last_name}'
print(full_name)
print(f'Hello, {full_name}!')
运行结果
ada lovelace
Hello, ada lovelace!
2.1.4删除空白
rstrip()
剔除字符串末尾空白
lstrip()
剔除字符串开头空白
strip()
剔除字符串两边空白
text = ' python '
print(text.rstrip())
print(text.lstrip())
print(text.strip())
print(text)
运行结果
python
python
python
python
2.2数(整数与浮点数)
- 只要操作数有浮点数,python默认得到的总是浮点数;
- 将任意两个数相除,结果总是浮点数;
- 数很大时,可使用下划线将其中的数字分组(可不等分),使其更清晰易读,python不会打印出下 划线;
- 同时给多个变量赋值(x,y,z=1,2,3),个数要对应
2.3常量
其值在程序的整个生命周期内保持不变。python没有内置的常量类型,可将全部大写的变量视为常量,其值应始终不变。
2.4注释
python中,注释用(#)号标识。
二、列表
1.定义
由一系列按特定顺序排列的元素组成
2.访问列表元素
指出列表的名称,再指出元素的索引。
通过将索引指定为-1,可让python返回列表最后一个元素。(-2为倒数第二个,以此类推)
bicycles = ['123','trek','666','redline','222']
print(bicycles)
print(bicycles[0])
print(bicycles[-1])
print(bicycles[-2])
print(f'My frist bicycle was a {bicycles[2]}.')
运行结果
[‘123’, ‘trek’, ‘666’, ‘redline’, ‘222’]
123
222
redline
My frist bicycle was a 666.
3.修改、添加和删除元素
3.1修改元素
找出要修改的元素,再指定元素新值
3.2添加元素
append()
在列表末尾添加(圆括号中输入新增元素)
insert()
在列表中插入(圆括号中输入索引+新增元素)
bicycles = ['123','trek','666','redline','222']
bicycles[1] = '888' # 修改
print(bicycles)
bicycles.append('999') # 在列表末尾添加元素
print(bicycles)
bicycles.insert(0,'000')# 在列表中添加元素
print(bicycles)
运行结果
[‘123’, ‘888’, ‘666’, ‘redline’, ‘222’]
[‘123’, ‘888’, ‘666’, ‘redline’, ‘222’, ‘999’]
[‘000’, ‘123’, ‘888’, ‘666’, ‘redline’, ‘222’, ‘999’]
3.3删除元素
3.3.1根据索引删除
del
语句,删除后无法再访问;
方法pop()
,删除列表末尾的元素,并让能够接着使用(类似于出栈)。
bicycles = ['123','trek','666','redline','222']
del bicycles[0]
print(bicycles)
a = bicycles.pop()
print(a)
print(bicycles)
运行结果
[‘trek’, ‘666’, ‘redline’, ‘222’]
222
[‘trek’, ‘666’, ‘redline’]
3.3.2根据值删除
方法remove()
,再圆括号中输入要删除的值即可
bicycles = ['123','trek','666','redline','222']
bicycles.remove('666')
print(bicycles)
运行结果
[‘123’, ‘trek’, ‘redline’, ‘222’]
4.组织列表
4.1方法sort()
对列表永久排序
向方法sort()
传递参数reverse=True
可进行反向排序。
nums = [6,9,88,4,5,0,17,65,3,12]
nums.sort()
print(nums)
nums.sort(reverse=True)
print(nums)
[0, 3, 4, 5, 6, 9, 12, 17, 65, 88]
[88, 65, 17, 12, 9, 6, 5, 4, 3, 0]
4.2函数sorted()
对列表临时排序
向sorted()
函数传递参数reverse=True
可进行反向排序,无论正反向排序均为临时排序。
nums = [6,9,88,4,5,0,17,65,3,12]
print(sorted(nums))
print(nums)
print(sorted(nums,reverse=True))
print(nums)
[0, 3, 4, 5, 6, 9, 12, 17, 65, 88]
[6, 9, 88, 4, 5, 0, 17, 65, 3, 12]
[88, 65, 17, 12, 9, 6, 5, 4, 3, 0]
[6, 9, 88, 4, 5, 0, 17, 65, 3, 12]
4.3倒着打印列表
使用方法reverse()
,可反转列表排序顺序(永久性),恢复原来顺序只需再调用一次。
nums = [6,9,88,4,5,0,17,65,3,12]
nums.reverse()
print(nums)
[12, 3, 65, 17, 0, 5, 4, 88, 9, 6]
4.4列表长度
函数len()
nums = [6,9,88,4,5,0,17,65,3,12]
print(len(nums))
10
三、操作列表
1.遍历列表 (for语句)
for语句循环 for ___ in ___ :
注意缩进 注意冒号
nums = [1,2,3,4,5]
for num in nums:
print(num)
1
2
3
4
5
2.创建数值列表
2.1函数range()
从指定的第一个值开始数,并到达指定的第二个值时停止(不包含该值)
指定第三个参数,可以表示步长
for num in range(1,6):
print(num)
print() #输出空行
for num in range(1,10,2): # 2为步长
print(num)
1
2
3
4
5
1
3
5
7
9
2.2使用range()
创建数值列表
使用函数list()
将range()
的结果转换为列表
nums1 = list(range(1,10))
print(nums1)
nums2 = list(range(1,10,2))
print(nums2)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 5, 7, 9]
range()
创建其他数集,并使用max(),min(),sum()简单统计
nums = [] #创建空表
for num in range(1,10):
nums.append(num**2) #在表尾插入元素
print(nums)
print(max(nums),min(nums),sum(nums))
[1, 4, 9, 16, 25, 36, 49, 64, 81]
81 1 285
2.3列表解析
将for循环和创建新元素的代码合并成一行,并自动附加新元素。这里的for语句没有冒号。
nums = [num**3 for num in range(1,15)]
print(nums)
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744]
3.使用列表的一部分(切片)
处理列表的部分元素,python称之为切片。
3.1切片与遍历切片
列表名称+[ : ]
(注意是冒号)
players = ['charles','martina','michael','florence','eli']
# 切片
print(players[1:3]) #到达第二个索引前停止
print(players[:3]) #自动从列表开头开始
print(players[1:]) #终止于列表末尾
print(players[-4:]) #从倒数第四个开始到末尾
# 遍历切片
for player in players[1:4]:
print(player)
[‘martina’, ‘michael’]
[‘charles’, ‘martina’, ‘michael’]
[‘martina’, ‘michael’, ‘florence’, ‘eli’]
[‘martina’, ‘michael’, ‘florence’, ‘eli’]
martina
michael
florence
3.2复制列表
方法:同时省略起始索引和终止索引([ : ])
players = ['charles','martina','michael','florence','eli']
pro_player = players[:] # 不可省略[:],否则相当于两个变量指向同一个列表
players.append(1) # 分别在两个列表末尾添加元素,检验两个列表是不同的
pro_player.append(6)
print(players)
print(pro_player)
[‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘eli’, 1]
[‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘eli’, 6]
四、元组
列表是可以修改的,python将不能修改的值成为不可变的,不可变的列表被称为元组。元组使用圆括号来标识,给元组变量重新赋值是合法的,但不能修改元组内的元素。*如果定义只包含一个元素的元组,必须在这个元素后加上逗号。
num_s = (1,2,6)
print(num_s)
print(num_s[1]) # 访问元素
print()
for num in num_s: # 遍历元组
print(num)
print()
num_s = (1,) # 重新赋值
print(num_s)
(1, 2, 6)
2
1
2
6
(1,)