Article From:https://www.cnblogs.com/houxingbin/p/9064196.html

# data type

## integer

```>>> bin(10)    #Decimal conversion binary
'0b1010'
>>> oct(10)    #Decimal conversion octal
'0o12'
>>> hex(10)    #Decimal to sixteen hexadecimal
'0xa'```

### Congruent operation (%)

```>>> 5%2
1
>>> 16%3
1
>>> 15%5
0```

### Arithmetic operations (+ * / / / divmod * * *)

```>>> 2+3        #plus
5
>>> 5-4        #reduce
1
>>> 3/2        #except
1.5
>>> 5//2        #To be divisible by
2
>>> divmod(5,1)    #Quotient - remainder
(5, 0)
>>> 2**4        #Sub square
16```

## Character string

### Common operation

```>>> s = "hello"
>>> s[1]
'e'
>>> s[-1]
'o'
>>> s.index('o')
4
>>> s.index('l')
2

# lookup
>>> s.find('e')
1
>>> s.find('z')
-1

# Remove the blank
>>> s = ' hello,world! '
>>> len(s)
14
# Remove the blank
>>> s.strip()
'hello,world!'
# Remove the left blank
>>> s.lstrip()
'hello,world! '
# Remove the right blank
>>> s.rstrip()
' hello,world!'
# Remove the specified character
>>> s2 = '***hello,world!***'
>>> s2.strip('*')
'hello,world!'

# Statistical length
>>> s = 'hello,world!'
>>> len(s)
12

# replace
>>> s = 'hello,world'
>>> s.replace('h','H')
'Hello,world'
>>>
>>> s2 = 'hi,how are you?'
>>> s2.replace('h','H')
'Hi,How are you?'

# Section
>>> s = 'asdflasjf'
>>> len(s)
9
>>> s[0:3]        # Take the first 3
'asd'
>>> s[3:9]        # Take 3-9
'flasjf'
>>> s[:7]        # Take s[0]~s[6]
'asdflas'
>>> s[7:]        # Take s[7]~ to end
'jf'
>>> s[:]        # Take all
'asdflasjf'
>>> s[0:7:2]    # Take 1 s[0]~s[6] middle septum
>>> s[3:9:3]    # Take 2 s[3]~s[8] middle septum
'fs'
>>> s[::2]        # Take all 2 intervals
>>> s[::-1]    # Reverse order take all
'fjsalfdsa'```

Be careful：

1.The single and double quotes of a string cannot cancel the meaning of a special character. If you want to cancel the special meaning of all the characters in the quotes, add r in front of the quotation, such as name = R ‘|\thf.

2.unicodeThe string must be used with R in front of R, such as name = ur ‘|\thf’.

## list

### The definition and creation of a list

Definition：[]It is separated by commas, and stores various data types according to index. Each location represents an element.

Establish

```>>> list_test = ["Zhang San", "Li Si", "houxingbin"]
>>> list_test
['Zhang San', 'Li Si', 'houxingbin']
# perhaps
>>> list_test = list('houxingbin')
>>> list_test
['h', 'o', 'u', 'x', 'i', 'n', 'g', 'b', 'i', 'n']
# perhaps
>>> list_test = list(["Zhang San", "Li Si", "houxingbin"])
>>> list_test
['Zhang San', 'Li Si', 'houxingbin']```

### The features and common operations of the list

Characteristic：

1.Multiple values can be stored

2.The list elements are defined in the order from left to right, and the subscript is sequentially accessed from 0.

Common operation

```>>> l = ['one', 'two', 'three', 'four']
>>> l[0]
'one'
>>> l[2]
'three'
>>> l[0:2]
['one', 'two']
>>> l[2:5]
['three', 'four']
>>> l[:2]
['one', 'two']
>>> l[2:]
['three', 'four']
>>> l[:]
['one', 'two', 'three', 'four']
>>> l[::2]
['one', 'three']
>>> l[::-1]
['four', 'three', 'two', 'one']

# Append
>>> l.append('five')
>>> l
['one', 'two', 'three', 'four', 'five']

# delete
>>> l.append('five')
>>> l
['one', 'two', 'three', 'four', 'five']
>>> l.pop()
'four'
>>> l
['one', 'two', 'three']

# length
>>> len(l)
3

# Contain
>>> 'one' in l
True
>>> 'seven' in l
False

# loop
>>> for i in l:
...     print(i)
...
one
two
three```

