Click to pay attention to “little white CV”, learn from fast people step

Today is the second in a series of Python basic data types. After the last section, “Python Basic Data Types” (part 1), we’ll focus on the basic data types in Python.

As we all know, the flow of information that computers process is made up of all kinds of data, and Python is no exception. As you can see, data types in Python are particularly important.

There are three types of data in Python: numeric, character, and container. The next three dots provide a brief introduction to the above types.

It takes about 2 minutes to read the full text. It takes about 2 minutes to read the full text.

1. The numeric

The type of a number or value. Such as:

  • 3 this is an integer, 3.0 this is a fraction to one decimal place
  • Bool of type True=1, False=0
  • Complex Has real and imaginary parts

These are known as int, float, bool, and complex, all of which are single elements.

Among them, there are four different forms of integral type, respectively:

  • Base 2: Starts with ‘0b’. For example, ‘0b11011’ indicates 27 in base 10
  • Base 8: Starts with ‘0o’. For example, ‘0o33’ indicates 27 in base 10
  • Base 10: Normal display
  • Hexadecimal: Starts with ‘0x’. For example, ‘0x1b’ indicates 27 in base 10

At the same time, these values can be converted to each other, as follows:

>>> a=1 >>> print(type(a)) <type 'int'> >>> b=float(a) >>> print(type(b)) <type 'float'> >>> c=bool(a) >>> c True >>> D = 0 > > > = bool d1 (d) > > > print (d1) False > > > 3.0 b > > > f = complex (b) > > > f (3 + 0 j)Copy the code

2. Character

Data in the form of single quotation marks (“”) or double quotation marks (“”) is character data, as follows:

>>> a='hello world'
>>> a
'hello world'
>>> type(a)
<type 'str'>
>>> 
>>> b=",python"
>>> b
',python'
>>> c=a+b
>>> c
'hello world,python'
>>> c.replace('python','c++')
'hello world,c++'
>>> c.split()
['hello', 'world,python']
>>> c.split(',')
['hello world', 'python']
Copy the code

The process is as follows:

  • We first define a string a in single quotes (” “) and print the type of a
  • B =a+b; c=a+b; c=a+b
  • Print the contents of C, which is also a string
  • Then the keyword replace and split of the string are replaced

There are many ways to manipulate strings beyond simple examples, and we will study them in detail in a separate article.

In particular, string matching operation, especially in Python re regular module matching, powerful, easy to write, widely used in crawler, data analysis. (Dig a hole, then we have to fill it)

3. The container type

To understand what a container model is, first know what a container is. Colloquially, a container is a vessel that can hold something, such as a jar. It can hold one or more items, or it can be empty. To summarize, a container object that can hold multiple elements is a container data type.

Common container data include list, tuple, dict, and set. Python defines these types of variables in a very concise syntax. As follows:

List LST =[1,2,3] tuple tup=(1,2,3) dict dictionary object DIC ={'a':1, 'b':1, 'b':1} set set object s={1,2,3}Copy the code

The following characteristics can be found by comparing the numerical data:

  1. Like a jar, can hold one or more data
  2. It can be empty, empty container
  3. Container data comparison is diverse

Finally, there are a few caveat about numeric and container data types, which we will encounter frequently.

  1. Numerical data is a single, container data is a series of increases and decreases
  2. Container data and numeric data can be contained in the contained relationship, that is, an element of the container data, which can be numeric; Multiple numeric data can also be composed of a container data
  3. List can have duplicate elements, not set
  4. The elements of a tuple cannot be modified
  5. Dict dictionaries are made up of key-value pairs

Here is a set of transformations to illustrate both numeric and container data, as follows:

> > > LST = [1, 2, 3] > > > type (LST) < type 'list' > > > > s = set (LST) > > > s the set ([1, 2, 3]) >>> type(s) <type 'set'> >>> dct={'a':1,'b':2,'c':3} >>> dct {'a': 1, 'c': 3, 'b': 2} >>> type(dct) <type 'dict'> >>> for key, value in dct.items(): ... print(key, value) ... print(type(key),type(value)) ... ('a', 1) (<type 'str'>, <type 'int'>) ('c', 3) (<type 'str'>, <type 'int'>) ('b', 2) (<type 'str'>, <type 'int'>)Copy the code

At the end of the article, there are links to some excellent articles on data typing and processing, as a supplement, if you are interested in reading them at the bottom.

Last but not least, slow is fast. Welcome to leave messages and private messages. This public number is a message function, looking forward to communicating with you. Let us study together on this road, hand in hand. PS: Xiao Bai CV recruit co-editor, like writing summary of friends, to share together, background contact with Xiao Bai Jun wechat.

Previous data series review: \

  • Python data Series (I) – List: Python’s coolies
  • Data series (part 2) : Dictionary of Python
  • Tuple: Python’s “immutable sequence” \