1. Sequence (sequence):
Description: In the previous string list in fact we have used the sequence, the reason put in this is mainly for the connecting link, easy to understand and memory.
python's data access model: Direct Access , Sequence , mapping
- The non-container classes can be accessed directly, and all numeric types are classified as such.
- The sequence type refers to the elements within the container that are accessed from a 0-based index and can access one or more elements at a time. string, list, tuple to sub-class
- The mapping type differs from the sequence type in that it uses a different index and sequential number offset, and its elements are stored out of order and accessed through a unique key . A dictionary is this type.
2.Python tuples
a Python tuple is similar to a list, except that the elements of a tuple cannot be modified.
2.1 Creating tuples
Tuples use parentheses, and the list uses square brackets.
Tuple creation is simple, just add elements in parentheses and separate them with commas.
Example 1:
#!/usr/bin/env python#-*-coding:utf-8-*-#@Time: 2016/9/11 20:54#@Author: WwyxTup1 = ("python","Java","C","C + +") tup2= ("Demacia","Knorr-Bremse","What about IO?") Tup3= ("withdrawals, Jackie .","Childcare Cable","Child Robbery","Baby Fish","Primary Monk")Print "Tup1 as follows:" forVar1inchTup1:Printvar1Print "tup2 as follows:" forVar2inchtup2:Printvar2Print "Tup3 as follows:" forVar3inchTup3:PrintVar3
Example 1 run Result:
Tup1 is as follows: PYTHONJAVACC+ +tup2 is as follows: Demacianox an Sao ah Tup3 is as follows: Withdrawal Jackie child robbing baby fish primary monk
2.2 Creating an empty tuple
When you include only one element in a tuple, you need to add a comma after the element
tuples are similar to strings, and subscript indexes start at 0 and can be intercepted, combined, and so on.
Example 2
# !/usr/bin/env python # -*-coding:utf-8-*- # @Time : 2016/9/11 20:54# @Author : wwyxtup1 = () # Create an empty tuple tup2 = (" io "# Create a tuple print tup1 print tup2[0]
Example 2 run Result:
() io?
Note: element traversal in tuples is traversed based on the traversal of the sequence, and if printed directly as in the example above tup2 returns the representation of tup2 in memory
2.3 Accessing tuples
- Access to tuples can be accessed through a for iteration, such as Example 1
- Access to tuples can also be traversed by the index of the sequence, as in Example 2
2.4 Modifying tuples
Note: element values in tuples are not allowed to be modified, but we can concatenate combinations of tuples.
Example 3:
#!/usr/bin/env python#-*-coding:utf-8-*-#@Time: 2016/9/11 20:54#@Author: WwyxTup1 = ("python","Java","C","C + +") tup2= ("Demacia","Knorr-Bremse","What about IO?") Tup3= ("withdrawals, Jackie .","Childcare Cable","Child Robbery","Baby Fish","Primary Monk") Tup4= Tup1 + tup2 +Tup3Print "Tup4 as follows:" forVar1inchTUP4:PrintVar1
Example 3 run Results
Tup4 is as follows: PYTHONJAVACC+ + Demacianox An Sao what about the withdrawal Jackie child robbing baby fish primary monk
2.5 Deleting tuples
Note: element values in tuples are not allowed to be deleted, but we can use the Del statement to delete the entire tuple
Example 4
Tup2 = ("Demacia","Knorr-Bremse","What about IO?") Tup3= ("withdrawals, Jackie .","Childcare Cable","Child Robbery","Baby Fish","Primary Monk")Print "tup2 as follows:" forVar1inchtup2:Printvar1deltup2Print "the tup2 after deletion are as follows:"PrintTup2[2]delTUP3[2]#Delete an element with an index of TUP3 of 2
Example 4 Run Results
tup2 as follows: Traceback (most recent): Demacia " e:/python/hello/untitled3/tuple.py " in <module> Knox print tup2[2'tup2' The tup2 following the deletion of is not defined:
3. Tuple operators
As with strings, you can use the + and * numbers to perform operations between tuples. This means that they can be combined and copied, and a new tuple is generated after the operation.
Python Expressions |
Results |
Description |
Len ((1, 2, 3)) |
3 |
Count 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 |
4. tuple index, intercept
Because tuples are also a sequence, we can access the elements at the specified location in the tuple, or we can intercept an element in the index as follows:
Meta-group:
L = ('spam'spam'spam! ')
Python Expressions |
Results |
Description |
L[2] |
' spam! ' |
Reading a third element |
L[-2] |
' Spam ' |
Read the second last element of the inverse |
L[1:] |
(' Spam ', ' spam! ') |
Intercepting elements |
5. No closing separator
Arbitrarily unsigned objects, separated by commas, are tuples by default.
Example 5
Print " withdrawals, Jackie . " " Childcare Cable " " Child Robbery " " Baby Fish " " Primary Monk " Print " Captain Timothy . ", 1, 2, 3, 4
Example 5 Run Results
1 2 3 4
Note: Remember this one, try not to use it yourself, in order to be able to read people's code smoothly
6. Meta-set built-in functions
Python tuples commonly used built-in functions:
Serial Number |
Method and Description |
1 |
CMP (Tuple1, Tuple2) Compares two tuples of elements. |
2 |
Len (tuple) Counts the number of tuple elements. |
3 |
Max (tuple) Returns the element's maximum value in a tuple. |
4 |
MIN (tuple) Returns the element minimum value in a tuple. |
5 |
Tuple (SEQ) Converts a list to a tuple. |
Example 6
#!/usr/bin/env python#-*-coding:utf-8-*-#@Time: 2016/9/11 20:54#@Author: Wwyx#tup1 = ("Python", "Java", "C", "C + +")Tup2 = ("Demacia","Knorr-Bremse","What about IO?") Tup3= ("withdrawals, Jackie .","Childcare Cable","Child Robbery","Baby Fish","Primary Monk") var1=CMP (tup2, TUP3)Print "tup2 and Tup3 comparison results:", Var1Print "maximum value of tup2:", Max (tup2)Print "minimum value of TUP3:", Min (TUP3)
Example 6 run Result:
Tup2 and TUP3 comparison results: 1tup2 Maximum: Noxus tup3 minimum: child robbery
Well, the tuple is here, the next dictionary, refueling!!
The most powerful Python King (7)--tuple (tuple)