## tuple

### The definition and characteristics of the tuple

Definition: similar to a list, only [] is changed to ().

Characteristic：

1.Many values can be stored.

2.Immutable;

3.By defining the tuple elements from left to right, the subscript is sequentially accessed from 0.

### The creation and common operation of a tuple

Establish

```>>> age = (11,22,33,44,55)
>>> age
(11, 22, 33, 44, 55)
# or
>>> age = tuple((11,22,33,44,55))
>>> age
(11, 22, 33, 44, 55)```

Common operation

```# Indexes
>>> ages = (11 ,22 ,33, 44 ,55)
>>> ages[0]
11
>>> ages[3]
44
>>> ages[-1]
55

# Slice: the same list

# loop
>>> for age in ages:
...     print(age)
...
11
22
33
44
55

# length
>>> len(ages)
5

# Contain
>>> 11 in ages
True
>>> 77 in ages
False
>>> 11 not in ages
False```

### A detailed explanation of the characteristics of a tuple

1.Multiple values can be stored

If there is only one value in the tuple

```>>> t = (1, )
>>> t
(1,)
>>> t = (1)     Equivalent to t = 1> > > t
1```

Tuples can not only store numbers, strings, but also store more complex data types.

2.Immutable

Tuples themselves are immutable. If tuples contain other variable elements, these variable elements can be changed.

## Dictionaries

### The definition and characteristics of dictionaries

Dictionaries are the only mapping types in the python language.

Definition: {key1:value1, key2:value2}

1.Keys and values are separated by a colon.

2.Items and items are separated by comma.

Characteristic：

1.key-valueStructure;

2.keyMust be hash, and must be immutable data type, must be unique;

3.It can store any number of values, which can be modified or not unique.

4.Disorder.

### The creation and common operation of dictionaries

The creation of a dictionary

```>>> person = {'name':'houxingbin', 'age':18}
>>> person
{'name': 'houxingbin', 'age': 18}
# or
>>> person = dict(name = 'zhangsan',age=20)
>>> person
{'name': 'zhangsan', 'age': 20}
# or
>>> person = dict({'name':'lisi' , 'age':21})
>>> person
{'name': 'lisi', 'age': 21}
# or
>>> person = dict((['name','Wang Wu'],['age',18]))
>>> person```

The common operation of a dictionary

```# Key, value, key value pair
dic.keys() # Return a list containing all the key of the dictionary
dic.values() # Return a list containing all the value of the dictionary
dic.items() # Returns a list containing all (key, value) tuples.
dic.iteritems()、dic.iterkeys()、dic.itervalues() # Like their corresponding non iterative methods, the difference is that they return an iterator instead of a list:
dic['new_key'] = 'new_value';
dic.setdefault(key,None) # If the key key does not exist in the dictionary, it is assigned to it by dic[key] = default.
#delete
dic.pop(key[,default]) # It is similar to the get method. If there is a key in the dictionary, delete and return the key corresponding value; if key does not exist and does not give the value of default, it triggers the keyerror exception.
dic.clear() # Delete all the items or elements in the dictionary;
#modify
dic['key'] = 'new_value' # If key exists in the dictionary, 'new_value' will replace the original value value;
dic.update(dic2)  # Add the key value pair of the dictionary dic2 to the dictionary DIC
#See
dic['key'] # Returns the value of the corresponding key in the dictionary. If key does not exist in the dictionary, it will be wrong.
dict.get(key,default = None) # Returns the value of the corresponding key in the dictionary. If key does not exist in the dictionary, the value of default is returned (default is None by default).
#loop
for k in dic.keys()
for k,v in dic.item()
for k in dic
#length
len(dic)```