A simple introduction to Python programming

Source: Internet
Author: User
Tags class definition function definition object object

I. Statements and syntax

# comment \  Translate carriage return, continue on the previous line, in the case of a long line of statements can be used to cut into more than one line, because of its poor readability is not recommended; Connect two statements to one line, poor readability, not recommended: The code's head and body separate statements (blocks of code) are indented to reflect different code levels. 4 spaces are recommended (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, 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 difference between the type and 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 2.c++ before all statements. Java can declare variables anytime, anywhere, but you must declare variable names and type 3.python, and you can declare variables anywhere, but when a variable is defined, the interpreter determines its type 4 based on the value to the right of the equation. The variable must be assigned before it can be used

  

Three. Memory management
1. The variable does not need to specify type 2. The programmer does not care about memory management 3. The variable does not point to the given value when it is automatically reclaimed 4.del can directly free the memory object (reduce the object's reference count)

Reference count: To increase the reference count:

1. The object is created and its reference is assigned to the variable, the reference count plus 1 (example a=1) 2. References to the same object are assigned to other variables, the reference count plus 1 (example b=a) 3. The object is called by the function as a parameter, the reference count plus 1 (example int (a)) 4. The object becomes an element Reference count plus 1 (example list_test=[' text ', ' z ', T])

 

To reduce the reference count:
1.a as a parameter of a function call, after the function is run, all local variables, including a, are destroyed, and the reference count is reduced by 1 2. The variable is assigned to another object, the reference count of the original object is reduced by 1 (example b=2,1 the reference to this memory object is only a) 3. Use Del to delete a reference to an object, Reference count minus 1 (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 (which contains reference counters and a cyclic garbage collector), and the reference count is not immediately cleared (there may be circular calls) without having to tangle with circular reference collection, just remember that the garbage collector helps you automatically clean up the memory.

Simple example
X=1 # Creates a memory variable 1, passes a reference to the variable 1 to x, the reference count of 1 is incremented to 2 y=2 # for the reference count of 1y=x  # # # to create a new memory variable 2, pass a reference to the variable 2 to Y, which points to 1 Y, now 2, so 1 of the reference count is reduced to 1 del 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 that there are three characteristics of instantiating an Object object:1. Identity: Memory address, can be confirmed with ID (), the same object with the same ID 2. Type: Can be viewed with type (), the type of the return value is also 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 first letter must be a letter or an underscore (_) 3. The remaining characters can be letters and numbers or underscores 4. Case Sensitivity 5. Two styles: Conn_obj or Connobj 6. Cannot use keywords, Built-in built-in is not available: automatically imported by the interpreter (providing basic functionality), can be considered as a global variable,

  

Six. Dedicated underline identifiers
_xxx: Cannot import __xxx__ with from module import *: System-defined name __XXX: Private variable underline in class has special meaning for the interpreter, and is the symbol used by the built-in identifier, it is not recommended to start the custom variable with an underscore but if it is a private variable in the class,     __xxx will be a good habit    . The system variable __name__ will draw different values depending on how the python file is loaded. The python file is imported as a module: the __name__= module name or the file name python file is executed: __name__= ' __main__ '    when we use Python to write a software, There should be only one main program that contains a lot of top-level code (that is, code without indentation, the Python interpreter reads to the top-level code immediately), and the other. py file should    have only a few top-level code, all of which should be encapsulated in a function or class.    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. Documentation Note 3. Module Import 4. variable definition 5. class definition statement 6. function definition Statement 7. Main program 8. Test code

  

Eight. Demonstration
#_ *_coding:utf-8_*_#!/usr/bin/env python ' This is an example module ' Import Sys,os debug=true class Test:    ' 
   
    test class    '    pass def Main (): '    test func    : return: ' '    pass if __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.