"Turn" python learning--based on

Source: Internet
Author: User
Tags python script

Learning Python originally wanted to summarize, but found a good Daniel's blog, take doctrine, and I practiced the

About the first two if the summary of the not detailed, so the other people's reprint come over http://www.cnblogs.com/BeginMan/archive/2013/04/12/3016323.html

First, to practice this work, will be self-palace

From now on, began to really touch her, perhaps a lot of people like me, do not know how to quickly master a new programming language, today put out some suggestions, easy to see, this is a very important thing before learning. Although "to practice this work, must first from the palace" is too exaggerated, but before the study, must be ready. As a result of working with Python, one months before work began to study and put into the actual project, a lapse of one months of study, today to relive it again. I am prepared to do the following:

1. Tool Preparation

This is already done in the previous chapter of the study notes.

2. Information preparation

"Python Core Programming"
Official Python documentation
Blog Park, CSDN and other communities

3. Getting Started Tutorial preparation

Complete the Python introductory notes about June ago.

4. Psychological preparation

about how to learn a new programming language, it is recommended to read: http://www.cnblogs.com/chgaowei/archive/2011/05/29/2061902.html

5. Knowledge Framework Split Preparation

By splitting the framework throughout the Python learning process and using the Python core programming book, which covers almost all Python knowledge points, I still have difficulty mastering her entire learning framework and process for beginners like me. If we look at this book roughly, we can subdivide her frame and divide it into modules to learn. Here's the framework I split: (Because we're learning from the Python basics, we don't consider the Advanced section here)

[1], the basic Python part:

(1). Syntax basis, (2), data type, (3). Python objects, (4). strings, Ganso, and lists, (5). Mappings and collections, (6). Functions

[2], Process Control

Conditions and loops

[3], file operation

[4], module

[5], Object-oriented

[6], exception handling

[7], environment deployment

Well, all of these preparations are in place, so start learning now, and when it's all right, suggest a look at the Python philosophy.

Second, Python syntax

I don't know how I wrote the first Python program before, and it seems to have been written in the tutorial "Dive into Python", which was a lesson in the pain of an egg. As always, we start from the "HelloWorld", haha, we learn a lot of language from the beginning of HelloWorld.

>>> 1 + 1               2>>> print ' Hello World ' Hello world>>> x = 1               >>> y = 2>>> X + y3

