The Python program can be decomposed into modules, statements, expressions, and objects as follows:
The program is composed of modules
Module contains statements
Statement contains an expression
Expressions set up and exported to an object
First, the description of Python type:
1.1 Why use built-in types
Python provides a powerful object type as part of the language, and in fact, unless there are special objects that the built-in types cannot provide, it is best to always use built-in objects instead of using your own implementations. Here's why:
Built-in objects make the program easier to write:
For simple tasks, built-in types can often represent all structures in the problem domain. For example, collections (lists) and search tables (dictionaries) can be used right away, and a lot of work can be done with only Python built-in object types.
Built-in objects enable an extended build:
For more complex tasks, you might still need to provide your own objects, using Python's class or C-language structure. For example, a stack data structure might be implemented as a class for managing and customizing built-in lists.
Built-in objects tend to be more efficient than custom data structures:
In terms of speed, Python's built-in type optimizes the implementation of data structure algorithms using C. Although you can implement a similar data type that belongs to you, it is often difficult to achieve the level of performance provided by the built-in data types.
Built-in objects are part of the language's standard:
Python not only borrows from languages that rely on built-in tools, such as Lisp, but also absorbs the benefits of languages that rely on programmers to provide their own tools or frameworks (such as C + +). Although a unique object type can be implemented in Python, there is no need to do so at the start stage. Also, because Python's built-in tools are standard, they are generally consistent.
1.2 Python's core data types
Python's built-in object types and some of the syntax used to write its constants (literal) are expressions that generate these objects.
Built-in objects:
Numbers, strings, lists, dictionaries, tuples, files, collections, other types, programming unit types, implementation-related types
The content listed above is not complete, because everything that is handled in a Python program is an object. For example, when you do text pattern matching in Python, you create schema objects, and when you do network scripting, you use socket objects, which are created from statements and expressions such as Def, class, import, and Lamba, and can be passed freely between scripts, and stored in other objects medium.
1.3 Numbers
Python's collection of core objects includes general types: integers, floating-point numbers, and more uncommon types (complex numbers with imaginary parts, decimal numbers with fixed precision, rational fractions with numerator and denominator, and collections).
The basic numeric type of Python is still fairly basic. The numbers in Python support general mathematical operations. where * * indicates a exponentiation.
>>> 12+3345>>> 1.5*46.0>>> 2 * * 1001267650600228229401496703205376
#注意这里的最后一个记过: When needed, the integer type of Python 3.0 automatically provides additional precision for larger values.
1.4 String
Just like a collection of arbitrary characters, a string is used to record text information. In a strict sense, a string is a sequence of strings of a single character, and other types of sequences also include lists and tuples.
As a sequence, the string supports operations that assume that each element contains a positional order. For example, if we have a four-character string, we validate its length with the built-in Len function and the index operation gets its individual elements.
' Spam '>>> len (s)4>>> s[0]'S'>>> s[1 ]'P'
In Python, the index is encoded at the front offset, starting with 0, the first index is 0, the second index is 1, and so on.
>>> s[-1]'m'>>> s[-2]'a' >>>
#在Python中, we can reverse the index from the last start (the reverse index is calculated from the left, and the reverse index is calculated from the right)
>>> S[len (S)-1]'m'
#一般来说, a negative index number is simply added to the length of the string, so the above operation and S[-1] are equivalent.
>>> s[1:3]'pa'
#除了简单的从位置进行索引, the sequence also supports the operation of a so-called Shard (Slice), which is a way to extract the entire shard (splice) in one step.
>>> s[1:]'pam'>>> s[0:3]'Spa ' >>> s[:3]'Spa'>>> s[:-1]' Spa'>>> s[:]'Spam'
#在一个分片中, the left border defaults to 0, and the right boundary defaults to the length of the Shard sequence.
>>> s+ 'xyz'spamxyz'>>> s*8' Spamspamspamspamspamspamspamspam '
#作为一个序列, strings also support merging with a plus sign (combining two strings into a new string), or repeating (by creating a new string once again).
The #注意加号 (+) has different meanings for different objects: for numbers, for addition, for strings for merging. This is the general nature of Python, which is what we will be talking about later in this book. In short, the meaning of an operation depends on the object being manipulated. The operations written by Python are usually automatically applied to different types of objects.
Python Object type