Chapter 3 python3, python3
- Tuples are similar to lists. The difference is that elements of tuples cannot be modified.
- The tuples use parentheses, And the list uses square brackets.
Creating tuples is simple. You only need to add elements in brackets and separate them with commas:
language = ('c', 'c++', 'python')
You can also omit the brackets:
language = 'c', 'c++', 'python'
Create null tuples:
language = ()
When a tuple contains only one element, you must add a comma (,) next to the element to eliminate ambiguity. Otherwise, parentheses will be used as operators (think, what operators will be used ):
Tup1 = (50) print (type (tup1) # Without commas (,), the type is Integer tup1 = (50,) print (type (tup1) # With commas (,), the type is tuples
The above code outputs:
<class 'int'><class 'tuple'>
Tuples are similar to strings. subscript indexes start from 0 and can be truncated or combined.
1. Access tuples
You can use subscript indexes to access values in tuples:
language = ('c', 'c++', 'python')
print(language[0])print(language[1:3])
The above code outputs:
c('c++', 'python')
2. Modify tuples
Element values in tuples cannot be modified, but we can concatenate and combine them, as shown in the following example:
Language = ('C', 'c ++ ', 'python') animal = ('Dog', 'cat') # The following operation to modify the element of a tuples is invalid. # Language [0] = 'C # 'tup = language + animal # create a new tuple print (tup)
The above code outputs:
('c', 'c++', 'python', 'dog', 'cat')
3. Delete tuples
The element values in the tuples cannot be deleted, but we can use the del statement to delete the entire tuples, as shown in the following example:
Language = ('C', 'c ++ ', 'python') print (language) del languageprint ('deleted tuples', language)
The above code outputs:
('c', 'c++', 'python')NameError: name 'language' is not defined
We found that after del is called to delete the tuples, printing the tuples language will output abnormal information about the variables.
4. tuples
Like a string, you can use the plus sign (+) and minus sign (*) to perform operations between tuples. This means they can combine and copy, and a new tuples will be generated after the operation.
| Python expressions |
Result |
Description |
| Len (1, 2, 3 )) |
3 |
Calculate the number of elements |
| (1, 2, 3) + (4, 5, 6) |
(1, 2, 3, 4, 5, 6) |
Connection |
| ('Hi! ',) * 4 |
('Hi! ', 'Hi! ', 'Hi! ', 'Hi! ') |
Copy |
| 3 in (1, 2, 3) |
True |
Whether the element exists |
| For x in (1, 2, 3): print x, |
1 2 3 |
Iteration |
5. tuples index and truncation
Because tuples are also a sequence, we can access the elements at the specified position in the tuples, or intercept some elements in the index, as shown below:
Language = ('C', 'c ++ ', 'python') print ('read the third element language [2]:', language [2]) print ('read the second-to-last element language [-2]: ', language [-2] from the right) print ('language [1:]: ', language [1:])
The above code outputs:
Read the third element language [2]: python reads the second to last element language from the right [-2]: c ++ outputs the language [1:]: ('C ++ ', 'python') of all elements starting from the second element ')
6. "variable" tuple:
Language = ('C', 'c ++ ', ['C #', 'python']) # The third element of the tuple language is the list print (language) language [2] [0] = 'java' # change the print (language) value of the first element in the list)
The above code outputs:
('c', 'c++', ['c#', 'python'])('c', 'c++', ['java', 'python'])
Doesn't tuple be immutable once defined? Why have they changed? On the surface, the tuple elements have indeed changed, but in fact they are not the tuple elements, but the list elements. The list to which tuple points at the beginning is not changed to another list. Therefore, tuple's so-called "unchanged" means that every element of tuple will always point to it. That is, if you point to 'A', you cannot change it to 'B' or to a list. You cannot change it to another object, but the list itself is variable!
After "pointing to unchanged", how does one create a tuple with unchanged content? Therefore, each element of the tuple cannot be changed.