Python entry notes (2): BASICS (I)

Source: Internet
Author: User
1. to practice this skill, you must first learn from your own palace

From now on, I have really started to contact her. Maybe many people, like me, don't know how to quickly master a new programming language. Today I am posting some suggestions for you to take a look, this is also a very important thing before learning. Although it is too exaggerated to "exercise this skill first, you must be prepared before learning. Since Python is used at work, I started to learn and put it into practical projects one month before work. After a month of study, I will repeat it today. My preparations are as follows:

1. Prepare the tool

This has been completed in the last chapter of study notes.

2. Data Preparation

Python core programming
Python official documentation
Blogs, CSDN, and other communities

3. Getting started with tutorials

Complete the Python entry notes about march.

4. psychological preparation

About how to learn a new programming language, read here: http://www.cnblogs.com/chgaowei/archive/2011/05/29/2061902.html

5. Preparations for splitting the knowledge framework

By splitting the framework of the entire Python learning process and using the book "Python core programming", almost all Python knowledge points are covered, but for beginners like me, I still cannot master her entire learning framework and process. If we read this book roughly, we can break down her framework and divide it into modules so that we can be aware of it. The following is my splitting framework: (because we first learn from the basics of Python, we will not consider the advanced section here)

[1] Python basics:

(1 ). syntax basics, (2), data type, (3 ). python object, (4 ). string, ancestor, and list, (5 ). ing and set, (6 ). function

[2] Process Control

Conditions and loops

[3] File Operations

[4] modules

[5]. Object-oriented

[6] Exception Handling

[7] environment deployment

 

Well, all these preparations are in place. Now let's start learning. If you're okay, we suggest you look at the Python philosophy.

Ii. Python syntax

I don't know how I used to write the first Python program. It seems that I wrote it in Dive Into Python, a tough tutorial. As always, let's start with "helloworld". Haha, many of the languages we have learned start with helloworld.

>>> 1 + 1               2>>> print 'hello world' hello world>>> x = 1               >>> y = 2>>> x + y3

The example here does not explain any syntax, but it can make us familiar with it, for example,Python is both a dynamic Type Definition Language (because it does not use the display data type declaration) and a strong Type Definition Language (because once a variable has a data type, it actually remains of this type), a program does not end with a semicolon, print can output a string, and so on. Next, let's explain the python syntax in detail.

1. Notes

Similar to many Unix scripts, the Python comment statement starts with the # character, for example:

# print "hello world"

Note that Python # character comments can only implement single-line comments. Maybe many people are just as curious as me. If you want to comment out more than one hundred lines of code, do you need to repeat it for more than one hundred times ?? Without a doubt, if we do this, we can only prove that we are the one who hired to help with soy sauce.

We can have the following tips:

(1). Procedural Control Method:

Without affecting the entire program, we can use the if clause to indirectly comment out a lot of code segments to be commented out, that is, if shields the execution of the code block to be commented out, you can use if False:, if None:, if 0: to shield the code, as shown below:

1 In eclipse: 2 print 'beginman' # output 3 if False: # block, if, then, all boolean values such as 0 and None are false. The condition 4 print 'Hello world' 5 print 'Good man '6 print 'ssss' 7 print 'OK! '# Output

Then the code under if will not be executed and no output will be made. However, it is best not to use this technique. There may be latent exceptions when many codes exist.

(2) Editor skills

If you select multiple lines under eclipse, you can use the shortcut key ctrl +/to comment out or cancel the comment. This is strongly recommended. For the editor, it is best to master common shortcut keys to improve development efficiency.

(3 ).Doc string(Document string)

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

Double quotation marks (including single quotation marks and double quotation marks) indicate a multi-line string. Everything between start and end quotation marks is considered part of a single string, including hard carriage return and other quotation marks. Anything in double quotes is the function.Doc stringThey are used to illustrate what functions can do. If yesDoc stringIt must be the first content to be defined by a function (that is, the first content after the colon ). Technically, functions are not required.Doc string, But this should be done. Python brings some extra motives:Doc stringIt can be used as a function attribute at runtime.

2. line feed (\ n)

Common line breaks are often seen in other programming languages.

 3. backslash (\) continues the previous line

That is to say, for a row of a statement, for some too long statements, you can use the backslash \ to break them into multiple rows, such:

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

There are two exceptions: parentheses (), brackets [], braces {}, and three quotation marks. Anti-slashes are not recommended for readability.

4. Code indent

Python functions are not obvious.BeginAndEnd, No curly brackets, used for the start and end of the scalar function. The only separator is a colon (:), And the Code itself is indented. For example, refer to the above. Note that the Code indentation is very strict. If you do not follow the rules, you may encounter syntax errors, such as unexpected indent. Sometimes a logical error occurs.

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

5. Use a semicolon (;) to write multiple statements in the same line.

Such as; import OS; a = 123; print

Note that do not include at the end of the sentence; it is recommended not to write like this because it will greatly reduce the readability of the Code.

6. Modules

Each python script is regarded as a module and stored as a disk. It can be split into multiple modules multiple times. Import data using import.

Iii. Python Variables

Note that, unlike C #, Java, C/C ++ and other programming languages, Python variables have no type, and their type is determined by the object in the memory. Because Python functions are involved, we will not talk about them in this section. This section describes how to assign values to variables.

Python is of a weak type. You do not need to specify the variable type explicitly. The type is automatically declared when values are assigned.. For example:

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 10 Car 1.8 [1, 2, 3] ('aa', 'ss')

Note the following:

1. in Python, values are assigned to variables through object references rather than values.

2. The value assignment operator is mainly "=", and incremental value assignment can also be used, for example, x + = 1. However, there are no auto-increment or auto-subtraction operators.

3. In C, the value assignment statement can be used as an expression (which can return values), but in Python, the value assignment statement does not return values. The following statements are invalid:

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

4. Python supports chained assignment, multi-assignment, and multivariate assignment.

(1). chained assignment:

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

(2). multi-value assignment

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

An integer object with a value of 1 is created, and the same reference of this object is assigned to x, y, z.

(3). Multivariate assignment

Why is multivariate assignment? In the core Python programming book, it is the author's own name, because the objects on both sides of the equal sign are the ancestor.

>>> 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) # recommended

Here, the Python variable exchange is too elegant. If we exchange the values of x and y, we may use a temporary variable in other languages, such as C. But in Python, you only need to do this.

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

First, the naming rules of variables are the same as those of other mainstream languages such as C. Second, Python keywords and identifiers are available in Python documentation. You can refer to them if necessary.

V. Encoding Style
1 # coding = UTF-8 # encoding format 2''' 3 Created on 2013-3-11 4 @ author: beginMan 5 ''' 6 "this is a test module" # module documentation 7 import OS # import module 8 import sys 9 s = True # global variable) 10 class FooClass (object): # class definition 11 "fooclass test" # class Document object 12 classAttribute = 10 # class attribute 13 def myFunction (self ): # function Definition 14 "function test" # function Document Object 15 dataAttribute = "BeginMan" # data attributes (instance attributes) 16 pass 17 18 if _ name __= = "_ main _": # main program 19 inst = FooClass () 20 pass21

Here we focus on the main Python program. So what is the main Python program? That is to say, this part of code will be executed no matter whether this module is imported by another module or directly executed as a script. Generally, there is not much functional code here, but different functions are called in execution mode.

 

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.