Python knowledge notes (+4): popular understanding of concepts such as list (List), tuple (Tuple) and string (String)

Since I was brushing Leetcode questions recently, I encountered some basic concepts that I did not understand very well, so I need to supplement the basic knowledge. Therefore, the following are some basic concepts about List, Tuple and String that I have reviewed and summarized by myself.

Understanding sequences in python (lists, tuples and strings)

1.1 Basic Concepts

The data structures commonly used in python are generally sequences , and we almost always use sequences when programming. Each element in the sequence has a number, that is, an index, the index of the first element is 0, and so on, while the negative index is used to indicate the position of the element at the end of the sequence

The sequence mainly includes:listtupleandstringWait. (These three are the most common)

  • 1) The basic format is as follows: lists are generally used [], tuples are generally used (), and strings are generally " "used to represent, pay attention to the difference in their representation.
l = [1,2,3,4,5]   #列表
t =54321#元组
s = "Hello"  #字符串
  • 2) The difference between lists and tuples and strings: lists can be modified, while tuples and strings cannot .
  • 3) Sequences can use operations such as indexing, slicing, adding, multiplying, and membership checking.

1.1.1 Index (index)

Given a sequence, returns the element at the specified index position. As follows

l = [1,2,3,4,5]   #列表
t =54321#元组
s = "Hello"  #字符串
print(l[0],t[0],s[0])  #索引操作,输出它们的第一个元素
print(l[-1],t[-1],s[-1])  #索引操作,输出它们的倒数第一个元素

output:

1 5 H
5 1 o

1.1.2 Slicing

Given a sequence, slices can be used to access the specified range of elements. and separated by colons . (This part is described in detail in another blog post )

As follows:

l = [1,2,3,4,5]   #列表
t =54321#元组
s = "Hello"  #字符串
print(l[13]) #切片操作,返回列表索引为1到索引为2的所有元素(不包括右端的索引3)
print(t[0:2])  #切片操作,返回元组索引为0到索引为1的所有元素
print(s[2:4])  ##切片操作,返回字符串索引为2到索引为3的所有元素

output:

[2,3]
(5,4)
ll

1.1.3 Addition

Remember, the addition of sequences is not mathematical addition, but concatenation of them , and addition can only add sequences of the same type . As follows

l = [1,2,3,4,5]   #列表
t =54321#元组
s = "Hello"  #字符串
print(l+l) #相加操作,返回列表拼接本身后形成的新列表
print(t+t)  #相加操作,返回元组拼接本身后形成的新元组
print(s+s[2])  ##相加操作,返回字符串拼接指定元素后的新字符串

output:

[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
(5, 4, 3, 2, 1, 5, 4, 3, 2, 1)
Hellol

1.1.4 Multiplication

Similar to adding, it means concatenating. Just repeat the splicing of this sequence many times to form a new sequence. As follows

l = [1,2,3,4,5]  #列表
t = (5,4,3,2,1)  #元组
s = "Hello"  #字符串
print(l*2)  #相乘操作,返回列表重复拼接2次后形成的新列表
print(t*2)  #相乘操作,返回元组重复拼接2次后形成的新元组
print(s*3)  #相乘操作,返回字符串重复拼接3次后形成的新字符串

output:

[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
(5, 4, 3, 2, 1, 5, 4, 3, 2, 1)
HelloHelloHello

1.1.5 Membership

Checking if a particular value or element is in a sequence usually requires membership. Operators are generally used into determine whether a specific condition is met, and return Truewhen it is met, and return when it is not met False. This type of operator is also called a boolean operator and the value returned is called a boolean value . As follows

l = [1,2,3,4,5]  #列表
t = (5,4,3,2,1)  #元组
s = "Hello"  #字符串
print('2' in l)  #资格检查操作,判断字符2是否在列表中,输出布尔值
print( 2 in l)  #资格检查操作,判断数字2是否在列表中,输出布尔值(注意与上面的区别)
print('2' in t)  #资格检查操作,判断字符2是否在元组中,输出布尔值
print('e' in s)  #资格检查操作,判断字符e是否在字符串中,输出布尔值

output:

False
True
False
True

1.2 Understanding of List

Lists are the most frequently used sequences in python, mainly for operations such as modification, deletion, and slicing, as described above. This section highlights some of the methods listed below.

1.2.1 append

means toan objectAppended to the end of the list , the representation format is l.append(). E.g

l = [1,2,3,4,5]
l.append(2)
print(l)

Output: [1,2,3,4,5,2].

1.2.2 clear

Indicates that all contents of the list are cleared , and the format is l.clear(). E.g

l = [1,2,3,4,5]
l.clear()
print(l)

Output: [].

The difference from clear is the pop operation, which means that an element is deleted . E.g

l = [1,2,3,4,5]
l.pop(2)
print(l)

output:[1,2,4,5]

1.2.3 copy

Indicates to copy the contents of the list in the format l.copy(). E.g

l = [1,2,3,4,5]
a = l.copy()
print(a[1])

Output: 2.

1.2.4 count

Represents the number of times the specified element appears in the list, in the format l.count(). E.g

l = ['a','b','b','c','d']
print(l.count('b'))

Output: 2.

1.2.5 index

Represents the index position of the first occurrence of an element in the list, in the format l.index(). E.g

l = ['a','b','b','c','d']
print(l.index('c'))

Output: 3.

1.2.6 insert

Indicates that an object is inserted into the specified index position in the list, in the format of l.insert( *,*). E.g

l = ['a','b','b','c','d']
l.insert(2,'e')
print(l)

Output: ['a','b','e','b','c','d'].

1.2.7 reverse

Indicates that the order in the list is reversed , that is, the reverse order. The format is l.reverse(). E.g

l = [1,2,3,4,5]
l.reverse()
print(l)

Output: [5,4,3,2,1].

1.2.8 sort或sorted

Indicates that the elements in the list are sorted in-place , that is, from small to large, in the format l.sort(). E.g

l = [32145]
l.sort()
print(l)

Output: [1,2,3,4,5]. Note the difference with reverse.

1.3 Understanding of tuples

A tuple is similar to a list, but the representation method and operability are different, that is, the list is used []and the tuple is used to ()represent the method, the list can be modified but the tuple cannot be modified .

Other basic operations are similar to lists, so I won't repeat them here!

1.4 Understanding of strings

Strings are immutable, and the general approach is similar to that of lists. Since there are too many, here is what I know so far.

1.4.1 center

Indicates padding characters on both sides of the string and centering the string . E.g

s = "hello"
print(s.center(10,'*'))  #两边填充'*‘号,且满足最后的总长度为10.

Output: **hello***.

1.4.2 find

Indicates to find the corresponding substring in the string , if found, return the index position , otherwise return-1

s = "hello world!"
print(s.find('o'))
print(s.find('z')

Output: 4and -1.

1.4.3 join

Represents the elements of the merged sequence , and needs to be a list of strings.

l = ['a','b','b','c','d']
x = '+'
print(x.join(l))

output:a+e+b+c+d

1.4.4 split

Contrary to the join method, which means to split a string into a sequence.

l = 'a+e+b+c+d'
x = '+'
print(x.join(l))

output:['a','b','b','c','d']

1.4.5 lower

Represents the lowercase version of the returned string in the format l.lower(). E.g

s = "Hello World!"
print(s.lower())

output:hello world!

Guess you like

Origin blog.csdn.net/A33280000f/article/details/121262242