Chapter 2 Python Data Types
content
1. Boolean
2. Integer
Integers in Python are of type int, the default is decimal, and the representation of binary, octal, and hexadecimal is supported.
>>> bin(10 ) #binary ' 0b1010 ' >>> oct(10 )# octal ' 0o12 ' >>> hex(10 )# hexadecimal ' 0xa ' >>>
3. Floating point
A floating-point number is a numerical representation of a number belonging to a certain subset of rational numbers, and is used to approximate any real number in a computer. Python defaults to 17-bit precision, which means the decimal point is 16-bit thick.
>>> a = 3.151592653513651054608317828332
>>> a
3.151592653513651
>>>
4. String
A string is an ordered collection of characters
>>> s = 'hello' >>> s[0]#索引 'h' >>> s[1] 'e' >>> s[-1] 'o' >>> s.index('e') 1>>> s.find( ' e ' ) #find 1 >>> a = ' hello, world ' >>> a ' hello, world ' >>> a.strip() #remove blank ' hello ,world ' >>> a.lstrip() #remove left blank ' hello,world ' >>> a.rstrip() #remove right blank ' hello,world ' >>> b = ' hello,world ' >>> len(b ) #length 11 >>> b.replace( ' h ' , ' H ' ) #replace ' Hello,world ' >>> s = ' helloworld ' >>> s[0:5] # slice ' hello ' >>> s[ : 5] # Take the first 5 ' hello ' >>> s[5:] # Take all the ' world ' >>> s[:] # Take all the ' helloworld ' >>> >>> s[0:5:2 ] ' hlo ' >>> s[::2] #Take one every other ' hlool ' starting from h >>> s[::-1] #Reverse ' dlrowolleh ' >>>
5. List
list: list , ordered items, search by index, use square brackets "[]";
>>> l = [ ' alex ' , ' cat ' , ' dog ' , ' pig ' ] #Find >>> l[0] ' alex ' >>> l[0 : 2 ] #Slice [ ' alex ' , ' cat ' ] >>> l[::2 ] ['alex', 'dog'] >>> l.append('rabbit')#添加 >>> l ['alex', 'cat', 'dog', 'pig', 'rabbit'] >>> l.remove('rabbit')#删除 >>> l [ ' alex ' , ' cat ' , ' dog ' , ' pig ' ] >>> l.pop() #remove ' pig ' from the last one >>> l [ ' alex ' , ' cat ' , ' dog ' ] >>> len(l ) #length 3 >>> 'cat' in l#包含 True >>> for i in l:#循环 >>> print(1) ['alex', 'cat', 'dog'] >>>
6. Origin
tuple: tuple , tuple collects various objects together, cannot be modified, searched by index, use parentheses "()";
>>> ages = (11,22,33,44,55) >>> ages[0]#查找 11 >>> ages[-1] 55 >>> for i in ages:#循环 ... print(i) ... 11 22 33 44 55 >>> len(ages ) #length 5 >>> 11 in ages#包含 True >>>
7. Mutable, immutable data types and hashing
>>> l = [1,2,3,4] #list >>> id (l) 1466396532232 >>> >>> a=1#数字 >>> id(a) 1661187136 >>> >>> s = ' hello ' #string >>> id(s ) 1466396546584 >>> >>> t = (1,2,3,4)#元组 >>> id(t) 1466396477864 >>> >>> hash("alex") -909141948193867673
8. Dictionary
dict: dictionary, a dictionary is a combination of a set of keys (keys) and values (values), searched by keys (key), no order, using curly brackets "{}";
key, value, key-value pair 1. dic.keys() returns a list containing all the keys of the dictionary 2. dic.values() returns a list containing all the values of the dictionary 3. dic.items() returns a list containing all (key, value) tuples 4. dic.iteritems(), dic.iterkeys(), dic.itervaluses() are the same as their non-iterative counterparts,
except that they return an iterator instead of a list new 1、dic['new_key'] = 'new_value'; 2. dic.setdefault(key, none) If the Key key does not exist in the dictionary, assign it by dic[key] = default; delete 1. dic.pop(key[,default]) is similar to the get method. If there is a key in the dictionary, delete and return the values corresponding to the key; if the key does not exist
and there is no value given to default, a keyerror exception is raised; 2. dic.cler() deletes all items or elements in the dictionary; Revise 1. dic['key'] = 'new_value' If the key exists in the dictionary, 'new_value' will replace the original value; 2. dic.update(dic2) adds the key-value pair of the dictionary dic2 to the dictionary dic Check 1. dic['key'], returns the value corresponding to the key in the dictionary, if the key does not exist in the dictionary, an error will be reported; 2. dic.get(key, default = none) returns the value corresponding to the key in the dictionary. If the key does not exist in the dictionary, it returns the default value (default default is none) cycle 1、for k in dic.keys() 2、for k,v in dic.items() 3、for k in dic length 1 、 len (Dec)
9. Collection
set: set, unordered, elements appear only once, automatically deduplicate, use "set([])";
set.isdisjoint(s): Determine whether two sets intersect
set.issuperset(s): Determine whether the set contains other sets, equivalent to a>=b
set.issubset(s): Determine whether the set is contained by other sets, equivalent to a<=b
add(): Add a single element, similar to append in a list
update(): Adds to the sequence, similar to the extend method, supports adding multiple parameters at the same time
set.discard(s): no exception will be thrown
set.remove(s): will throw a KeyError error
pop: Since the collection is unordered, the result returned by pop cannot be determined. When the collection is empty, a KeyError will be thrown
clear: clear the collection
>>> a = {1,2} >>> a.update([3,4],[1,2,7]) >>> a {1, 2, 3, 4, 7} >>> a.update("hello") >>> a {1, 2, 3, 4, 'h', 7, 'o', 'e', 'l'} >>> a.add("hello") >>> a {1, 2, 3, 4, 'h', 7, 'o', 'e', 'hello', 'l'} >>> a.discard(1) >>> a {2, 3, 4, 'h', 7, 'o', 'e', 'hello', 'l'} >>> a.discard(1) >>> a {2, 3, 4, 'h', 7, 'o', 'e', 'hello', 'l'} >>> a.remove(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 1 >>> a.pop() 2 >>> a {3, 4, 'h', 7, 'o', 'e', 'hello', 'l'} >>> a.clear() >>> a set() >>> a.pop() Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'pop from an empty set' >>>