"2" Quick start learning python overall

Source: Internet
Author: User
Tags naming convention

Look at that.

>>> main prompt

... Second prompt

Python solves the problem in two main ways: statements and expressions (functions, arithmetic expressions);

ABS () function, output absolute value function.

The absolute value of English is absolute value, take the front abs, good to remember points.

ABS (-4)

4

ABS (4)

4

What we need now, of course, is to learn the print statement first.

Note the output redirection for Python.

Python's parentheses are all functions.

Learn to use the Help () function in Python

Help (name of function)

Core style: Always do user interaction outside the function

The functions are divided into two categories, one to do things and no return values. A class performs some calculations and does not require a return value.

Python comment symbol is #

Also note the Python document string.

NET text excerpt:

Http://c.biancheng.net/cpp/html/1833.html

Python has a good feature called a document string (documentation strings) , which is usually specified with the abbreviated name Docstrings. The document string is an important tool that you should use, which helps to make it easy to understand the program documentation. Surprisingly, when the program actually runs, we can even return a document string from a function like this.

Examples of document strings:

def printmax (x, y):
"Prints the maximum value in two digits.
The two value must be an integer. ‘‘‘
x = Int (x) # if possible, convert to an integer
y = Int (y)

If x > Y:
Print (x, ' Max ')
Else
Print (Y, ' Max ')

Printmax (3, 5)
Print (printmax.__doc__)

Output:
5 Maximum
Prints the maximum value in two numbers.
The two value must be an integer.

How it works:
The string of the first logical line of a function is the document string for that function. Note that the document strings also apply to the modules and (classes) (#面向对象的程序设计) that will be learned in the respective chapters.

The document's example is a multiline string, and the first line begins with a capital letter with a period (.). End (Note: Chinese is also available in V3.3), the second line is a blank line, and the third line starts with a detailed description. It is strongly recommended that you write a document string for your important function to follow this example.

We can use the __doc__ (note, double underscore) property of the function (which belongs to the name) to access the document string of the Printmax function. Just remember that Python treats everything as an object, including functions. We will learn more about objects in this chapter of the class.

If you've already used help () in Python, you've seen how to use the document string! All it does is get the __doc__ property of the function and show it to you in a neat way. You can try it on the function above-just include help (Printmax) in your program. Remember to press the Q key to exit help.

Automation tools can retrieve documents in this way from your program. Therefore, I strongly recommend that you use a document string for any important functions that you write. Automation tools from Python the Pydoc command works similarly to help () using a document string.

Learn to write document strings for important functions

The format should be like this.
"Prints the maximum value in two digits.
The two value must be an integer. ‘‘‘

With 3 single apostrophes in the logical first line of the function, which is the line below Def, the end of the 3 single apostrophes is written.
The next time you print a document string, you can pass the print function:
Method such as print (function name. __doc__) 〉〉〉 Note this place's underscore is double-underlined.


But this is the case in the Python core programming book:
Def fooo ():
"This is a doc string."
return True

The difference here is that a double quote is used. So it is estimated that as long as the first line of single quotation marks or double quotation marks, back to the end of the document string, but if the contents of the document string is also quoted, what should be said that Python is not a line to explain it? Then it might be a good result to say 3 single apostrophes. Continue to find methods.

The following to the operator. A single-mesh operator and a binocular operator are divided.

The monocular operator refers to an operator with only one operand, such as: take positive (+), take negative (-), inverse (^), or (|), and (&), and so on;
Binocular operators are operands of two, such as: plus (+) minus (-) multiplication (*) except (/), self-addition (+ +), self-subtraction (-), Logic and (| | ), logic or (&&), redundancy (%), assignment (=), etc.;
See how many of the operands are called a few operands. Monocular operation is higher than binocular operator

The Python logical operator.

And Or not

>>> 3<4<5== (3<4 and 4<5)
False
>>> (3<4<5) = = (3<4 and 4<5)
True

This needs to be noticed, because this place is two different, and when you put a parenthesis in it, it becomes the same. So we need to look at the precedence of the arithmetic operators of Python.

Core style: Reasonable use of parentheses, reasonable use of parentheses can improve the readability of code, maintenance of the code will be a lot of benefits.

The variable name of the Python variable is case-sensitive, so pay attention to naming the variable name.

About naming conventions, don't know what to do with reference to the Python naming convention

Python supports incremental assignment is n=n*3 equivalent to n*=3

Python does not support self-decrement assignments, which means ++i, which is equivalent to two single-mesh operators. No difference.

If there are a lot of characters in the string that need to be escaped, you need to add a lot \ , in order to simplify, Python also allows to use r‘‘ ‘‘ A string that represents the inside of the default is not escaped, you can try it yourself:

>>> print ‘\\\t\\‘\       >>> print r‘\\\t\\‘\\\t\

If there is a lot of line wrapping inside the string, \n writing in a row is not good to read, in order to simplify, Python allows ‘‘‘...‘‘‘ a format to represent multiple lines of content, you can try it yourself:

>>> print ‘‘‘line1... line2... line3‘‘‘line1line2line3

The above is entered in the interactive command line, if written as a program, that is:

print ‘‘‘line1line2line3‘‘‘

Multi-line strings ‘‘‘...‘‘‘ can also be used in front of each other r , please test yourself.

"2" Quick start learning python overall

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.