Python学习3

组合数据类型

1、序列和索引

a、定义

序列是一个用于储存多个值的连续空间,每个值都对应一个整数编号,称为索引。

索引分为正向递增索引和反向递减索引

# 正向递增
s = 'hello world'
for i in range(0, len(s)):
    print(i, s[i], end='\t\t')
print()
# 反向递减
for i in range(-10, 0):
    print(i, s[i], end='\t\t')
print()

b、切片操作的语法结构

序列[start: end: step]

start:切片的开始索引(包含)

end:切片的结束索引(不包含)

step:步长(默认为1)

# 序列的切片操作
s = 'hello world'
# 切片操作
s1 = s[0:5:2]  # 索引从0开始 到5结束(不包括5) 步长为2
print(s1)
# 省略开始位置 start默认从零开始
print(s[:5:1])
# 省略start和step,默认步长为1
print(s[:5:])
# 省略结束位置
print(s[::1])  # stop 默认到序列的最后一个元素(包括最后一个元素)
# 13和14行代码功能相同 省略了结束和步长
print(s[5::])
print(s[5:])
# 省略开始位置和结束位置 只有步长
print(s[::2])  # 从零开始到s序列最后一个元素结束 步长为2
# 步长为负数
print(s[::-1])  # 逆序输出

输出结果

hlo
hello
hello
hello world
 world
 world
hlowrd
dlrow olleh

c、序列的一些操作

操作符/函数 描述说明
x in s 如果x是s的元素,结果为true,否则结果为False

x not in s

如果x不是s的元素,结果为True,否则结果为False
len(s) 序列s中元素的个数(即序列的长度)
max(s) 序列s中元素的最大值
min(s) 序列s中元素的最小值
s.index(x) 序列s中第一次出现元素x的位置
s.count(x) 序列s中出现x的总次数
# 序列的相加操作
s1 = 'hello'
s2 = 'world'
print(s1+s2)  # 产生一个新的字符串序列
# 序列的相乘操作
print(s1*5)  # 输出5个连续的hello
# x in s
s = 'hello world'
print('e在hello world中存在吗?', ('e' in s))
print('e在hello world中存在吗?', ('v' in s))
# not in
print('e在hello world中不存在吗?', ('v' not in s))
# 内置函数的使用
print('len():', len(s))
print('max():', max(s))
print('min():', min(s))
# 序列对象的方法,使用序列的名称,打点调用
print('s.index():', s.index('o'))  # o在s中第一次出现的索引位置
# print('s.index():', s.index('v'))  # 报错 v在字符串中不存在
print('s.count():', s.count('o'))  # 统计o在字符串中出现的次数

运行结果:

helloworld
hellohellohellohellohello
e在hello world中存在吗? True
e在hello world中存在吗? False
e在hello world中不存在吗? True
len(): 11
max(): w
min():  
s.index(): 4
s.count(): 2

2、列表类型

列表:是指一系列的按特定顺序排列的元素组成;是Python中内置的可变序列;在Python中使用[]定义列表,元素与元素之间使用英文的逗号分隔;列表中的元素可以是任意的数据类型。

列表创建的方式有:

a、使用[]直接创建列表

语法结构:列表名=[element1,element2,……,elementN]

b、使用内置函数list()创建列表

语法结构:列表名=list(序列)

c、列表的删除:

语法结构:del 列表名 

# 列表的创建与删除
# 直接使用[]创建列表
lst = ['hello', 'world', 98, 100.5]
print(lst)

# 使用内置的函数list()创建列表
lst2 = list('helloworld')
lst3 = list(range(1, 10, 2))  # 从1开始到10结束 步长为2 不包括10
print(lst2)
print(lst3)
# 列表是序列中的一种 对于列表的操作符、运算符、函数均可以使用
print(lst+lst2+lst3)  # 序列的相加操作
print(lst*3)  # 序列的相乘操作
print(len(lst))
print(max(lst3))
print(min(lst3))
print(lst2.count('o'))  # 统计o的个数
print(lst2.index('o'))  # o在列表lst2中第一次出现的位置
# 列表的删除操作
lst4 = [10, 20, 30]
print(lst4)
# 删除列表
del lst4
# print(lst4)  # NameError: name 'lst4' is not defined. Did you mean: 'lst'?

d、enumerate函数的使用

语法结构:

for index, item in enumerate(lst):

    输出index和item

# 列表的遍历操作
lst = ['hello', 'world', 'python', 'php']
# for循环遍历
for item in lst:
    print(item)
# 使用for循环,range()函数,len()函数,根据索引进行遍历
for i in range(0, len(lst)):
    print(i, '-->', lst[i])
# enumerate()函数进行遍历
for index, item in enumerate(lst):
    print(index, item)  # index是序号,不是索引,可以修改
# 手动设置序号的起始值
for index, item in enumerate(lst, start=1):
    print(lst, item)
for index, item in enumerate(lst, 1):  # 省略start不写,直接写起始值
    print(lst, item)

总结:emm...因为下周就是期末考试,这周基本上在复习,Python的内容就学习得比较少,可能下周也会很少,因为考试要考到星期天...最后就祝愿自己考试顺利吧~

你可能感兴趣的:(学习)