The basics of python data analysis-the basic use of python

The basics of python data analysis-the basic use of python

1. data type

  • Integer type: int type, positive and negative integers, no decimal point
  • Floating point type: float type, decimal and integer
  • plural
  • Constant: Can not change the existing value, such as pi
  • Boolean: True False
  • String: text data type
    'a'
  • Data type conversion: can be converted by related functions
# Integer floating point >>> a = 5 >>> type (a) < class ' int '> >>> b = 3.3 >>> type ( b ) < class ' float '> >>> int ( b ) 3 >>> float ( a ) 5.0 >>> The integer part of 6//3 division 2 >>> 7//3 2 >>> The remainder of 4%3 division 1 >>> 12%4 0 # Boolean >>> 3> 5 False >>> ' a ' is ' a ' True >>> ' a ' is a False >>> True or False True >>> not True False # String # Create with single quotes or str () function >>> s ='I am python ' or >>> s = str ( 'I am python' ) >>> s'I am python ' >>> type ( s ) < class ' str '> # Use [] to index the elements # The elements in the string cannot be changed >>> s [1] 'Yes' >>> s [1: 3 ] 'is p' >>> s[- 5 :- 1 ] 'ytho' >>> s * 3 # String multiplication'I am python I am python I am python' >>> v = 'hello' >>> s+v # string addition'I am pythonhello' >>> 'hello,' + 'world' + 'b' * 10 # first calculate the multiplication'hello ,worldbbbbbbbbbb' # inf >>> float ( '-inf' ) # Use float() function to create a - -inf >>> type ( float ( '-inf' )) < class ' float '> # nan ( non-numerical ) >>> float ( '-inf' )+ float ( 'inf' ) nan >>> type ( float ( '-inf' )+ float ( 'inf' ) ) < class ' float '> # None null values are often used to make judgments >>> X = None >>> X >>> a float ( X ) copying the code

2. the data structure

Data structure is the way to store data

  • List list: an ordered collection of different data types
  • Tuple tuple: tuple and list are very similar, but once the tuple is initialized, it cannot be modified
  • Set: disordered, non-repetitive combination
  • Dictionary dict: a container for storing key: value

1. List

# Create a list, using square brackets. The data types of the elements in the list can be different li = [ 1 , 2 , 3 , 'abc' , 4.5 ,[ 2 , 3 , 4 ], { 1 : 'one' }] Integer, string, floating point, list, byte s = [] # empty list >>> li = list ( 'abc' ) >>> li [ 'a' , 'b' , 'c' ] # List information: length >>> len (li) # The length of the list li >>> max (li) # The maximum value of the list li >>> min (li) # The minimum value of the list li # Read and write according to index: List name put the index in parentheses (counting from 0) >>> li[ 0 ] Print the element in the list li whose subscript is 0 , which is the first element. or >>> print (li[ 0 ]) 1 >>> print (li[ 5 ]) The element with subscript 5 in the list li , which is the sixth element [ 2 , 3 , 4 ] >>> print (li[- 1 ])- 1 represents the first element from the bottom, and so on { 1 : 'one' } # Slice (closed left and open right) >>> li[ 2 : 5 ] 2 <=subscript< 5 elements in the list with subscripts 2 , 3 , 4 [ 3 , 'abc' , 4.5 ] >>> li[: 5 ] omit the left side, 0 <= subscript < 5 , that is, the subscript is 0 1 2 3 4 the element [ 1 , 2 , 3 , 'abc' , 4.5 ] >>> li[ 5 :] omit the right side, 5 <= subscript, until the last element [[ 2 , 3 , 4 ], { 1 : 'one' }] >>> li[:] left and right are omitted, which means all elements equal >>> li [ 1 , 2 , 3 , 'abc' , 4.5 , [ 2 , 3 , 4 ], { 1 : 'one' }] >>> li[ 0 : 4 : 2 ] 0 =<subscript< 4 and start from 0 , add 2 each time , the step size is 2 . In summary, the elements with subscripts 0 and 2 [ 1 , 3 ] >>> li[ 3 :: 2 ] 3 =< subscript, up to the last element ( 6 ) and add 2 every time starting from 3 . The overall superscript and subscripts are 3 5 [ 'abc' , [ 2 , 3 , 4 ]] >>> li[- 6 :- 1 : 1 ] Sixth from the bottom=<Element<The first one from the bottom adds 1 each time [ 1 , 2 , 3 , 4 , 5 ] Step size is negative >>> li= [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] >>> li[::- 1 ] If the left side is missing, the default is- 1 and the first element from the bottom is missing. If the right side is missing, the default is to the first Elements. The step size is -1 , which means adding 1 each time from back to front . Countdown 1 2 3 4 5 6 7 elements is to reverse the original list equal >>> li[- 1 ::- 1 ] equal >>> li.reverse() [6, 5, 4, 3, 2, 1, 0] >>> li[::-2] 1 3 5 7 [6, 4, 2, 0] >>> li[-1:-4:-1] 1 2 3 [6, 5, 4] # >>> li=[1,2,3] >>> li.append('a') >>> print(li) [1, 2, 3, 'a'] >>> li.append([4,5,6]) # >>> print(li) [1, 2, 3, 'a', [4, 5, 6]] >>> li.extend([7,8,9]) # >>> print(li) [1, 2, 3, 'a', [4, 5, 6], 7, 8, 9] >>> li = [1,2,3] >>> li.insert(0,'0') # 0 >>> li ['0', 1, 2, 3] # >>> li.pop() # 9 >>> li.pop(0) # 1 1 >>> print(li) [2, 3, 'a', [4, 5, 6], 7, 8] >>> del(li[1]) # li >>> print(li) [3, 'a', [4, 5, 6], 7, 8] >>> li = ['0','0', 1, 2, 3] >>> li.remove('0') # 0 >>> li ['0',1, 2, 3] # >>> print(1 in li) False >>> print(7 in li) True >>> print(4 in li) False # >>> not li li False false >>> print(len(li)) li 0 6 # >>> s = 'abcd' s >>> li=list(s) s li( ) >>> li ['a', 'b', 'c', 'd'] >>> li[3]='T' >>> print(li) ['a', 'b', 'c', 'T'] >>> s=''.join(li) >>> s 'abcT' # >>> for i in li : print(i) 1 2 3 >>> for i in range(len(li)): print(li[i]) 1 2 3 # >>> li=[1,1,2,3,4,4] >>> li.count(1) # 1 2 >>> li.index(2) # 2 2 # >>> li=[1,3,2] >>> li.sort() >>> li [1, 2, 3] # >>> a=[1,2,3] >>> b=list('python') >>> b ['p', 'y', 't', 'h', 'o', 'n'] >>> a+b # [1, 2, 3, 'p', 'y', 't', 'h', 'o', 'n'] >>> a*3 # [1, 2, 3, 1, 2, 3, 1, 2, 3]
# range(i,j,b) i=< <j,b range(5) = range(0,5) 0 0 1 2 3 4 >>> print([i*2 for i in range(10)]) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> print([(x,y) for x in range(3) for y in range(3)]) [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

