Python中的元组介绍

作者:Mr.o.j 时间:2021-04-09 19:56:37 

1.元组的创建

元组(tuple):元组本身是不可变数据类型,没有增删改查

元组内可以存储任意数据类型


t = (1,2.3,True,'star')   ##例如这里面有数字,波尔值,和字符
print(t)
print(type(t))

Python中的元组介绍

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


t1 = ([1,2,3],4)    ##里面含有一个数组,可以改变里面数组的值
t1[0].append(4)
print(t1)

Python中的元组介绍

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


t2 = ('hello',)  
t3 = (1,)
print(type(t2))
print(type(t3))

Python中的元组介绍

2.元组的特性

下面是举例子用的元组


allowUsers = ('root','westos','redhat')
allowPasswd = ('123','456','789')

1)索引和切片


print(allowUsers[0])
print(allowUsers[-1])
print(allowUsers[1:])
print(allowUsers[2:])
print(allowUsers[:-1])
print(allowUsers[::-1])

Python中的元组介绍

2)重复


print(allowUsers * 3)

3)连接


print(allowUsers + ('linux','python'))

Python中的元组介绍

4)成员操作符


print('westos' in allowUsers)
print('westos' not in allowUsers)

Python中的元组介绍

5)for循环


for user in allowUsers:
 print(user)

Python中的元组介绍


for index,user in enumerate(allowUsers):
 print('第%d个白名单用户: %s' %(index+1,user))

Python中的元组介绍

6)zip:两个元组之间的元素相互对应

Python中的元组介绍

3.元组的常用方法


t = (1,2.3,True,'westos','westos')
print(t.count('westos'))
print(t.index(2.3))

Python中的元组介绍

4.元组的应用场景

1)变量交换数值

现在给变量赋值,a=1,b=2。如何使用元组快速的将a和b的值互换


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

这样就将a,b的值互换了

2)打印变量的值


name = 'westos'
age = 11
t = (name,age)
print('name:%s , age:%d' %(name,age))
print('name:%s , age:%d' %t)

Python中的元组介绍

3)元组的赋值,有多少个元素,就用多少个变量


t = ('westos',11,100)
name,age,score = t
print(name,age,score)

Python中的元组介绍

4)排序加元组的赋值


score = (100,89,45,78,65)
# scoreLi = list(score)
# scoreLi.sort()
# print(scoreLi)
scores = sorted(score)
# print(scores)
minscore,*middlescore,maxscore = scores
print(minscore)
print(middlescore)
print(maxscore)
print('最终成绩为: %.2f' %(sum(middlescore) / len(middlescore)))

Python中的元组介绍

来源:https://blog.csdn.net/weixin_40543283/article/details/86617822

标签:python,元组
0
投稿

猜你喜欢

  • sql2000如何完美压缩.mdf文件

    2010-03-03 15:47:00
  • python对象及面向对象技术详解

    2023-05-14 00:04:16
  • vbscript与javascript如何传递变量(包括服务器端与客户端)

    2008-04-09 13:46:00
  • PHP JSAPI调支付API实现微信支付功能详解

    2023-05-29 05:09:13
  • 详解python读取matlab数据(.mat文件)

    2021-03-04 19:29:29
  • Selenium之模拟登录铁路12306的示例代码

    2022-01-22 17:06:27
  • 白 刃之战:PHP vs. ASP.NET(节选)-架构比较

    2023-11-15 12:31:22
  • Oracle 中文字段进行排序的sql语句

    2009-09-26 18:58:00
  • 源码解析python的内存回收机制

    2023-05-19 18:12:16
  • Python如何输出整数

    2022-03-30 07:19:35
  • 浅谈Python使用Bottle来提供一个简单的web服务

    2021-08-30 13:03:19
  • Python实现base64编码的图片保存到本地功能示例

    2024-01-01 22:58:03
  • JSP EL表达式详细介绍

    2023-07-02 22:32:32
  • 基于python 处理中文路径的终极解决方法

    2023-07-21 00:47:09
  • pandas按照列的值排序(某一列或者多列)

    2022-08-14 08:54:08
  • 详谈javascript异步编程

    2023-08-23 17:49:53
  • 关于获取HTML元素的CSS属性值函数

    2008-09-01 13:20:00
  • asp之自动闭合HTML/ubb标签函数附简单注释

    2011-04-04 11:18:00
  • 使用numba对Python运算加速的方法

    2022-05-12 20:30:57
  • python按行读取文件,去掉每行的换行符\\n的实例

    2022-06-01 03:49:43
  • asp之家 网络编程 m.aspxhome.com