Python data structures and algorithms-Data Types
We will review the built-in atomic data types from the atomic data types. python has two main built-in data classes: int and float. standard Arithmetic Operations: +,-, *,/, and ** (Power Operation). You can use parentheses to change the operation Priority. other very useful operators are the remainder operation %, and the integer division operation //. note: When two integers are separated, the result is a floating point number. the integer division operation returns the integer part after the fractional part is truncated. >>> print 2 + 3*414> print (2 + 3) * 420 >>>> print 2 ** 101024 >>> print 6/32 >>>> print 7 // 32 >>> print 7/32 31 >>> print 7%>> print 3 // 60 >>> print 3% 63 >>> print 2 ** 1001267213600228229401496703205376boolean type, by Python B The ool class provides implementation, which is very useful when expressing the true value. for a boolean object, there are only two values: True and False. The standard boolean type operations are: and, or, and not. >>> trueTrue >>> FalseFalse >>>> False or TrueTrue >>>not (False or True) False >>>> True and TrueTrueBoolean data objects are also used to represent the comparison operator results, for example: equal (=) and greater than (> ). in addition, Relational operators and logical operators can be combined to solve complicated logic problems. table 1 shows Logical Link Operations. Examples are also shown later. table 1: operator description of logical and relational operation operator <less than operator> greater than operator <= less than or equal to operator> = greater than or equal to operator = equal to operator not equal! = When the operator logic and are both True at the same time, the True logic is True, or either of the two is True, and the result is True. The logic is not negative, and False is True, example code of True to False: >>> print (5 = 10) False >>> print (10> 5) True >>> print (5> = 1) and (5 <= 10) True identifiers are used in programming languages in the form of names. in Python, an identifier starts with a letter or an underscore. It is case sensitive and can be of any length. remember to use variables to express your meaning, making your code easier to read and understand. A Python variable is assigned the left value once it is created. the value assignment statement provides a method for associating variables and values. the variable maintains a reference pointing to the data area, not the data itself. see the following code: >>> theSum = 0 >>>> theSum0 >>>> theSum + = 1 >>> theSum1 >>>> theSum = True >>> theSumTrue built-in set data type as a supplement to the Data Type and Boolean Type, python also has some built-in Collection types. lists, strings, and tuples (tuples) are ordered sets. They are similar to common structures but have some special differences. Therefore, you must understand them to use them correctly. sets and dictionaries are unordered Sets. list is an empty or multiple references pointing to the Python data object type. lists is usually written as some values in square brackets separated by commas. the empty table is []. the elements in Lists can be different data objects. The following example shows different data types in a list. >>> [6.5, True, 6.5] [1, 3, True, 6.5] >>> myList = [, True,] >>> myList [1, 3, True, 6.5] When a list value is assigned, the list is returned. however, to operate on the list, you need to assign it to an object. lists is considered as a continuous sequence. They support some common operations that can be used for other sequences. table 2 demonstrates these operations and provides examples to further describe their applications. table 2: general Operation operator of sequence in Python interpretation index [] accessing Element Join in sequence + merging sequences repetition * consecutive repetition Number member in judge whether element length in quence len calculates sequence length shard [:] for sequence shards, we noticed that the index of lists starts from 0. take the slice operation, myList [], returns the list data items, starting from the end of 1 but not including 3. sometimes you want to initialize a list. repeat can be used to quickly complete the process. for example, >>> myList = [0] * 6 >>> myList [0, 0, 0, 0, 0] is easy to understand through the following example: >>> myList = [1, 2, 3, 4] >>> A = [myList] * 3 >>> print (A) [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4] >>> myList [2] = 45 >>> print (A) [[1, 2, 45, 4], [1, 2, 45, 4], [1, 2, 45, 4] Lists provides a large number of methods for building data structures. table 3 provides an overview. the following are some examples. table 3: append alist. append (item) adds an insert alist at the end of the list. insert (I, item) inserts an item pop alist at position I of the list. pop () is removed and the last element pop alist in the list is returned. pop (I) removes and returns the element sort alist at position I in the list. modify listreverse alist. reverse () operation del alist [I] in reverse order to delete the index alist element at position I. index (item) returns the index count alist. count (item) returns the number of times that the item appears remove alist. remove (item) deletes the example code of the method in the first appeared item list: >>> myList = [1024, 3, True, 6.5] >>> myList. append (False) >>> print (myList) [1024, 3, True, 6.5, False] >>> myList. insert (2, 4.5) >>> print (myList) [1024, 3, 4.5, True, 6.5, False] >>> print (myList. pop () False >>> print (myList) [1024, 3, 4.5, True, 6.5] >>> print (myList. pop (1) 3 >>> print (myList) [1024, 4.5, True, 6.5] >>> myList. pop (2) True >>> print (myList) [1024, 4.5, 6.5] >>> myList. sort () >>> print (myList) [4.5, 6.5, 1024] >>> myList. reverse () >>> print (myList) [1024, 6.5, 4.5] >>> print (myList. count (6.5) 1 >>> print (myList. index (4.5) 2 >>> myList. remove (6.5) >>> print (myList) [1024, 4.5] >>> del myList [0] >>> print (myList) [4.5] Even an object like an integer can be called as follows: >>> (54 ). _ add _ (21) 75 in the above Code, we make the integer object 54 execute the add method (known as _ add _) and pass 21 as the number of add. the result is their sum, 75. of course, we usually write 54 + 21. this method will be detailed later. A common function that Python uses to connect to lists: range function. range generates objects in a range. by using the list function, you can see the value in the specified range in the list, as shown in the following code:> range (10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range (0, 10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list (range (10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range (5, 10) [5, 6, 7, 8, 9] >>> list (range (5, 10) [5, 6, 7, 8, 9] >>> list (range (5, 10, 2) [5, 7, 9] >>> list (range (10, 1,-1) [10, 9, A continuous set of 8, 7, 6, 5, 4, 3, 2] Strings that contains 0 or more numbers or other characters. these are called letters, numbers, and other symbolic characters. >>> "David" 'David '>>> myName = "David" >>> myName [3]' I '>>> myName * 2 'David' >>>> len (myName) 5 since strings is a sequence, all operations of the sequence mentioned above can be used. however, strings has its own methods, as shown in table 4. for example, >>> myName 'David '>>> myName. upper () 'David '> myName. center (10) 'David '> myName. find ('V') 2 >>> myName. split ('V') ['da', 'id'] Table 4: method name provided by Strings in Python application description center astring. center (w) returns a center string of w width, count astring. count (item) returns the number of items contained in the string ljust astring. ljust (w) returns a left-aligned string lower astring with w as the width. lower () returns the string in lower case. rjust (w) returns a right-aligned string of w to find astring. find (item) returns the index split astring of the first item. split (schar) divides string into substrings using schar as the separator. The main difference between lists and strings is that lists can be modified but strings cannot be modified. for example, you can change the value of an item through index and value assignment. the string cannot be changed. >>> myList = [6.5, True, 1024] >>> myList [0] = 2 ** 10 >>> myList [, 3, True, 6.5] >>> myName = 'David' >>> myName [0] = 'X' Traceback (most recent call last): File "<pyshell #81> ", line 1, in <module> myName [0] = 'X' TypeError: 'str' object does not support item assignmentTuples (tuples) is very similar to lists, because their elements can all be of different types. the difference is that tuple cannot be changed, just like string. tuples is a set of comma-separated values enclosed by parentheses. as a sequence, you can use all the methods described above. example: >>> myTuple = (2, True, 4.32) >>> myTuple (2, True, 4.32) >>> len (myTuple) 3 >>> myTuple [0] 2 >>> myTuple * 3 (2, True, 4.32, 2, True, 4.32, 2, True, 4.32) >>> myTuple [0: 2] (2, True) However, if you try to modify the elements in the tuples, an error occurs, as shown below. >>> myTuple [1] = falseTraceback (most recent call last): File "<pyshell #92>", line 1, in <module> myTuple [1] = falseNameError: name 'false' is not definedset is an unordered set of zero or multiple unchangeable Python objects. null set indicates set (). sets have various element types. >>> {4.3, "cat", 4.3, False} set ([False, 3, 6, 'cat']) >>> mySet = {, "cat ", 4.3, False }>>> mySetset ([False, 3, 4.3, 6, 'cat']) Table 5: in Python, the Set operation name indicates that the member in judges the member length. len returns the number of elements in the set | aset | otherset returns a new set, return a new set as the Union & aset & otherset, and return a new set as the intersection-aset-otherset, judge whether the first set is a subset of the second set as a difference set <= aset <= otherset> {4.3, "cat", False} set ([False, 3, 4.3, 6, 'cat']) >>> mySet = {3, 6, "cat", 4.3, False }>>> mySetset ([False, 3, 4.3, 6, 'cat']) >>>>> mySet = {4.3, "cat", False >>> len (mySet) 5 >>> False in mySetTrue >>> "dog" in mySetFalse >>> yourSet = {3, 1, "cat", 4.7, false }>>> mySet | yourSetset ([False, 1, 3, 6, 4.3, 'cat', 4.7]) >>> mySet & yourSetset ([False, 3, 'cat']) >>> mySet-yourSetset ([4.3, 6]) >>> mySet <= yourSetFalseSets provides methods similar to mathematical sets. table 6 provides a summary. example: Table 6: union aset. union (otherset) returns a new set. The element consists of the union of two sets to form an intersection aset. intersection (otherset) returns a new set. The element consists of the intersection of two sets to form difference aset. difference (otherset) returns a new set. The element is composed of the difference of two sets and issubset aset. issubset (otherset) checks whether all elements in the first set are added to the second set. add (item) add the element remove aset to the set. remove (item) removes the element pop aset from the set. pop () deletes any element clear aset from the set. clear () delete all elements in the set >>> mySet = {False, 4.5, 3, 6, 'cat' }>>> yourSet = {99, 3,100 >>>> mySet. union (yourSet) set ([4.5, False, 3,100, 6, 'cat', 99]) >>> mySet | yourSetset ([4.5, False, 3,100, 6, 'cat', 99]) >>> mySet. intersection (yourSet) set ([3]) >>> mySet & yourSetset ([3]) >>> mySet. difference (yourSet) set ([4.5, False, 6, 'cat']) >>> mySet-yourSetset ([4.5, False, 6, 'cat']) >>> {3,100 }. issubset (yourSet) True >>{ 3,100 }<= yourSetTrue >>> mySet. add ("house") >>> mySetset ([4.5, False, 3, 6, 'house', 'cat']) >>> mySet. remove (4.5) >>> mySetset ([False, 3, 6, 'house', 'cat']) >>> mySet. pop () False >>> mySetset ([3, 6, 'house', 'cat']) >>> mySet. clear () >>> mySetset ([]) Dictionaries (dictionary) has both methods and operations. tables 7 and 8 describe them. table 7: application of the Operation operators provided by Dictionaries in Python [] myDict [k] The Return key is k, otherwise, an error occurs in key in adict. If the key is in the dictionary, True is returned. Otherwise, False del adict [key] is returned to delete all dictionary elements. >>> phoneext = {'David ': 1410, 'bra': 1137 }>>> phoneext {'bra': 1137, 'David ': 1410 }>>> phoneext. keys () ['bra', 'David ']> phoneext. values () [1137,141 0] >>> phoneext. items () [('Brad ', 1137), ('David', 1410)] >>> phoneext. get ("kent") Table 8: Application Description of method names provided by Dictionaries in Python keys adict. keys () returns keyvalues adict in dictionary. values () returns the value of items adict in dictionary. items () returns all key-value pairs in the dictionary get adict. get (k) returns the value corresponding to k. Otherwise, Noneget adict is returned. get (k, alt) returns the value corresponding to k; otherwise, alt is returned.