2

# # >>> tp=(1,2,3) >>> tp (1, 2, 3) >>> tp=tuple([1,2,3]) # >>> tp (1, 2, 3) # >>> del tp # >>> a=(1,2,3) >>> a*3 # (1, 2, 3, 1, 2, 3, 1, 2, 3) >>> b=tuple([4,5,6]) >>> a+b # (1, 2, 3, 4, 5, 6) >>> a1,a2,a3,a4,a5,a6=a+b # >>> a1 1

3

# set() >>> s_a = set([1,1,3,2]) >>> print(s_a) {1, 2, 3} # >>> s_a.add(6) 6 >>> s_a {1, 2, 3, 6} >>> s_a.update([2,4,5,6]) >>> s_a {1, 2, 3, 4, 5, 6} # >>> s_a.remove(6) # >>> s_a {1, 2, 3, 4, 5} # >>> 1 in s_a True >>> 6 in s_a False >>> {1}.issubset(s_a) True >>> {1,7}.issubset(s_a) False # >>> not s_a False # >>> s_a = set([2,3,5,7]) >>> for i in s_a: print(i) 2 3 5 7 # >>> s_a= set([1,2,3,4,5]) >>> s_b = set([4,5,6,7,8]) >>> s_a | s_b or >>> s_a.union(s_b) {1, 2, 3, 4, 5, 6, 7, 8} # >>> s_a & s_b >>> s_a.intersection(s_b) {4, 5} # >>> s_a - s_b # s_a s_b or >>> s_a.difference(s_b) {1, 2, 3} # - >>> s_a ^ s_b or >>> s_a.symmetric_difference(s_b) {1, 2, 3, 6, 7, 8} # s_a s_b >>> s_a < s_b False

4

# d key value >>> d={'a':1,2:'b'} >>> d {'a': 1, 2: 'b'} # >>> len(d) 2 # >>> d['a'] # key 1 >>> d['2'] # '2' d Traceback (most recent call last): File "<pyshell#56>", line 1, in <module> d['2'] KeyError: '2' >>> d.get('2',1) # '2' d 1 d value 1 >>> d[2] 'b' >>> d.keys() # key dict_keys(['a', 2]) >>> d.values() # value dict_values([1, 'b']) >>> d.items() # items key value dict_items([('a', 1), (2, 'b')]) # >>> d['f']=5 >>> d {'a': 1, 2: 'b', 'f': 5} # >>> del(d['f']) >>> d {'a': 1, 2: 'b'} >>> d.pop('f') # # key >>> 'a' in d True >>> d.get('2',1) # '2' d 1( 1 None) d value 1 >>> d.get('2') is None True # >>> not d d False # >>> d.clear() # key : key key >>> for k in d.keys() : print(str(k)+':'+str(d[k])) a:1 2:b item key value >>> for k,v in d.items():print(str(k)+':'+str(v)) a:1 2:b