Can be reproduced arbitrarily, but must be reproduced when the 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). However, it is important to note that imaginary units are written as 1j when used alone and cannot be written as J.
- The difference from Perl and PHP: There is no difference between single and double quotes. There is no interpolation function.
- Three quotes: "and" "are equivalent to Perl's heredoc.
- A semicolon is not required at the end of a line, and a semicolon is used only for one line of multiple statements. It is somewhat similar to the colon of basic.
- Indentation is semantic! We recommend using the tab indent
Operators and expressions
- The operator * * is a.
- The * representation of a string is repeated, equivalent to the X operator of Perl.
- There are also dedicated to divide the//. Don't confuse Perl 6//.
Control flow
- If-elif-else,perl to If-elsif-else,bash for If-elif-fi
- If, elif and other control flow statements are preceded by a colon, much like PHP syntax
- While and for loops can take else, the loop executes the else when it ends normally. Instead of using the C language, check that the loop variable is equal to the final value at the end of the loop to determine if it ends normally. Praise
- The result generated by the range function does not include the end point. Comparison: Perl's.. The operator includes the end point. Therefore, the Python range (A, b) is interpreted as a C language for (i=a;i < li="">
- A pass statement that doesn't do anything, it's a python feature.
Function
- Using global variables in a function is required with the global keyword, similar to PHP
- DocString, another Python feature, functions the first logical voyage of a string as a document string. Similar to pods and Javadoc, but written in the body of the function. can also be accessed through the "function name. __doc__"
- Lambda: perl-like closures
Data
- List: defined as a=[1,2]. Array/list equivalent to Perl
- Tuple (tuple): defined as a=.
- Notice that the list is not 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 as c={' a ': 1, ' B ': 2}. Hash equivalent to Perl. Note two points:
- When defining and accessing, key must be enclosed in quotation marks.
- Using the {} definition, use [] access, which is c[' a ']. Different from Perl.
- A list assignment is a reference assignment. To copy, you must use a slice: b = a[:]. Perl programmers should pay attention.
- The list sort will change the original list.
Exception
- Try-except-finally, equivalent to Java's try-catch-finally
- There is also else, which executes when no exception occurs and the try block ends normally. Same as For/while's else block.
- Intentionally thrown exception called Raise,java inside 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 argument of a class method definition must be self, which is passed as the object itself when called. Similar to the first parameter in Perl,perl is also passed as the object itself, but requires the programmer to write my $self = shift; To receive.
- In C + +, all member functions are virtual functions.
- When you call a base class method, you manually pass self, such as parnet.__init__ (self, name).
Module
- Import foo; Use Foo equivalent to Perl;
- The From Foo Import bar corresponds to Perl's use Foo qw/bar/;