First, the basic data type--tuple |
Tuple: is a data type that is very similar to a list. However, once created it can no longer be modified, so it is called a read-only list.
Definition: Similar to a list, except [] Change to ()
Characteristics:
- Can hold multiple values (can hold complex data types)
- Immutable ( explicitly telling someone that data is not modifiable, often used for database connection configuration information, etc. )
- The tuple elements are defined in order from left to right, and the subscript is accessed sequentially from 0, ordered
- The tuple itself is immutable, and these mutable elements can be changed if the tuple also contains other mutable elements. (such as lists, etc.)
Create
Ages = (one, 22, 33, 44, 55)# or ages = tuple ((11)
Index & Slice
#Index>>> ages = (11, 22, 33, 44, 55)>>>Ages[0]11>>> ages[3]44>>> ages[-1]55#slices>>> name = ('Egon','Alex','Seven','Yuan')>>> Name[0:2]('Egon','Alex')>>> Name[2:5]('Seven','Yuan')>>> Name[:2]('Egon','Alex')>>> name[2:] ('Seven','Yuan')>>>name[:] ('Egon','Alex','Seven','Yuan')
Inclusions, lengths, and loops
>>> ages = (one, one, one, one, one) in ages # contains True in Agesfalse not inch agesfalse>>> Len (ages) # length for in ages: # Loop ... Print (age) ... 1122334455
II. basic Data Type--dictionary |
A dictionary is the only type of mapping in the Python language.
Definition: {key1:value1,key2:value2}
- The keys and values are separated by a colon ":";
- The item is separated from the item with a comma ",";
Characteristics:
- Key-value structure
- Key must be hash, must be immutable data type, must be unique
- can store any number of values, can be modified, can not be unique
- Disordered
Dictionary common Operations |
Dictionary creation
person = {"name":"Alex",' Age': 20}#orperson = dict (name='Seven', age=20)#orPerson = Dict ({"name":"Egon",' Age': 20})#or Person= Dict ((['name','Yuanhao'],['Wen Zhou', 18]) {}.fromkeys (seq,100)#do not specify 100 defaults to none#Note>>> Dic={}.fromkeys (['K1','K2'],[])>>>dic{'K1': [],'K2': []}>>> dic['K1'].append (1)>>>dic{'K1': [1],'K2': [1]}
Dictionary View
#key, value, key-value pairsDic.keys ()#returns a list containing all keys for the dictionary;Dic.values ()#returns a list containing all the value of the dictionary;Dic.items ()#returns a list containing all (key, value) Ganso;#Dic.iteritems (), Dic.iterkeys (), and dic.itervalues () are the same as the non-iterative methods they correspond to, but they return an iteration instead of a list;#Viewdic['Key']#returns the value corresponding to the key in the dictionary, or an error if the key does not exist in the dictionary;Dict.get (key, default = None)#returns the value of the key in the dictionary, or the value of default if the key does not exist in the dictionary (default defaults to None)#Loops forKinchDic.keys () forKvinchDic.items () forKinchDiC#lengthLen (DIC)
Dictionary additions and Deletions change
#Newdic['New_key'] ='New_value'Dic.setdefault (Key, None)#If the key key does not exist in the dictionary, it is assigned by dic[key] = default; _#DeleteDic.pop (Key[,default])#similar to the Get method. If there is a key in the dictionary, delete and return the key corresponding to the Vuale; if the key does not exist and the default value is not given, the keyerror exception is thrown;Dic.clear ()#Delete all items or elements in the dictionary;#Modifydic['Key'] ='New_value' #if key exists in the dictionary, ' New_value ' will replace the original valueDic.update (DIC2)#Add dictionary dic2 key-value pairs to dictionary dic
III. basic data Types--collections |
A collection is a mathematical concept: a whole is called a set, consisting of one or more determined elements.
The elements in the collection have three characteristics:
- Deterministic (element must be hash)
- Cross-specific (go-to-weight)--Change a list to a set and automatically go heavy
- Unordered (the elements in the collection have no successive points), such as the collection {3,4,5} and {3,5,4} count as the same collection.
Note: The meaning of a set exists in the de-emphasis and relational operations
Relational operations: The intersection of two sets of data, the difference set, and the relationship between the set.
collection creation and relational operations
s = {1,2,3,2,4,5}#Create a collections = set ([1,3,4,5,6])#Convert to CollectionIphone7= {'Alex','Rain','Jack','Old_driver'}iphone8= {'Alex','Shanshan','Jack','Old_boy'}#intersectioniphone7.intersection (Iphone8)#intersectionIphone7 &Iphone8#Difference SetIphone7.difference (Iphone8)#Iphone7, there's Iphone8.#Difference SetIphone7-Iphone8#and set (de-weight)iphone7.union (Iphone8)#and setIphone8 | Iphone7#Pipe character#symmetric difference set (only for people who bought IPhone7 or Iphone8)iphone8.symmetric_difference (Iphone7)#Symmetric difference SetsIphone7 ^ Iphone8
Include relationship
- In,not in: Determines whether an element is within the set
- ==,! =: Determine whether two sets are equal
There are generally three relationships between two collections, Intersect, include, and do not intersect. In Python, the following methods are used to determine:
- Set.isdisjoint (s): Determine if two sets are disjoint
- Set.issuperset (s): Determines whether the collection contains other collections, equivalent to A>=b
- Set.issubset (s): Determines whether a collection is contained by another collection, equivalent to A<=b
Collection of the increase, deletion, search
" "The addition of a single element: Add (), the effect of add is similar to the list of append to increase the sequence: update (), and update similar to the Extend method, the Update method can support the simultaneous passing of multiple parameters:" ">>> a={1,2}>>> A.update ([3,4],[1,2,7])>>>a{1, 2, 3, 4, 7}>>> A.update ("Hello")>>>a{1, 2, 3, 4, 7,'h','e','L','o'}>>> A.add ("Hello")>>>a{1, 2, 3, 4,'Hello', 7,'h','e','L','o'}" "Deleting A collection of elements there are two ways to delete a single element: When the element is not in the original collection: Set.discard (x) does not throw an exception Set.remove (x) throws Keyerror error >>> a={1,2,3,4}" ">>> a={1,2,3,4}>>> A.discard (1)>>>a{2, 3, 4}>>> A.discard (1)>>>a{2, 3, 4}>>> A.remove (1) Traceback (most recent): File"<input>", Line 1,inch<module>Keyerror:1" "pop (): Because the collection is unordered, the result of the pop return cannot be determined, and calling pop when the collection is empty throws a Keyerror error, clear (): Empties the collection" ">>> A={3,"a", 2.1,1}>>>A.pop ()>>>A.pop ()>>>a.clear ()>>>aset ()>>>A.pop () Traceback (most recent): File"<input>", Line 1,inch<module>Keyerror:'pop from an empty set'
Python learning 04-Data types (tuples, dictionaries, collections)