Master all the methods of using python lists in one article (zero-based learning python (1))

the list

A list in Python is a mutable data type that can store multiple values ​​and elements can be added, removed, or modified at any time. The following are the basic operations and sample codes for Python lists:

create list

You can use square brackets [] to create an empty list, or add elements within square brackets to create a non-empty list. For example:


empty_list = []
number_list = [1, 2, 3, 4, 5]
string_list = ["apple", "banana", "cherry"]
mixed_list = [1, "apple", True, 3.14]

access list element

Elements in a list can be accessed by their index. Indexing in Python starts at 0, so the first element has index 0, the second element has index 1, and so on. For example:

fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # 输出 "apple"
print(fruits[1])  # 输出 "banana"
print(fruits[2])  # 输出 "cherry"

modify list elements

An element in a list can be modified by its index. For example:

fruits = ["apple", "banana", "cherry"]
fruits[1] = "orange"
print(fruits)  # 输出 ["apple", "orange", "cherry"]

add element

You can use the append() method to add an element to the end of the list, or you can use the insert() method to add an element anywhere in the list. For example:

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)  # 输出 ["apple", "banana", "cherry", "orange"]

fruits.insert(1, "lemon")
print(fruits)  # 输出 ["apple", "lemon", "banana", "cherry", "orange"]

delete element

You can use the remove() method to delete an element in the list, or you can use the del statement to delete an element at any position. For example:

fruits = ["apple", "lemon", "banana", "cherry", "orange"]
fruits.remove("lemon")
print(fruits)  # 输出 ["apple", "banana", "cherry", "orange"]

del fruits[1]
print(fruits)  # 输出 ["apple", "cherry", "orange"]

list slice

Slicing can be used to obtain a sublist of a list. Slices use square brackets and colons to specify the start index and end index, separated by a colon. For example:

fruits = ["apple", "banana", "cherry", "orange", "lemon"]
print(fruits[1:3])  # 输出 ["banana", "cherry"]
print(fruits[:2])  # 输出 ["apple", "banana"]
print(fruits[3:])  # 输出 ["orange", "lemon"]

list length

You can use the len() function to get the length of a list. For example:

fruits = ["apple", "banana", "cherry"]
print(len(fruits))  # 输出 3

list sort

The list can be sorted using the sort() method. For example:

numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
numbers.sort()
print(numbers)  # 输出 [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

list reverse

The elements of a list can be reversed using the reverse() method. For example:

fruits = ["apple", "banana", "cherry"]
fruits.reverse()
print(fruits)  #输出 ["cherry", "banana", "apple"]

That's it for basic operations and example code for lists in Python. Hope this helps you better understand and use lists in Python.

Python operator

Python arithmetic operators
Let variable a=10, variable b=21:

operator description

+	加 - 两个对象相加	a + b 输出结果 31
-	减 - 得到负数或是一个数减去另一个数	a - b 输出结果 -11
*	乘 - 两个数相乘或是返回一个被重复若干次的字符串	a * b 输出结果 210
/	除 - x 除以 y	b / a 输出结果 2.1
%	取模 - 返回除法的余数	b % a 输出结果 1
**	幂 - 返回x的y次幂	a**b 为10的21次方
//	取整除 - 往小的方向取整数	

code practice

a = 21
b = 10
c = 0
 
c = a + b
print ("1 - c 的值为:", c)
 
c = a - b
print ("2 - c 的值为:", c)
 
c = a * b
print ("3 - c 的值为:", c)
 
c = a / b
print ("4 - c 的值为:", c)
 
c = a % b
print ("5 - c 的值为:", c)
 
# 修改变量 a 、b 、c
a = 2
b = 3
c = a**b 
print ("6 - c 的值为:", c)
 
a = 10
b = 5
c = a//b 
print ("7 - c 的值为:", c)

The output of the above example:

1 - c 的值为: 31
2 - c 的值为: 11
3 - c 的值为: 210
4 - c 的值为: 2.1
5 - c 的值为: 1
6 - c 的值为: 8
7 - c 的值为: 2

Guess you like

Origin blog.csdn.net/ALiLiLiYa/article/details/130813006