Python Basic Learning Chapter III

Source: Internet
Author: User

One. Python Object type

1. The object is the most basic concept of Python, a Python program can be decomposed into modules, statements, expressions, and objects. Their relationship is as follows:(1) The program is composed of modules

(2) module contains statements

(3) statement contains an expression

(4) Expression creation and processing of an object

2. Python core data type (built-in object)

(1) Digital 324,3.1415,992l,3+4j,decimal

(2) Strings ' string ', ' Hello '

(3) List [2, [3, ' World '], 4]

(4) Dictionary {' name ': ' Bob ', ' taste ': ' Yum '}

(5) tuples ("So", ' Kej ', 34)

(6) File MyFile = open (' Eggs ', ' r ')

(7) Other types None, set, Boolean

3. Once an object is created, it binds to the action collection----can only string-related operations on strings, and list-related operations. Python is a dynamic type (capable of automatically tracking data types rather than requiring declarative code), and it is a strongly typed language (you can only work on an object effectively).

4. The following are specific descriptions of the above data types:

(1) Number

The number types in Python are subdivided into integers, floating-point numbers, infinite precision ' long ' shaping, complex numbers with imaginary parts, decimal numbers with fixed precision, and collections. There are also a number of common math modules: Math module, random module (randomly generated and randomly selected), etc.

(2) string

A string is the first example mentioned in Python as a sequence (an ordered collection containing other objects). The elements in the sequence contain a left-to-right order----The elements in the sequence are stored and read according to their relative positions. As a sequence, strings have some operations, such as using the built-in function Len to get its length, by indexing each element in the string (similar to the element in PHP that gets an indexed array). It's worth noting that Python supports the echo index! Here's a rule: a negative index is added to the length of the string, and the resulting operation is equivalent! In addition, the sequence also supports the operation of shards (slice) (personal understanding is similar to string interception in PHP, except that the syntax and the term are different). The basic syntactic form of a shard is this:x[i:j] means "remove the content from offset to I in X, but not including J". In one slice, the left boundary defaults to 0, and the right border defaults to the length of the Shard sequence. Therefore, the following equivalent results are produced:

Str[1:]----Str[1:len (str)], Str[:3]----Str[0:3], str[:]----Get the entire string

As a sequence, strings also support merging, using the + sign can be two characters to join together, and also can be repeated, using the * number

Note: The string has immutability. For example, you cannot change a string by assigning a value to one of its positions. But you can create a new string and assign it to the same variable name. Because Python cleans up the old objects while it is running.

Each object in Python can be divided into variability and immutability. In a core type, numbers, strings, pairs tuples groups are immutable, and lists and dictionaries are mutable. The above-mentioned length acquisition, element acquisition, and slicing operations are common operations for sequences, i.e. the above operations can also be used for lists and tuple sequences. There are specific methods for each data type in Python. Here's a rule: there are many types of general-purpose operations that occur in the form of built-in functions or expressions (for example, Len (X), x[0]), but a type-specific operation occurs in the form of a method call (such as Str.upper ())!

To know what methods an object has, you can use the built-in Dir function, such as DIR (str)

To know what each method does, use Help (Str.find) to get it:

Python allows strings to be included in single or double quotes, as well as in the form of a multiline string in three quotation marks (single or double quotes). When this form is used, all rows are merged together and a newline character is added at the end of each line.

It is also important to note that the method of the string object supports pattern-based text processing, which is the pattern matching described here. We need to import a module called Re. This module contains calls like search, split, and replace.

(3) List

The Python list object is the most common sequence available in this language. A list is an ordered collection of positions related to an arbitrary type of object, and it does not have a fixed size. The sequence operation that is used on the string is not described here. The Python list is much like an array in PHP, but the list is a lot more powerful. The list does not have a fixed type of constraint. Here's a look at the type-specific operations:

Use the Dir () built-in function:

The Append (), which is listed above, is the function of the pop () method, which appends the element to the list, and deletes the element at a location, which returns the deleted element.

bounds checking for lists : Python does not allow referencing elements that do not exist. Take a look at the following actions:

Python only allows you to modify elements in the list index range! To increase the list, you can use the Append method or the Insert method.

nesting : One of the characteristics of the Python core data type is that they support arbitrary nesting. It can be nested in any combination (let the list contain a dictionary, and include another list in the dictionary, etc.). A direct application of this feature is the implementation of a "multidimensional array" in the Matrix or Python.

The above effect shows a 3*3 number matrix. You can also get the elements in one of several ways:

List parsing : This is a more advanced operation to process the list, join we want to take the second column out of the matrix, you can do this:

List parsing is derived from the concept of a collection. is a new list that is created by running an expression in a sequence. The list parsing is written in square brackets (reminding you of the fact that you are creating the list), and there are expressions and looping structures, and variables (raw) are also used.

(4) Dictionary

A dictionary in Python is not a sequence, but a mapping. A map is a collection of other objects that are stored by key rather than relative position. The mappings do not have any reliable left-to-right order. Simply map the key to the value. A dictionary is the only type of mapping that is unique to a collection of Python core objects. Can be either increased or decreased with the need for variability.

Map operation: The dictionary is written in curly braces and contains a series of "key: value" pairs. You can retrieve the value of an element in the dictionary by way of an index.

Dictionaries are written in two ways: D = {' name ': ' Liu ', ' sex ': ' Boy ', ' Age ': 20} or D = {} d[' name '] = ' Liu ' d[' sex '] = ' boy ' d[' age ' = 20. The code is as follows:

Python Basic Learning Chapter III

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.