首页 > 编程语言 >python中元组的学习

python中元组的学习

时间:2024-07-31 10:38:58浏览次数:8  
标签:tuple python 元素 元组 学习 print 方法 hello

元组


目录

元组的概念

  • Tuple(元组)与列表相似,不同之处遭遇元组的元素不能修改
  • 元组表示多个元素组成的序列
  • 用于储存一串信息,数据之间使用,分隔
  • 元组用()定义
# 元组的创建
info_tuple = ("zhangsan",18,1.75)
info_tuple2 = (1,)  # 只有一个元素时要加一个逗号

tuple3 = tuple('hello')  # tuple():类型转换
tuple4 = tuple([3,4,5])  # 列表-->元组

元组操作

# 索引
print(tuple[-1])
# 切片
print(tuple[::-1])
# 计算元素个数
print(len(tuple))
# 取元组中最大值最小值
print(max(tuple),min(tuple))
# 删除元素del
del tuple()
print(tuple)

元组的常用方法

a=tuple.count('hello')
print(a)  # 应该输出tuple中hello的个数

b=tuple.index(2)
print(b)  # 应该输出2在tuple中的索引值

元组的遍历

# 方法一:
for i in tuple:
    print(i)

# 方法二:
for index,value in enumerate(tuple):
    print(inex,value)

#方法三:
for i in range(len(tuple)):
    print(i,tuple[i])

标签:tuple,python,元素,元组,学习,print,方法,hello
From: https://www.cnblogs.com/BingBing-8888/p/18334112

相关文章

  • 尝试通过Python访问.zip文件中的.gz文件
    我有一个包含大量.gz文件的.zip文件,我需要对其进行处理。我想打开.zip,我可以通过以下代码轻松完成:zf=zipfile.ZipFile("file.zip","r")forgzfileinzf.filelist:withgzip.GzipFile(fileobj=zf.open(gzfile.filename,"r"),mode="r")asf:df......
  • python导入包报错ImportError: cannot import name ‘Protocol‘
    python32.pyTraceback(mostrecentcalllast):File"2.py",line5,in<module>importptwt#use"fromsrcimportptwt"foraclonedtherepoFile"……lib/python3.6/site-packages/ptwt/_util.py",line2......
  • Python - Creating your own Iterator
    Inourfirstexample,wewillcreateiterableobjects,which,wheniteratedover,willgiveoutcubesofnumbers,andtheseobjectswillsupportmultipleiterations.classCubes:def__init__(self,start,stop):self.start=startsel......
  • 无法从Android恢复删除的文件该怎么办?5 个方法可以学习参考下
    Android设备已成为我们生活中不可或缺的一部分,充当重要文件、照片和文档的存储中心。但是,意外时有发生,有时我们会不小心从Android设备中删除重要文件。好消息是,有一些方法可以从Android恢复永久删除的文件。在这篇博文中,我们将探讨各种方法和解决方案,以帮助您检索有价值的数据......
  • 三种语言实现前缀和(C++/Python/Java)
    题目输入一个长度为n的整数序列。接下来再输入m个询问,每个询问输入一对l,r对于每个询问,输出原序列中从第l个数到第r个数的和。输入格式第一行包含两个整数n和m。第二行包含n个整数,表示整数数列。接下来m行,每行包含两个整数l和r,表示一个询问的区间范围。......
  • Python - 旨在通过命令提示符执行数据清理,但代码似乎无法运行
    我从一位同事那里收到了这段代码,我打算用它来处理100csv文件以提取有关粒子的值。代码如下所示:importsysimportcsv#Usage#skdata_decode.py[inputfile1][inputfile2]...#(Itispossibletousefiledcardtospecifyinputfiles.)##l......
  • 如何在 python 终端中的 x,y 位置上书写(基于文本)
    我想在python(基于文本)的终端中的定义位置(x,y)上写入字符。假设,我有一个大小为25x80的终端,并且想要在位置(2,20)上写入字符。我可以在Python中执行此操作吗?现在,我使用25x80数组,并写入该数组。为了在屏幕上显示,我清除屏幕并将该数组的全部内容写入屏幕,但这效......
  • Python - Composition
     classEngine:def__init__(self,power):self.power=powerdefstart(self):self.draw_current()self.spin()self.ignite()defdraw_current(self):print('Drawingcurrent')defspin(sel......
  • Python - Iterator vs Iterable
    Therearemanybuilt-infunctionsandmethodsthatreturniterablesanditerators.Hereareafewexamples:range()returnsaniterabledict.keys()returnsaniterabledict.items()returnsaniterabledict.values()returnsaniterableenumerate()returns......
  • 在python中使用变量引用Panda列名称
    我正在尝试编写一个函数来简化我的代码,因此我传递了包含列名称的变量。它适用于Django应用程序,调试器不会对我的错误所在提供任何反馈,只是“内部服务器错误”。我的代码工作正常,不是作为函数编写的:df_trips['trip_time_prep_starts']=df_trips["trip_time_prep_sta......