Can be reproduced arbitrarily, but the reprint must be marked original author Charlee, the original link http://tech.idv2.com/2008/11/03/python-memo/and this statement.
Basic concepts
- Python actually supports complex numbers. such as ( -5+4J). But note that the imaginary unit used alone to write 1j, can not be written as J.
- The difference between Perl and PHP: Single and double quotes are no different. No interpolation function.
- Triple quotes: "and" "is the equivalent of Perl's heredoc.
- A semicolon is not required at the end of a line, and a semicolon is used only for a single line to write multiple statements. Similar to the colon in basic.
- Indentation is semantically! It is recommended that you use the tab indent
Operators and expressions
- operator * is a power and Perl has it.
- The * representation of the string is duplicated, equivalent to the Perl x operator.
- There are also special to divide the//. Don't confuse Perl 6//.
Control flow
- If-elif-else,perl as If-elsif-else,bash for If-elif-fi
- If, elif, and other control flow statements to be followed by a colon, much like the syntax of PHP
- While and for loops can take else, the loop normally ends with an else. Do not like C language, the end of the loop to check whether the loop variable is equal to the final value, to determine whether the normal end. Praise
- The results generated by the range function do not include endpoints. Comparison: Perl's. Operators include endpoints. So, the Python range (a,b) is better understood as the C language for (i=a;i<b,i++).
- A pass statement that doesn't do anything, it's a python feature.
Function
- Global variables are required in a function, similar to PHP
- DocString, another Python feature, functions the first logical navigation string as a document string. Similar to pod and Javadoc, but written in the body of the function. It can also be accessed through the "function name. __doc__"
- Lambda: A closure similar to Perl
Data
- List: defined as a=[1,2]. An array/list equivalent to Perl
- Tuple (tuple): Defined by a= (1,2).
- Note that the list in the list will not be broken, such as a=[1,2], b=[a,3,4], and the result is b=[[1,2],3,4]. This is different from Perl.
- Dictionary: defined by c={' a ': 1, ' B ': 2}. Hash equivalent to Perl. Note two points:
- Key must be enclosed in quotation marks when defining and accessing.
- Using {} definitions, use [] access, that is, c[' a '. is different from Perl.
- A list assignment is a reference assignment. To copy, you must use a slice: b = a[:]. Perl programmers should be aware.
- The sort of the list changes the original list.
Exception
- Try-except-finally, the equivalent of Java try-catch-finally
- Also else, executed when no exception occurred, and the try block was ended normally. Like For/while's else block.
- Intentionally throws the unusual call Raise,java inside is called throw
Object-oriented
- The __init__ function of a class is similar to a C + + constructor, and the __del__ function is similar to a C + + destructor.
- The first parameter of a class method definition must be self, which is passed as the object itself when invoked. The first parameter, similar to Perl,perl, is also passed as the object itself, but requires the programmer to write my $self = shift; To receive.
- In the case of C + +, all member functions are virtual functions.
- You must manually pass self when calling a base class method, such as parnet.__init__ (self, name).
Module
- Import foo; Equivalent to perl use Foo;
- The From Foo Import bar is equivalent to Perl's use Foo qw/bar/;