To revisit the Python list base - operation

Learn Python easily 2022-11-08 04:24:58 阅读数:478

revisitpythonlistbaseoperation

序言

哈喽兄弟们,Let's review it todayPythonList operations in the basics,I don't know how much you remember?

Iterates over the entire list of bold styles

遍历列表的所有元素,Do the same for each element,is what we often need.在python中可使用for()循环.

Suppose we need to print the mobile phone brands in a list one by one,We can get the name of the mobile phone brand separately.If the data is special,Our operations are very demanding,而且容易输入错误,但使用for循环,python可以帮你解决这些问题.

例如:

shouji=['iphone','huawei','xiaomi','vivo','oppo']
for shoujis in shouji:
print(shoujis)

运行结果

iphone
huawei
xiaomi
vivo
oppo

深入研究循环

The concept of looping is very important,It is one of the common ways for computers to automate repetitive tasks.
pythonFirst read the first line of commands for shoujis in shouji: 这让python获取到列表shouji的第一个值,iphone,然后下一行 print(shoujis),Then print out the first value read iphone ,如此循环.

在forExecute more commands in a loop

在forThe loop can perform arbitrary operations on each element.The following demonstration prints a message to each mobile phone

shouji=['iphone','huawei','xiaomi','vivo','oppo']
for shoujis in shouji:
print(f"{
shoujis.title()} it is a good phone")

运行结果

Iphone it is a good phone
Huawei it is a good phone
Xiaomi it is a good phone
Vivo it is a good phone
Oppo it is a good phone

在for循环后面,Every indented code is part of a loop.will be executed once for each value of the list.

在for循环结束后执行一些操作

在for循环后面,没有缩进的代码只执行一次,不会重复执行.Do it now and give five-element praise to all mobile phones,Code is not indented.

shouji=['iphone','huawei','xiaomi','vivo','oppo']
for shoujis in shouji:
print(f"{
shoujis} it is a good phone")
print("I give a five star review")

运行结果

iphone it is a good phone
huawei it is a good phone
xiaomi it is a good phone
vivo it is a good phone
oppo it is a good phone
I give a five star review

When starting to write properly indented code,There are common indentation mistakes to watch out for,对于forThe loop section and belongs to the lines of code that make up the loop,一定要缩进!Sometimes forgetting to indent code will run without reporting an error,The results may well surprise you.

使用函数range()

range()Can easily help you to produce a series of numbers
例如:

for value in range(1,5):
print(value)

运行结果

1
2
3
4

The above code seems to print1到5,Does not actually print5.This is a result of the one-off line behavior common in editing languages.如果需要打印5,则需要使用
for value in range(1,6):
调用函数**range()**时,You can also specify a parameter to print,他将从0开始.

使用range()创建数字列表

要创建数字列表,可使用函数list()将range()的结果直接转换为列表.如果将range()作为list()的一个参数,The output will be a list of time digits.

numbers = list(range(1,6))
print(numbers)

运行结果

[1, 2, 3, 4, 5]

使用rangeThe function can also specify a step size

numbers = list(range(1,6,2))
print(numbers)

运行结果

[1, 3, 5]

使用range()You can still create as many sets as you want,For example, in0——10The set of even squared numbers

squares = []
for numbers in range(0,11,2):
square = numbers ** 2
squares.append(square)
print(squares)

运行结果

[0, 4, 16, 36, 64, 100]

切片

要创建切片,You can specify the index of the first and last element to use.与函数range()一样pythonArrives until the second index is reached.

You can produce any subset of the list,If you want to extract the first of the list1,2,3个数,可将起始索引指定为0,The termination index is specified as 3.

If no index is specified for start thenpythonExtraction will start at the beginning
If the termination does not specify an index thenpythonwill end at the end

value = list(range(1,11))
print(value[0:3])
print(value[4:])
print(value[:4])

运行结果

[1, 2, 3]
[5, 6, 7, 8, 9, 10]
[1, 2, 3, 4]

元组

列表非常适合用于存储在程序运行期间可能变化的数据集.The list can be modified.But sometimes it is necessary to create a series of unmodifiable elements,元组可以满足这种需求.
元组看起来很像列表,Use parentheses instead of square brackets to identify.定义元组后,Elements can be accessed using indices,就像访问列表一样.
The following example manipulates tuples and attempts to modify tuples.

name = ('xiao','xiong')
print(name[0])
print(name[1])
name[1]='old'

运行结果

Traceback (most recent call last):
File "C:\Users\Desktop\python\py1.py", line 195, in <module>
name[1]='old'
TypeError: 'tuple' object does not support item assignment
xiao
xiong

由此可见,If changes are made to the data inside the tuple,则会出现报错.
According to the method of list access, it can be accessed normally.

遍历元组中的所有值

像列表一样,可以使用for循环来遍历元组中的所有值

numbers = (1,2,3,4,5,6)
for number in numbers:
print(number)

#运行结果

1
2
3
4
5
6

修改元组变量

虽然不能修改元组的元素,But variable assignments that can store tuples.So the whole tuple can be traversed:

numbers = (1,2,3,4,5,6)
print("original numbers")
for number in numbers:
print(number)
numbers = (11,22,33,44,55,66)
print("change numbers")
for number in numbers:
print(number)

运行结果

original numbers
1
2
3
4
5
6
change numbers
11
22
33
44**加粗样式**
55
66

首先定义一个元组,and print out its stored data,Next associate a new tuple to the variablenumbers,Then print the new data.This time it compiles successfully,因为给元组变量重新赋值是合法的.

写在最后

小伙伴们在学习Python的过程中,有时候不知道怎么学,从哪里开始学.掌握了一些基本的知识或者做了一些案例后,不知道下一步怎么走,不知道如何去学习更加高深的知识.
那么对于这些大兄弟们,我准备了大量的免费视频教程,PDF电子书籍,以及源代码!You can pick up your business card directly below~

今天的分享就到这里结束了,I wish you all the best of luck in your studies!

版权声明:本文为[Learn Python easily]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/312/202211080408576022.html