# data type

There are six standard data types in Python 3:

• Number
• String (string)
• List
• Tuple (tuple)
• Set
• Dictionary

The immutable types are Number, String and Tuple
Variable types: List, Dictionary, Set

## number

The numbers supported in Python 3 include int (integer), float (floating point number), bool (Boolean value) and complex (complex number). The assignment and calculation of numeric types are very intuitive.

```#You can use the built-in function type() to query the object type that the variable refers to
>>> a, b, c, d = 1, False, 3.2, 3+6j
>>> print(type(a), type(b), type(c), type(d))
<class 'int'> <class 'bool'> <class 'float'> <class 'complex'>

#You can also use isinstance to judge
>>> a ,b = 2 , True
>>> isinstance(a, int)
True
>>> isinstance(a, bool)
False
>>> isinstance(b, bool)
True
```

Note: type() does not consider a subclass to be a parent type, while isinstance() considers a subclass to be a parent type.

```>>> class A:
...     pass   #The pass statement is used to occupy bits. It has no practical significance. It should be indented in front of it
...
>>> class B(A):
...     pass
...
>>> isinstance(A(), A)
True
>>> type(A()) == A
True
>>> isinstance(B(), A)
True
>>> type(B()) == A
False
```

In Python 3, bool is a subclass of int, so True and False have corresponding integers, True1 and False 0. You can judge the type through is. In Python 2, there is no Boolean. It uses the number 0 to represent False and 1 to represent True.

```>>> issubclass(bool, int)
True
>>> True==1
True
>>> False==0
True
>>> True+2
3
>>> False+2
2
>>> 1 is True
False
>>> 0 is False
False
```

## character string

Strings in Python are quoted in single quotation marks or double quotation marks. At the same time, backslashes \ can be used to escape special characters. Each character in the Python string is equivalent to an index. The following is the method to intercept Python strings. Note: Python has no separate character type, and a character is a string with length of 1

```------
Python The index from the front to the back starts with 0 and from the back to the front is 0-1 start
0  1  2  3  4  5  6
x  i  a  o  b  a  i
-7 -6 -5 -4 -3 -2 -1
------
>>> str = "xiaobai"
>>> print(str)   Print string
xiaobai

#Indexes
>>> print(str[0:5])   For characters from index 0 to index 5 (excluding index 5), print the first to fifth characters
xiaob
>>> print(str[0:-1])   From index 0 to index-1 Characters (excluding index)-1)，Print first to penultimate character
xiaoba
>>> print(str[0])   Print the characters of index 0, and print the first character
x
>>> print(str[2:5])   Print the characters from index 2 to index 5 (excluding index 5), and print the third to fifth characters
aob
>>> print(str[3:])   Print all characters after index 3 and print all characters after the fourth
obai

#Escape character
>>> print('xiao\nbai')   add n Add before\ Then it becomes a newline character
xiao
bai
>>> print(r'xiao\nbai')   You can add it at the beginning r Do not escape backslashes
xiao\nbai

#Splice copy
------
plus+ You can connect two strings, asterisks* Indicates that the current string is copied, and the number combined with it is the number of times it is copied
------
>>> print(str * 3)   Copy the string three times and output it
xiaobaixiaobaixiaobai
>>> print(str + "zhenshuai")   Splice a new string after the string"zhenshuai"
xiaobaizhenshuai
>>> print(str + "zhenshuai" * 2)
xiaobaizhenshuaizhenshuai
>>> print((str + "zhenshuai") * 2)
xiaobaizhenshuaixiaobaizhenshuai

#character
>>> print(str[0], str[4])
x b
>>> print(str[-3], str[-7])
b x
```

Python strings cannot be changed. Assigning a value to an index position, such as STR [0] ='q ', will cause an error

## list

List can complete the data structure implementation of most collection classes. The types of elements in the list can be different. It supports numbers. Strings can even contain lists (so-called nesting). Lists are lists of elements written between square brackets [] and separated by commas. Like strings, lists can also be indexed and intercepted. After being intercepted, a new list containing the required elements is returned.

```The operation of the list is basically the same as that of the string, and the format of the index is basically the same
>>> list = ['abcd', 789, 3.14, 'xiao', 52.6]
>>> tinylist = [123, 'bai']
>>> print(list)
['abcd', 789, 3.14, 'xiao', 52.6]
>>> print(list[0])
abcd
>>> print(list[1:3])
[789, 3.14]
>>> print(list[2:])
[3.14, 'xiao', 52.6]
>>> print(tinylist * 2)
[123, 'bai', 123, 'bai']
>>> print(list + tinylist)
['abcd', 789, 3.14, 'xiao', 52.6, 123, 'bai']

------
And Python Unlike strings, the elements in the list can be changed
------

>>> a = [1, 2, 3, 4, 5, 6]
>>> a[0] = 9
>>> print(a)
[9, 2, 3, 4, 5, 6]
>>> a[2:5] = [13, 14, 15]
>>> print(a)
[9, 2, 13, 14, 15, 6]
>>> a[2:5] = []
>>> print(a)
[9, 2, 6]

------
Python List interception can receive the third parameter. The parameter is used to intercept the step size. The following example intercepts the string at the position from index 1 to index 4 and set the step size to 2 (one position at an interval)
------

>>> xb = ['x', 'i', 'a', 'o', 'b', 'a', 'i']
>>> print(xb[1:4:2])   Print index 1 to index 4 in steps of 2
['i', 'o']
```

