A simple introduction to Python programming

Source: Internet
Author: User
Tags class definition function definition

I. Statements and syntax

# Notes

\ translate carriage return, continue on the previous line, in a long line of statements can be used to cut into multiple lines, because of its poor readability, it is not recommended to use

; Connect two statements to one line with poor readability and not recommended

: Separates the header and the body of the Code

Statements (code blocks) are indented to reflect different levels of code, and it is recommended to use 4 spaces (do not use tab), because the number of spaces that the tab represents in different programming locales is not necessarily 4

Python files are organized in a modular way, and writing a. py end file actually writes a module

Two. Variable definition and Assignment

A=1:1 memory variables in memory, A is a reference to a variable, Python is a dynamic language, and the variable and its type do not have to declare the type beforehand

Differences from C and C + +: A=1 no return value

Note: The 1.C language variable declaration must be at the beginning of the code and be preceded by all statements

2.c++,java can declare variables anywhere, but must declare variable names and types

3.python can also declare variables anywhere, but when a variable is defined, the interpreter determines its type based on the value to the right of the equation.

4. Variables must be assigned before they can be used

Three. Memory Management memory Management:

1. Variable does not need to specify type

2. Programmers need not care about memory management

3. The variable is automatically recycled when it does not point to the assigned value

4.del ability to directly free memory objects (reduce reference count of objects)

Reference count:

To increase the reference count: 1. Object is created and its reference is assigned to a variable, reference count plus 1 (example a=1)

2. A reference to the same object is assigned to another variable, the reference count plus 1 (example b=a)

3. The object is called by the function as a parameter, and the reference count plus 1 (example int (a)

4. Object becomes an element in the container object, reference count plus 1 (example list_test=[' Alex ', ' Z ', a])

To reduce the reference count: 1.a as a function call parameter, after the function is finished, all local variables, including a, will be destroyed, the reference count minus 1

2. Variable is assigned to another object, the original object reference count minus 1 (example b=2,1 this memory object's reference only a)

3. Use Del to delete a reference to the object, minus 1 for the reference count (Example del a)

4.a as an element in the container list_test, is cleared and the reference count is reduced (example List_test.remove (a))

5. The container itself is destroyed (Example del list_test)

Note: Python memory recycling is given to a separate code, the garbage collector (with reference counters and a cyclic garbage collector), and the reference count is not cleared immediately (there may be cyclic calls) at zero

Instead of having to tangle with circular reference collection, just remember that the garbage collector helps you automatically clean up memory.

Simple example
X=1 # Creates a memory variable 1, passes a reference to the variable 1 to X, at the moment the reference count of 1 is increased to 2y=2 # of 1y=x # # to create a new memory variable 2, pass the reference to the variable 2 to Y, which points to 1 y, which is 2, so the reference count for 1 is reduced to 1del X # A reference to memory object 1 is deleted, and at the moment 1 is no longer referenced, it becomes the target of the Python interpreter's recovery.

Four. Python objects

In Python, where the object model is used to store data, the factory function used to generate the data type is essentially a class, and the result of the new data is the instantiation of an object

The object has three kinds of features: 1. Identity: Memory address, can be confirmed with ID (), the same object is the same ID

2. Type: Can be viewed with type (), the type of the return value is also an object

3. Value

Five. Identifiers

Definition: A valid string collection that is allowed as a name

1. The name must be meaningful and readable

2. The initial letter must be a letter or an underscore (_)

3. The remaining characters can be letters and numbers or underscores

4. Case sensitive

5. Two styles: Conn_obj or Connobj

6. Cannot use keywords, cannot use built-in

Built in: automatically imported by the interpreter (provides basic functionality), can be considered as a global variable,

Six. Dedicated underline identifiers

_xxx: Cannot import with from module import *

__XXX__: System-defined Name

__XXX: Private variables in class

Underscores are special for the interpreter and are symbols used by built-in identifiers, and it is not recommended that custom variables begin with an underscore

But if it's a private variable in the class, __xxx will be a good habit.

The system variable __name__ will derive different values depending on how the python file is loaded.

Python files are imported as modules: __name__= module name or file name

Python file executed: __name__= ' __main__ '

When we use Python to write a software, there should be only one main program that contains a large number of top-level code (that is, no indentation code, the Python interpreter reads to the top-level code will be executed immediately), the other. py files should

There is only a small amount of top-level code, and all features should be encapsulated in functions or classes.

Usually at the end of the file, combine the __name__ variable and write the test code.

Seven. Write the basic style of the module
    1. Title
    2. Document comments
    3. Module Import
    4. Variable definition
    5. Class definition statement
    6. function definition Statements
    7. Main program
    8. Test code

Eight. Demonstration
#_ *_coding:utf-8_*_#!/usr/bin/env python ' This is an example module ' Import sys,osdebug=trueclass Test:    '    test Class    '    passdef Main (): '    test func    : return: ' '    passif __name__ = = ' __ MAIN__ ':    Main ()

  

A simple introduction to Python programming

Related Article

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.