python- 元组tuple

1.元组的创建 
2.元组的特性

    -1索引
    -2切片
    -3连接
    -4重复
    -5for循环
    -6成员操作符

3.元组的常用方法 

    -1索引
    -2出现的次数

4.元组的应用场景

    -1.变量交换数值 
    -2.打印变量值 
    -3.元组的赋值 
    -4.比赛计分器、排序 

1.元组的创建
列表:打了激素的数组
元组:带了紧箍咒的列表,不可变数据类型,没有增删改查,可以存储任意数据类型。

#定义元组

t = (1,1.2,True,'westos')	#元组为(圆括号)
print(t,type(t))
(1, 1.2, True, 'westos') <class 'tuple'>

#如果元组里面包含可变数据类型,可以间接修改元组内容

t1 = ([1, 2, 3], 4)	
t1[0].append(4)	#索引值为0处增加元素
print(t1)

([1, 2, 3, 4], 4)

#元组与列表类型转换

t2 = ()
print(t2)

()

t3 = tuple([])
print(t3)	#列表转换为元组

()

l4 = list(())	#元组转换为列表
print(l4)

[]

#元组如果只有一个元素,后面一定要加逗号,否则数据类型不确定

t4 = (1,)
print(t4,type(t4))
    
(1,) <class 'tuple'>

2.元组的特性
-1索引
-2切片
-3连接
-4重复
-5for循环
-6成员操作符

-1索引

tuple = (1,1.0,True,'westos')
# 索引
print(tuple[0])
print(tuple[-1])

-2切片

tuple = (1,1.0,True,'westos')
# 切片
print(tuple[1:])
print(tuple[:-1])
print(tuple[::-1])

-3连接

# 连接
# 不同的数据类型可以连接麼(除了数值类型之外,不同的数据类型之间不可以连接)
tuple = (1,1.0,True,'westos')
print(tuple + (1,2,3))

(1, 1.0, True, 'westos', 1, 2, 3)

tuple = (1,1.0,True,'westos')
print(tuple + [4,5,6])	#不同数据类型不可以连接
     File "F:/python/python1125/day04/02_元组的特性.py", line 19, in <module>
    print(tuple + [4,5,6])
TypeError: can only concatenate tuple (not "list") to tuple

tuple = (1,1.0,True,'westos')	#不同数据类型不可以连接
print(tuple + 'westos')
    
 File "F:/python/python1125/day04/02_元组的特性.py", line 19, in <module>
    print(tuple + [4,5,6])
TypeError: can only concatenate tuple (not "list") to tuple

-4重复

#重复

tuple = (1,1.0,True,'westos')
print(tuple * 5)

(1, 1.0, True, 'westos', 1, 1.0, True, 'westos', 1, 1.0, True, 'westos', 1, 1.0, True, 'westos', 1, 1.0, True, 'westos')

-5for循环

# for循环
tuple = (1,1.0,True,'westos')
for i in tuple:
    print(i)
 
1
1.0
True
westos

-6成员操作符

#成员操作符
tuple = (1,1.0,True,'westos')
print(1 in tuple)

True

tuple = (1,1.0,True,'westos')
print(1 not in tuple)

False

3.元组的常用方法
-1索引
-2出现的次数

t = (1,2,'a','c','a')
# 查看元素的索引值
print(t.index('c'))
# 查看元素在元组中出现的次数
print(t.count('a'))

4.元组的应用场景
-1.变量交换数值
-2.打印变量值
-3.元组的赋值
-2.比赛计分器、排序

-1变量交换数值

a = 1
b = 2
b,a = a,b
"""
1.先把(a,b)封装成了一个元组 (1,2)
2. b,a = a,b ===> b,a = (1,2)
3. b = (1,2)[0] a =(1,2)[1]
"""
print(a,b)

2 1

-2.打印变量值

#变量交换数值有多少个元素,就用多少个变量接收

name = 'westos'
age = 11
t = (name, age)				#创建元组
print('name:%s,age:%s' % (name, age))	
print('name:%s,age:%s' %(t))		#两种方法都可以打印变量值

-3.变量的赋值:变量交换数值有多少个元素,就用多少个变量接收

t = ('Westos',10,100)	#变量有三个元素
name,age,score = t	#则用三个变量接收
print(name,age,score)

-4.元组的排序:

两种方法:
1.类型转换为列表用.sort()排序

scores = (100,89,45,78,65)
#先对元组进行转换
scoreli = list(scores)
scoreli.sort()
print(scoreli)

[45, 65, 78, 89, 100]

2.sorted()直接排序

scores = (100,89,45,78,65)
scores_sort = sorted(scores)
print(scores_sort)

[45, 65, 78, 89, 100]

比赛计时器:

scores = (100, 89, 98, 60, 90, 67)
# 或scores.sort()
scores = sorted(scores)
# python2中*middle不能使用的
min_score, *middle, max_score = scores
print(min_score, '~~~~', *middle, '~~~~~~', max_score)
print('最终成绩为:%s' % (sum(middle) / 4))

60 ~~~~ 67 89 90 98 ~~~~~~ 100
最终成绩为:86.0

猜你喜欢

转载自blog.csdn.net/weixin_43067754/article/details/84573110