Python study notes (tuples, lists)

The concept of
tuples Tuples are types that contain multiple elements, separated by commas.
For example: t1 = 123,456, the "hello"
tuple can be empty, and t2 = ()
can use parentheses outside the tuple or not.
Tuples have three characteristics
. The elements in a tuple can be of different types.
For example: t3 = 123,456, ("hello", "China") The
elements in the tuple have a sequential relationship, and the elements in the tuple can be accessed by index.
For example: t3 [0]
tuples cannot be changed or deleted after being defined.
For example: t3 [0] = 456 is
similar to the string type, you can access some elements in the tuple through the index interval.
Like string, + and * can be used for operation between tuples.
The concept of a
list A list is an ordered collection of elements.
List elements can access individual elements by index.
Lists are similar to tuples
. Each element type in the list can be different.
The index form is used when accessing elements in the list.
The list is different from the tuple.
The size of the list is not limited and can be modified at any time.
List operations :

Sequence operator Operator meaning
<seq>+<seq> Connect two sequences
<seq> * <integer type> Repeat the sequence an integer number of times
<seq> [<integer type>] Elements in the index sequence
Len(<seq>) Number of elements in the sequence
<seq> [<integer type>: <integer type>] Take a subsequence of a sequence
For <var> in <seq> : Cycle through the sequence
<expr> in <seq> Member check to determine whether <expr> is in the sequence
method Method meaning
<list>.append(x) Add element x to the end of the list
<list>.sort() Sort list elements
<list>.reverse() Invert list elements
<list>.index() Returns the index value of the first occurrence of element x
<list>.insert(i,x) Insert new element x at position i
<list>.count(x) Returns the number of elements x in the list
<list>.remove(x) Delete the first element x in the list
<list>.pop(i) Take the element at position i in the list and delete it

The string can be split into a list through the split () function.
For example:
>>> “python is an excellent language” .split ()
['python', 'is', 'an', 'excellent', 'language']

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/97274519