Python Study Notes

Source: Internet
Author: User

The original author charlee and original link http://tech.idv2.com/2008/11/03/python-memo/can be reproduced at will.

Basic Concepts
    • Python supports the plural. For example, (-5 + 4j ). Note that the virtual number unit must be written as 1j rather than J when used separately.
    • Difference from Perl and PHP: single quotes and double quotes are no different. No interpolation function.
    • Three quotation marks: ''' and "" are equivalent to Perl's heredoc.
    • You do not need to use a semicolon at the end of a row. A semicolon is only used to write multiple statements in one row. Similar to the basic colon.
    • Indentation has Semantics! We recommend that you use tab indentation.
Operators and expressions
    • The ** power operator also exists in Perl.
    • The string * Indicates repetition, which is equivalent to the Perl x operator.
    • Actually there is a/dedicated for division //. Do not confuse it with Perl 6.
Control Flow
    • If-Elif-Else, Perl is if-elsif-Else, bash is if-Elif-fi
    • If, Elif, and other control flow statements must be followed by a colon, which is similar to the PHP syntax.
    • The while and for loops can contain else. Else is executed when the loop ends normally. You do not need to check whether the cyclic variable is equal to the final value at the end of the loop like the C language to determine whether the loop ends normally. Like
    • The result generated by the range function does not include the end point. Comparison: The... operator of Perl includes the end point. Therefore, the range (A, B) in python is better understood as for (I = A; I <B, I ++) in C language.
    • A pass statement that does not do anything is a special feature of Python.
Function
    • The Global keyword is used in the function to use global variables, which is similar to PhP.
    • Docstring, also a python feature, the first logical character string of the function as the document string. Similar to pod and javadoc, it is written in the function body. You can also use "function name. _ Doc _" to access
    • Lambda: A closure similar to Perl
Data Structure
    • List: defined as a = [1, 2]. Equivalent to Perl arrays/lists
    • Tuple: defined as a = (1, 2 ).
    • Note that the list in the list will not be dispersed, for example, a = [1, 2], B = [A, 3, 4], and B = [[1, 2], 3, 4]. This is different from Perl.
    • Dictionary: The definition method is c = {'A': 1, 'B': 2 }. It is equivalent to a Perl hash. Note:
      • During definition and access, the key must be enclosed in quotation marks.
      • Use {} to define and access [], that is, C ['a']. Unlike Perl.
    • A list value assignment is a reference value assignment. To copy a file, use slice: B = A [:]. PerlProgramAttention.
    • The sort of the list changes the original list.
Exceptions
    • Try-catch t-finally, equivalent to Java try-catch-finally
    • There is also else, which is executed when no exception occurs and the try block ends normally. Same as the else block for/while
    • Raise is thrown intentionally, and throw is called in Java.
Object-oriented
    • The _ init _ function of the class is similar to the C ++ constructor; __del _ function is similar to the C ++ destructor.
    • The first parameter defined by the class method must be self. This parameter will be passed as the object itself when called. Like Perl, the first parameter in Perl will also be passed as the object itself, but the programmer needs to write my $ self = shift; to receive it.
    • In C ++, all member functions are virtual functions.
    • You must manually pass self when calling the base class method, for example, parnet. _ init _ (self, name ).
Module
    • Import Foo; equivalent to use Foo of Perl;
    • From Foo import bar is equivalent to Perl's use Foo QW/BAR /;

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.