Python path-------4th chapter, introducing Python object types

Source: Internet
Author: User

An object is a part of memory that contains a collection of numeric values and related operations. Python can be decomposed into modules, statements, expressions, and objects: A program consists of a module, a module contains a statement, a statement contains an expression, an expression is established, and an object is processed.
1.1, why use the built-in type?
With C or C + +, a large portion of the work is focused on objects, requiring the deployment of memory structures, managing memory allocations, and implementing search and read processes. This is not the case with Python, which provides a powerful object type as part of the language.
Unless there are special objects that the built-in type cannot provide, it is best to always use built-in objects instead of using your own implementations.
1.2. Python Core Data type
Programming units such as functions, modules, and classes are also objects in Python, which are created by expressions and statements such as Def, import, and class; An expression of square brackets generates a list, and the curly brace expression creates a dictionary; there is no type declaration in Python. The syntax of the running expression determines the type of object that is created and used; Once an object is created, it is bound to the Operation collection-only string-related operations can be performed on strings. Python is a dynamic type (it automatically tracks the type of the variable), but it is a strongly typed language. The list provides an ordered collection of other objects, and the dictionary stores the objects by key. Lists and dictionaries can be nested, can be extended and truncated with requirements, and can contain any type of object.
Print (sys.modules) # #只列出当前已经import的module, modules without import on disk are not listed
{' tokenize ': <module ' tokenize ' from '/usr/lib64/python2.6/tokenize.pyc ';, ' Copy_reg ': <module ' Copy_reg ' From '/USR/LIB64/PYTHON2.6/COPY_REG.PYC ';
1.2.1, digital
+, *, * * (to indicate the exponentiation)
Len (str (2**1000000))/random.random ()/random.choice ([1,2,3,4])
1.2.2, String
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. Strictly speaking, a string is a sequence of strings of a single character.
Forward and backward index operations and shard operations for strings. The general form of a shard is x[i:j], which means that the removal begins at offset I in X until it does not include the content of the offset of J. In a shard, the left boundary defaults to 0, and the right boundary defaults to the length of the Shard sequence. + (string merge operation), * (string repeat operation) the meaning of a method depends on the object being manipulated, and this polymorphic feature brings great simplicity and flexibility to Python code. Because types are not constrained, Python-written operations can often be applied automatically to different types of objects.
1.2.3, non-denatured
Immutability: cannot be changed in place after creation
Each object in Python can be divided into immutability or variability. In a core type, numbers, strings, and tuples are immutable, and lists and dictionaries are mutable.
1.2.3, types of methods
One simple rule is this: available for multiple types of general-purpose operations that occur in the form of built-in functions and expressions, such as Len (x), x[0], but type-specific operations occur in the form of method calls.
1.3. Pattern Matching
The RE module includes calls like search, split, and replace.
1.4. List
The list of Python is similar to a list in other languages; the Python list does not have a fixed type of constraint; the Python list does not have a fixed size, and you can expand or reduce the size of the list as needed.
Sequence operations for 1.4.1, lists
>>> l=[123, ' spam ', 1.23]
>>> dir (L)
[' __add__ ', ' __class__ ', ' __contains__ ', ' __delattr__ ', ' __delitem__ ', ' __delslice__ ', ' __doc__ ', ' __eq__ ', ' __format_ ' _ ', ' __ge__ ', ' __getattribute__ ', ' __getitem__ ', ' __getslice__ ', ' __gt__ ', ' __hash__ ', ' __iadd__ ', ' __imul__ ', ' __init_ _ ', ' __iter__ ', ' __le__ ', ' __len__ ', ' __lt__ ', ' __mul__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ' , ' __reversed__ ', ' __rmul__ ', ' __setattr__ ', ' __setitem__ ', ' __setslice__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ' ', ' append ', ' count ', ' extend ', ' index ', ' Insert ', ' pop ', ' remove ', ' reverse ', ' sort ']
1.4.2, type-specific operations
>>> L
[1.23, ' spam ', 123]
>>> L.sort ()
>>> L
[1.23, 123, ' spam ']
>>> L.reverse ()
>>> L
[' Spam ', 123, 1.23]
1.5. Nesting
One of the great features of Python's core data types is that they support arbitrary nesting, can be nested in any combination, and can be nested in multiple layers.
1.6. The pass statement is a placeholder statement without operations, which can be used when the syntax requires a statement and there is no practical statement to write.
While <test>:
<statements1>
Else
<statements2>
Python does not allow assignment statements to appear where the expression should be, and the C assignment operation returns the value after the assignment, but the Python assignment statement is only a statement, not an expression.
For loop when a generic sequence iterator in Python: can traverse elements within any ordered sequence, for statements can be used for strings, lists, tuples, other built-in iterative objects, and new objects that can then be created through the class
For <target> in <object>
<statements>
If <test>: Break
If <test>: Continue
Else
<statements> # #if We didn ' t hit a break
When Python runs a For loop, the elements in the sequence object are assigned a value to the target, and then the loop body is executed for each element.
1.7. Loop counter: while, range
The range function is a generic tool, and range is commonly used to produce indexes in a for loop, but it can also be used in any place where an integer list is required.
When the range function uses a parameter, range produces a list of integers that increment by 1 from 0, but does not contain the value of the parameter; when there are 2 parameters, the first one is considered the bottom boundary; when there are 3 arguments, the third parameter provides a stepping value. eval function: eval (str [, Globals [, locals]), which can be used to tell the string str to be evaluated as a valid Python expression and return the result of the calculation
1.8. Parallel traversal: Zip and map
The built-in function range iterates through the sequence in a for loop in a non-exhaustive way; for (x, y) in Zip (List1,list2), the For loop uses a tuple assignment operation to unpack each tuple in the zip result.
Using the Dict constructor, you can convert lists or zip-past key/value pairs into dictionary types, and what are list parsing and python3.0 dictionary parsing??







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python path-------4th chapter, introducing Python object types

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.