The list has many built-in methods, such as append(), pop(), etc

## tuple

Tuples are similar to lists, but the elements of tuples cannot be modified. Tuples are written in parentheses and separated by commas

```>>> tuple = ['abcd', 789, 3.14, 'xiao', 52.6]
>>> tinytuple = [123, 'bai']
>>> print(tuple)
['abcd', 789, 3.14, 'xiao', 52.6]
>>> print(tuple[0])
abcd
>>> print(tuple[1:3])
[789, 3.14]
>>> print(tuple[2:])
[3.14, 'xiao', 52.6]
>>> print(tinytuple * 2)
[123, 'bai', 123, 'bai']
>>> print(list + tinytuple)
['abcd', 789, 3.14, 'xiao', 52.6, 123, 'bai']

>>> tup = (1, 2, 3, 4, 5, 6)
>>> print(tup[0])
1
>>> print(tup[1:5])
(2, 3, 4, 5)
>>> tup[0] = 11  # It is illegal to modify tuple elements
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

>>> tup1 = ()    # Empty tuple
>>> tup2 = (20,) # For an element, you need to add a comma after the element
```

Although the elements of a tuple cannot be changed, its elements can contain variable objects, such as lists. string, list, and tuple all belong to sequence

## aggregate

A set is composed of one or several large and small units in different shapes. The things or objects constituting the set are called elements or members. The basic function of the set is to test the membership relationship and delete duplicate elements. You can use the curly braces {} or set() function to create the set. Note: to create an empty set, you must use set() instead of {}, Because {} is used to create an empty dictionary

```------
Collections can be created in two ways
parame = {value01,value02,...}   Mode 1
set(value)   Mode II
------

>>> sites = {'xiaobai', 'xiaohu', 'xiaoming', 'niutou', 'xiaobai', 'wuqi'}
>>> print(sites)    Duplicate elements are automatically removed
{'xiaoming', 'xiaohu', 'xiaobai', 'niutou', 'wuqi'}

>>> if 'xiaobai' in sites :
...     print('xiaobai In collection')    #Four spaces as indent
... else :
...     print('xiaobai Not in collection')
...
xiaobai In collection    #Output results

set You can also perform set operations
>>> b = set('sagrd')
>>> print(a)
{'d', 's', 'a', 'g', 'f'}
>>> print(a - b)     # Difference sets of a and b
{'f'}
>>> print(a | b)     # Union of a and b
{'r', 'd', 's', 'a', 'g', 'f'}
>>> print(a & b)     # Intersection of a and b
{'s', 'd', 'a', 'g'}
>>> print(a ^ b)     # Elements in a and b that do not exist at the same time
{'r', 'f'}
```

## Dictionaries

Dictionary is another very useful built-in data type in Python. The list is an ordered collection of objects and the dictionary is an unordered collection of objects. The difference between the two is that the elements in the dictionary are accessed through keys rather than offset
A dictionary is a mapping type. The dictionary is identified by {}. It is an unordered set of keys: values. Keys must use immutable types. In the same dictionary, keys must be unique

```>>> dict = {}
>>> dict['one'] = "1 - Xiaobai"
>>> dict[2]     = "2 - xiaobai"
>>> print(dict)
{'one': '1 - Xiaobai', 2: '2 - xiaobai'}
>>> print (dict['one'])    # Output the value with key 'one' in dict
1 - Xiaobai
>>> print (dict[2])        # Output the value with key 2 in dict
2 - xiaobai

>>> tinydict = {'name': 'bai','code':1, 'site': 'www.xiaobai.plus'}
{'name': 'bai', 'code': 1, 'site': 'www.xiaobai.plus'}
>>> print (tinydict.keys())     # Output tinydict all keys
dict_keys(['name', 'code', 'site'])
>>> print (tinydict.values())   # Output all tinydict values
dict_values(['bai', 1, 'www.xiaobai.plus'])

------
Constructor dict()You can build dictionaries directly from the sequence of key value pairs, Dictionary types have many built-in functions, for example clear(),keys(),values()etc.
------
>>> dict([('Xiaobai', 1), ('Google', 2), ('Taobao', 3)])
{'Xiaobai': 1, 'Google': 2, 'Taobao': 3}
>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}