Python basic syntax

Source: Internet
Author: User
Tags natural string

1. Four numeric types: integer, long integer (L), floating point, plural (1 + 3j), and j representing the imaginary part)

2. String:

Single quotation marks and double quotation marks (single double quotation marks are equivalent and have no char type)

Three quotation marks (multiple rows)

Escape Character \ (Escape, line feed)

Natural string (r is added before the string, and the escape character is invalid)

Unicode string (the string is prefixed with u and processed in Unicode format)

Variable string (same as Java)

Two adjacent strings are automatically connected.

3. identifier: the first character can only contain letters or underscores. Other characters can contain letters, underscores, and numbers. It is case sensitive.

4. Variable: The type does not need to be declared. The variable name points to the memory area of the object (same as the C pointer)

5. physical and logical rows:

A line in the python file is a physical line, and each python command is a logical line.

A physical row can contain multiple logical rows, which must be separated by rows.

Only one logical row in a physical row can be omitted;

Multi-line string calculation as a logical line

6. indent: statements at the same level (code block) must be used for the same indent.

7. Operator priority: http://www.bkjia.com/shouce/pythonjc/ch05s03.html

8. if Condition Statement:

if condition:   statementselif condition:   statementselse:   statements

The condition statement does not require parentheses (). The end of the condition is followed by:, the code block must be indented.

9. while loop statement:

while condition:   statementselse:   statements

The condition statement does not require parentheses (). The end of the condition is followed by:, the code block must be indented; The else statement is executed only once at the end of the loop.

10.... In loop statement

for variable in sequence:   statementselse:   statements

The in statement can only be followed by sequences (lists, tuples, strings). The else statement is executed only once at the end of the loop.

11. break: Terminate and exit the current loop without executing the else statement of the loop.

12. continue: Skip the remaining statement of the loop and start the next loop directly.

13. List:

[item0, item1, item2, …]

Variable, ordered

Objects in the list can be accessed through [index] (same as the array)

14. tuples:

(item0, item1, item2, …)

Immutable (changes to the original tuples will directly generate new tuples, the original tuples remain unchanged), orderly

Objects in tuples can be accessed through [index ].

Only one element's tuples should be added after this element,

Tuples can be used to format strings, such

'%s is %d years old' % (name, age)

15. Dictionary:

{key0:value0, key1:value1, key2:value2, …}

Variable, key-value pair, unordered, yesdictClass instance

The key must be an unchangeable object and the key must be unique (Repeat will overwrite the original value)

The value in the dictionary can be accessed by using the key as the index [key]

The items () method of the dictionary returns a list of key and value tuples in the list.

16. sequence:

Strings, lists, and tuples are all sequences. They are ordered and Indexable.

The index operator is [index].

Indexes can be expanded to slices to obtain subsequences. The operator is [a: B], indicating that the elements starting from index a and ending with index B (including, does not contain B); If a has no value ([: B]), it indicates starting from the beginning. If B has no value ([a:]), it indicates that the end is included after the end. Therefore, [:] indicates the original sequence.

A non-negative number indicates scanning from the sequence header to the right, and a negative number indicates scanning from the sequence tail to the left.

17. Function Definition:

def function_name(
 
  ):   statements
 

Start with the keyword def: end; brackets can be filled with parameters (same as JavaScript)

18. Local variables and global variables:

There is no relationship between variables with the same name inside and outside the function; the variables in the function are local, and the scope of the variables is within the function.

The global variable declared in the function is a global variable. It points to the same memory region as the variable with the same name outside the function.

19. Default Value of the parameter: assign a value to the parameter = when the function is declared. the value to be changed is the default value of the parameter (same as JavaScript). The parameter with the default value must be on the right of the parameter without the default value.

20. Key Parameter: When a function is called, the parameter value is directly specified in brackets according to the parameter name, without the need to specify the parameter value in sequence.

21. return: jump out of the current function and return a value for the function (same as JavaScript)

22. _ doc __: the string of the first logical line of the function, module, and class is its document string, which is saved in the _ doc _ variable of the namespace. You can use . _ Doc _.

23. Module import: (the module is a collection file of several functions and classes)

import module

All functions and classes in the import module, but only the module itself is saved in the namespace. Therefore, the module. function Format must be used for actual calls.

from module import function/class/attribute

Import the specified function, class, or attribute of a module and save it to The namespace. when calling the module, you can directly use the specific function name, class name, or attribute name.

In addition, from module import * imports all functions, classes, and attributes in the module, and saves all imported objects in the namespace.

For code clarity, it is recommended to use the import module for import.

24. _ name __: the name of each module exists in the _ name _ variable of the namespace, the import module writes the value of its _ name _ attribute to the current namespace; the Variable _ name _ of the currently running module is automatically assigned as "_ main __"

25. Create a class:

class className:   statements

The class name is saved to the namespace of the module.

26. Define class methods:

class className:   deffunctionName(self, 
 
  ):        statements
 

The first parameter of the class method must exist (we recommend that you name it self) to indicate the class instance itself (same as this in Java)

27. constructor _ init __

class className:   def __init__(self, 
 
  ):        statements
 

When a new instance is created for the class, the __init _ method is automatically executed once.

28. destructor _ del __

class className:   def __del__(self):        statements

The system detects that the class instance is no longer used and automatically called to release the memory. You can also use the del statement to manually call it, as shown in figure

del instance

29. class variables and object variables:

The variables defined in the class are all class variables, and the class variables are copied in only one copy and saved in the namespace of the class. Any modification made to the class variables changes their values in other instances.

An object variable is only owned by an instance. It is stored in the self variable of each object.

Generally, class variables are public; only the variables whose names are prefixed with double underscores (_) are private)

30. Inheritance:

class subclass(superClass):   statement

Allow multiple inheritance

The subclass does not automatically call the constructor of the parent class. It must be manually called and the calling method is different from that of the common method, as shown below:

class subclass(superClass):   def __init__(self, 
 
  ):        superClass.__init__(self, 
  
   )
  
 

31. instantiation (similar to C ++ ):

instance = className(
 
  )
 

32. method call:

instance.functionName(
 
  )
 

Self is not required for calling

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.