Python knowledge notes (+ 4): understand the concepts of list, tuple and string

Keywords: Python leetcode list

Recently, I encountered some basic concepts that I didn't understand very well when I was brushing Leetcode questions, so I need to supplement the basic knowledge. Therefore, the following are some basic concepts about List, Tuple and String that I summarized after consulting the materials.

Understanding sequences (lists, tuples, and strings) in python

1.1 basic concepts

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

Sequences mainly include lists, tuples and strings. (the three most common)

  • 1) The basic format is as follows: lists are generally represented by [], tuples are generally represented by (), and strings are generally represented by "". Pay attention to the differences in their representations.
l = [1,2,3,4,5]   #list
t = (5,4,3,2,1)#tuple
s = "Hello"  #character string
  • 2) The difference between list, tuple and string: list can be modified, but tuple and string cannot.
  • 3) Sequences can be indexed, sliced, added, multiplied, and membership checked.

1.1.1 index

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

l = [1,2,3,4,5]   #list
t = (5,4,3,2,1)#tuple
s = "Hello"  #character string
print(l[0],t[0],s[0])  #Index operations, outputting their first element
print(l[-1],t[-1],s[-1])  #Index operations, outputting their penultimate element

Output:

1 5 H
5 1 o

1.1.2 slicing

Given a sequence, slices can be used to access a specified range of elements. And separated by colon:. (this part is in Another blog post (detailed introduction)

As follows:

l = [1,2,3,4,5]   #list
t = (5,4,3,2,1)#tuple
s = "Hello"  #character string
print(l[1: 3]) #The slicing operation returns all elements with index 1 to index 2 in the list (excluding index 3 at the right end)
print(t[0:2])  #Slicing operation, which returns all elements with tuple index 0 to index 1
print(s[2:4])  ##Slicing operation, which returns all elements with string index from 2 to 3

Output:

[2,3]
(5,4)
ll

1.1.3 addition

Remember, the addition of sequences is not addition in the mathematical sense, but splicing them, and addition can only add sequences of the same type. As shown below

l = [1,2,3,4,5]   #list
t = (5,4,3,2,1)#tuple
s = "Hello"  #character string
print(l+l) #The addition operation returns the new list formed after the list splicing itself
print(t+t)  #The addition operation returns the new element group formed after the tuple splicing itself
print(s+s[2])  ##The addition operation returns a new string after splicing the specified elements

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 addition, it means splicing. Just repeatedly splice this sequence to form a new sequence. As shown below

l = [1,2,3,4,5]  #list
t = (5,4,3,2,1)  #tuple
s = "Hello"  #character string
print(l*2)  #Multiply to return the new list formed after the list is spliced twice
print(t*2)  #The multiplication operation returns the new element group formed after the tuples are spliced twice
print(s*3)  #Multiplication operation returns a new string formed after repeated splicing of strings for 3 times

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 whether a particular value or element is in a sequence usually requires membership. Generally, the operator in is used to judge whether a specific condition is satisfied. When it is satisfied, it returns True, and when it is not satisfied, it returns False. This operator is also called Boolean operator, and the returned value is called Boolean value. As shown below

l = [1,2,3,4,5]  #list
t = (5,4,3,2,1)  #tuple
s = "Hello"  #character string
print('2' in l)  #Qualification check operation, judge whether character 2 is in the list, and output Boolean value
print( 2 in l)  #Qualification check operation, judge whether the number 2 is in the list, and output the Boolean value (note the difference from the above)
print('2' in t)  #Qualification check operation, judge whether character 2 is in tuple, and output Boolean value
print('e' in s)  #The qualification check operation determines whether the character e is in the string and outputs a Boolean value

Output:

False
True
False
True

1.2 understanding of List

List is the most frequently used sequence in python. It can be used to modify, delete, slice and other operations, as described above. This section focuses on some methods of the list.

1.2.1 append

Indicates that an object is attached to the end of the list. The format is l.append(). for example

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(). for example

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

Output: [].

Unlike clear, there is also a pop operation, which means deleting an element. for example

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

Output: [1,2,4,5]

1.2.3 copy

Indicates that the contents of the copy list are in the format of l.copy(). for example

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

Output: 2.

1.2.4 count

Indicates the number of times the specified element appears in the list. The format is l.count(). for example

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

Output: 2.

1.2.5 index

Indicates the index position of the first occurrence of an element in the list. The format is l.index(). for example

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

Output: 3.

1.2.6 insert

Indicates that an object is inserted into the index position specified in the list. The format is l.insert(*, *). for example

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(). for example

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

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

1.2.8 sort or sorted

Indicates that the elements in the list are sorted in place, i.e. from small to large. The format is l.sort(). for example

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

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

1.3 understanding of tuple s

Tuples are similar to lists, except for different representation methods and operability, that is, lists are represented by [] and tuples by (), lists can be modified but tuples cannot be modified.

Other basic operations are similar to the list, and will not be repeated here!

1.4 understanding of string

Strings are immutable, and their approximate method is similar to that of lists. Because there are too many, here is a record of what I know so far.

1.4.1 center

Indicates that characters are filled on both sides of the string and the string is centered. for example

s = "hello"
print(s.center(10,'*'))  #Both sides are filled with '*' signs, and the final total length is 10

Output: * * hello * * *

1.4.2 find

Indicates to find the corresponding substring in the string. If the index position is found, otherwise - 1 is returned

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

Output: 4 and - 1.

1.4.3 join

Represents the elements of the merge sequence and needs to be a string list.

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

Output: a+e+b+c+d

1.4.4 split

In contrast to the join method, it means splitting a string into sequences.

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 of l.lower(). for example

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

Output: hello world!

Posted by dhydrated on Wed, 10 Nov 2021 21:36:05 -0800