首先简单的说一下元组,元组开玩笑地说就是带了枷锁的序列,之所以这么说是因为元组不向序列那样可以随意的对其元素进行一系列的操作,一旦定义了一个元组之后,元组中的元素就不能随意改变了。
元组的定义:元组名=(,,,,,,)
注意元组的标志不是(),而是,
元祖就说那么多,接下来补充说一下字符串的格式化的问题。
不啰嗦,直接上干货。
#字符串的格式化print("------Format函数的使用------") #Format函数含有未知参数和关键字参数 #replacement字段用{}来代替 #前面叫做未知参数,后面叫做关键字参数 print("{0} love {1} {2}".format("I","FishC","com"))print("\n") #关键字参数 #下面这样写是错误的,因为编译器并不知道a,b,c分别对应谁 #print("{a} love {b} {c}".format("I","FishC","com")) print("{a} love {b} {c}".format(a="I",b="FishC",c="com")) #或者将关字参数与未知参数混合起来使用,但是 #未知参数要在关键字参数的前面 print("\n")print("{0} love {b} {c}".format("I",b="FishC",c="com")) #使用转义字符打印花括号print("\n")print("{{0}}".format("转义打印花括号")) #冒号表示格式化的开始print("{0:.1f}{1}".format(27.658,"GB"))
输出的结果为:
再说一下序列的相关问题,序列就是列表、元组、字符串的统称,之所以把它们三个放在一起,是因为他们之间存在着一些共性。今天主要学习的是序列的内置函数,直接上干货。
#序列序列 #列表 元组 字符串 统称为序列 #list()函数作用是将一个可迭代对象转换为列表 #若没有参数,则生成一个空列表 #若有一个参数,该参数是迭代器print("使用list()函数生成一个空的列表") a=list(); print(a,"\n")#list函数的过程可以理解为先建立一个空的序列 #然后用for循环采用索引函数将元素移到新的序列中 print("使用list(iterator)函数生成一个非空的列表") a="I Love You"atolist=list(a) print(atolist) print("\n")#tuple([iterator])把一个可迭代的对象转换为元组atotuple=tuple(a) print(atotuple) print("\n")#str(obj) 函数:将obj对象转换为字符串,强制类型转换#len(sub) 函数:返回序列的长度#max(),min()函数保证序列中的数据类型统一 #max() 函数:返回序列或者参数集合中的最大值print(max(1,2,3,4,5)) #min() 函数:返回序列或者参数集合中的最大值#sum(iterable[,start=0])返回序列iterable和可选参数start的总和#数列中的数据类型也必须保持一致,必须是数字print("\n") tuple1=(1,2,3,4,5) print(sum(tuple1)) print("\n")#**sorted**函数实现对序列的排序,默认是从小到大#注意与列表的内置函数sort区分list1=[1,5,4,3,6] list1=sorted(list1) print(list1) print("\n")#**reversed()**实现序列元素的逆转,但是返回的不是序列#注意与列表的内置函数reverse()区分#而是一个迭代的对象print(reversed(list1)) print("\n")#我们可以将返回的迭代器的对象作为list(iterator)的参数#返回一个列表print(list(reversed(list1))) print("\n")#zip合成序列a=[1,2,3,4,5,6,7,8] b=["a","b","c","d"] zip(a,b) print(list(zip(a,b)))
程序运行如下:
主要还是记住函数的用法就可以,没什么很需要费脑细胞的东西。
以上就是Python零基础入门之五字符串的格式化以及序列的内置函数的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,负索引,切片,口头化。1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具