Python Development -- 14 tuple type built-in method
編輯:Python
List of articles
One . effect
1. Tuples and lists
2. The function of tuples
Two . Define the way
Tuple immutable parsing
3、 ... and . Common operations + Built-in methods
Priority operation (*********)
1. Value by index ( Take... Forward + Take... In reverse ) : Can only take
2. section ( Head and tail + step )
3、 length : .len()
4、 Members of the operation int and not in
5、 loop
Operations that need to be mastered (****)
1. Number of Statistics : .count()
2. Search index : .index()
Four . summary
You can store multiple values
Orderly , Depends on index values
Four . summary
You can store multiple values
Orderly , Depends on index values
Immutable type ---> can hash type
One . effect
1. Tuples and lists
Tuple is actually immutable A list of
The list is readable and modifiable , And tuple read-only cannot be changed
Under the same data type , Tuples are more space efficient , And more efficient than lists
Because the bottom layer of tuple only provides read mechanism , The list has both read and change mechanism
2. The function of tuples
Obvious , It also stores multiple values by location , The index corresponds to the value
Two . Define the way
stay “( )” Use commas to separate multiple elements of any type
If the tuple contains only one element , So it needs to be separated by commas : (111,)
Tuple immutability means first floor Elemental The memory address cannot be changed
If the first layer contains a sublist , Then the memory address of the sublist cannot be changed , But you can change the elements in the sublist ( List variable types )
“ factory ” : tuple
# Definition
l=(11,11.11,"aaa",[222,333]) # Back call l=tuple(...)
# Defining a single value requires a comma ( No, it's just the meaning of inclusion )
x = (18)
y = (18,)
print(type(x)) #<class 'int'>
print(type(y)) #<class 'tuple'>
# A tuple
j=(1,) # Add a comma
print(j,type(j))
(1,) <class 'tuple'>
Tuple immutable parsing
# Immutable type resolution of tuples
tup = (111,"aaa",[222,333])
print(id(tup[0]),id(tup[1]),id(tup[2]))
#140709693587136 2763387504816 2763386278472
tup[0] = 222 # Report errors
tup[1] = 333 # Report errors
tup[2] = 333 # Report errors
# When changing elements in a sublist
tup[2][0] = 333
print(tup[2]) #[333, 333]
# Check after the modification , Of the first element of a tuple "id" There is no change
print(id(tup[0]),id(tup[1]),id(tup[2]))
#140709693587136 2763387504816 2763386278472
3、 ... and . Common operations + Built-in methods
Priority operation (*********)
1. Value by index ( Take... Forward + Take... In reverse ) : Can only take