This example does not explain what syntax, but it can be familiar to us, for example,Python is both a dynamic type definition language (because it does not use the display data type declaration), but also a strongly typed definition language ( Because once a variable has a data type, it is actually always the type, and the end of a sentence does not end with a semicolon, print can output a string, and so on. Let's go over the Python syntax below.

1. Comments

Like many Unix scripts, Python's comment statements start with the # character, such as:

# print "Hello World"

Note that Python's # character annotations can only implement single-line comments, perhaps a lot of people and I am just as curious, if you want to comment out more than 100 lines of code, it is not hit more than 100 #?? Needless to say, if we do this, we can only explain or soy sauce hired to help soy sauce people.

We can have the following tips:

(1). Program Control Method:

Without affecting the entire program, we can put a large number of code snippets to be commented out, using an if conditional sentence for indirect comments, that is, if the code block to be commented on the execution, you can use if False:, if None:, if 0: Mask processing, as follows:

1 in Eclipse: 2 print ' Beginman '    #输出 3 if False:           #屏蔽, if can also be 0, none, etc. all Boolean value false condition 4     print ' Hello World ' 5     print ' Good man ' 6     print ' sssss ' 7 print ' ok! '         #输出

The IF code is not executed, then no output is made. But this technique, it is best not to use, there may be a lot of code exists in the case of latent anomalies.

(2). Editor Tips

If you select more than one line under Eclipse, you can use the shortcut key ctrl+/Comment or uncomment, this strongly recommended, really "工欲善其事, its prerequisite" Ah, for the editor, it is best to master the commonly used shortcut keys, in order to improve the development efficiency.

(3). Doc String (document String)

1 def foo (params): 2     "" "Print A,b,c and return a" ""

Sanchong quotation marks, which contain both single and double quotation marks, represent a multiline string. Everything between the start and end quotes is treated as part of a single string, including a hard carriage return and other quote characters. Everything in the Sanchong quotes is the doc stringfor this function, which is used to illustrate what the function can do. If a doc stringexists, it must be the first content to be defined by a function (that is, the first content after the colon). The doc string that is not technically required to give the function , but should do so. Python brings some extra motivation: doc string can be used as a function property at run time.

2. Line break (\ n)

Common line breaks are often seen in other programming languages.

3. backslash (\) to continue on line

That is, a line of one statement of the interlaced, for some too long statements can be used to break down the backslash \ to multiple lines, such as:

1 if a>0 and 2 b<0:3     print ' ok! '

There are two exceptions that can be used without backslashes, one is: parentheses (), brackets [], curly braces {}, and the other is a three-way quotation mark. A backslash is not recommended for readability.

4, Code indentation

The Python function does not have a distinct begin and end, and no curly braces for the start and end of the marked function. The only delimiter is a colon (:), and then the code itself is indented. Examples can refer to the above. Note that the code indentation is very strict, if you do not follow the rules, careless words will appear grammatical errors, such as unexpected indent. There are even logic errors that sometimes occur.

Reference: http://www.cnblogs.com/tt-0411/archive/2011/11/11/2245693.html

5. Use semicolons (;) can write multiple statements on the same line

such as; Import OS; a=123; Print a

Note that the end of the sentence is not to be taken; This is not recommended because it greatly reduces the readability of the code.

6. Module

Each Python script is considered to be a module that is stored as a disk and can be split into multiple modules several times. Imported using Import.

Iii. Variables for Python

Note that unlike programming languages such as C #, Java, C + +, and so on, Python's variable has no type, and its type is determined in memory based on the object. Because of the Python function section, this section does not speak first, and is explained in a later section. In this section, we first learn about variable assignment.

Python is a weak type, without explicitly specifying the variable type, and the type is automatically declared at the same time that the value is assigned . Such as:

1 >>> anint=102 >>> astr= "Car" 3 >>> afloat=-1.2+34 >>> alist=[1,2,3]5 >>> atuple= (' AA ', ' SS ') 6 >>> print Anint,astr,afloat,alist,atuple7 Car 1.8 [1, 2, 3] (' AA ', ' SS ')

We need to pay attention to the following points:

1. In Python, it is assigned to a variable by reference to the object instead of by value.

2, the assignment operator is mainly "=", but also can use incremental assignment, such as X+=1. But there is no self-increment, self-decrement operator.

3, in the C language, the assignment statement can be used as an expression (can return a value), but in Python the assignment statement does not return a value, as the following is illegal:

>>> x=1>>> y= (x=x+1) syntaxerror:invalid syntax

4, Python support chain assignment, multiple assignment, multi-value assignment

(1). Chained assignment:

>>> y=x=x+1>>> x, Y (2, 2)

(2). Multiple assignments

>>> x=y=z=1>>> x, Y, Z (1, 1, 1)

An integer object with a value of 1 is created, and the same reference to the object is assigned to x, Y, Z

(3). Multivariate Assignment

What is called multivariate assignment, in the Python core programming book, is the name of the author himself, because both sides of the equals sign are ganso.

>>> x,y,z=1, ' ssss ',true>>> x, Y, Z (1, ' ssss ', True)
X,y,z=1, ' as ', 3 (x, Y, z) =1, ' as ', 3 (x, y, Z) = (1, ' as ', 3) #推荐

Where the Python variable exchange is too elegant, if the values of x and Y are exchanged, in other languages, such as C, we may use a temporary variable to do the work. But in Python, you just have to do this.

>>> x,y=y,x>>> x, y (' ssss ', 1)
Iv. identifiers

First, the name of the variable is the same as other mainstream languages such as C, followed by the Python keyword, identifiers are in the Python document, if necessary, can be browsed.

Five, coding style
1 #coding =utf-8                       #编码格式 2 "3 Created on 2013-3-11                4 @author: Beginman 5" ' 6 "This is a test module"             #模块文档 7 Import OS                           #导入模块 8 import sys 9 s=true                              #全局变量 (GlobalVariable) class Fooclass (object):             #类的定义11     " Fooclass test "                 #类的文档对象12     classattribute=10               #类属性     def myFunction (self):           #函数定义14         " function test "             #函数的文档对象15         dataattribute=" Beginman "    #数据属性 (instance properties)         Pass +                        if __name__== "__main__":            #主程序19     inst=fooclass ()     PASS21     

Here we focus on Python's main program. So what is the main program of Python, that is, whether the module is imported by another module or executed directly as a script, it will execute this part of the code. There is usually not much functional code here, but rather the execution of a pattern call to different functions.

Always remember the fact that all modules have the ability to execute code, the highest-level Python statement, the code without indentation, that executes when the module is imported.

Other:

such as: garbage collection mechanism, so that we focus more on the code.

"Turn" python learning--based on

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.