


Python zero-based introduction 5 String formatting and sequence built-in functions
First of all, let’s briefly talk about tuples. Tuples are jokingly said to be shackles of sequences. The reason why we say this is because tuples cannot perform a series of operations on their elements at will like sequences. Once defined After a tuple, the elements in the tuple cannot be changed at will.
Definition of tuple: tuple name = (,,,,,,,)
Note that the mark of the tuple is not (), but,
That’s all the ancestor said, let’s add some characters String formatting problem.
Don’t be too long-winded, let’s get straight to the essentials.
#字符串的格式化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"))
The output result is:
#Let’s talk about sequence-related issues. Sequence is the collective name for lists, tuples, and strings. The reason why they are The three are put together because there are some commonalities between them. Today we are mainly learning about the built-in functions of sequences, and we will go directly to the practical information.
#序列序列 #列表 元组 字符串 统称为序列 #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)))
The program runs as follows:
#The main thing is to remember the usage of the function, there is nothing that requires a lot of brainpower Cell stuff.
The above is the content of Python Zero Basic Introduction to String Formatting and Sequence Built-in Functions. For more related content, please pay attention to the PHP Chinese website (www.php. cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
