Getting started with Python: programming habits and features; getting started with python

Source: Internet
Author: User

Getting started with Python: programming habits and features; getting started with python

1. Code style

In Python, each line of program ends with a line break. If a line of program is too long, you can use the "\" symbol to extend it to the next line. Strings, lists, tuples, and dictionaries enclosed in three quotes (") in python can be used across rows. The Code enclosed by parentheses (...), brackets [...], and braces {...} can be expanded to multiple rows without the "\" character.

In Python, program function blocks are distinguished by indentation. The length of indentation is unlimited, but it is best to maintain consistent indentation for a function block.

If a row contains multiple statements, the statements must be separated by semicolons.

The content starting with "#" is annotated. The python interpreter ignores the content of this line.

In python, all identifiers can contain English letters, numbers, and underscores (_), but cannot start with a number. The identifiers in python are case sensitive.

The identifier starting with the following line is of special significance. A class attribute that starts with a single underscore (_ foo) represents a class attribute that cannot be accessed directly. It must be accessed through the interface provided by the class. It cannot be imported using "from xxx import; (_ foo) starting with a double underline represents a private member of the class; (_ foo _) Starting with and ending with a double underline represents a special identifier for special methods in python, for example, _ init _ () indicates the constructor of the class.

When running python in interactive mode, an underscore (_) is a special identifier, which retains the last computation result of the expression.

Copy codeThe Code is as follows:
>>> "Hello"
'Hello'
>>> _
'Hello'
>>> 10 + 10
20
>>> _
20

In python, if the first piece of code defined by functions, classes, and modules is a string, it is called a file string and can be accessed through the _ doc _ attribute. For example:

Copy codeThe Code is as follows:
Def test ():
"This is a document string"

Return 100 + 1000

>>> Print test. _ doc __
This is a document string

2. Reserved Words

Copy codeThe Code is as follows:
And elif global or yield
Assert else if pass
Break failed t import print
Class exec in raise
Continue finally is return
Def for lambda try
Del from not while

3. Python operators and expressions

Python operator list

Operator priority list (from highest to lowest)

Truth Table

Compound expression

For and, when a and B is calculated, python calculates a. If a is false, a is taken. If a is true, then python calculates B and the entire expression obtains B value. For example:

Copy codeThe Code is as follows:
>>> A, B = 10, 20
>>> A and B # a is true
20
>>> A, B = 0, 5 # a is false
>>> A and B
0

For or, when a or B is calculated, python calculates a. if a is true, the entire expression obtains the value of a. If a is false, the expression returns the value of B. For example:

Copy codeThe Code is as follows:
>>> A, B = 10, 20
>>> A or B
10
>>> A, B = 0, 5
>>> A or B
5

For not, not will reverse the "actual value" of the table expression. If the expression is true, not is false. For example, if the expression is false, not is true. For example:

Copy codeThe Code is as follows:
>>> Not 2
False
>>> Not 0
True
>>> Not "test"
False
>>> Not ""
True

4. assign values to variables

Variable assignment: A value is not directly assigned to a variable. in Python, objects are passed through reference. The value assignment statement of Python does not return values. The following statements are invalid:

Copy codeThe Code is as follows:
>>> X = 1
>>> Y = (x = x + 1)
SyntaxError: invalid syntax

No problem with chain assignment:

Copy codeThe Code is as follows:
>>> X = 1
>>> Y = x + 1
>>> X, y
(2, 2)

Python does not support pre-and post-auto-increment/auto-increment operations like x ++ or -- x.

Repeated values:

Copy codeThe Code is as follows:
>>> X = y = z = 1
>>> X
1
>>> Y
1
>>> Z
1

You can assign multiple values:

Copy codeThe Code is as follows:
>>> X, y, z = 1, 2, 'a string'
>>> X
1
>>> Y
2
>>> Z
'A string'

Usually enclosed in parentheses to enhance code readability:

Copy codeThe Code is as follows:
>>> (X, y, z) = (1, 2, 'a string ')
>>> X
1
>>> Y
2
>>> Z
'A string'

Using the multivariate value Assignment Method in Python, You can exchange values of two variables without intermediate variables:

Copy codeThe Code is as follows:
>>># Swapping variables in Python
>>> X, y = 1, 2
>>> X
1
>>> Y
2
>>> X, y = y, x
>>> X
2
>>> Y
1


Python features

Python is easy to use and easy to learn. Reading its code is like reading an article. People with a little logical thinking can see what it means in just a few minutes.
Python is a good start to learn programming from scratch.

Is there anyone programming with python?

In the face of the eye-catching Pearl (Perl) light, the spirit snake (Python) is more powerful than ever before. It can run on a variety of OS such as MS-DOS, Amiga, BeOS, OS/2, VMS, QNX, etc. It is faster and better than Perl and VB, and it is more scalable than C and C ++; not only do they provide special effects and animation in super movies such as Star Wars and Star Trek, but they also break into the control room of the Space Administration of the United States. For details about the truth, see the report.

For the Perl (PEARL) language, Internet developers can be said to have no one knows, no one knows; it runs fast, easy to operate, and can make users feel happy when using it. However, when it is not satisfactory: Its openness brings about its complexity. When it is used to deal with large projects, it is especially difficult to start.

Despite this, most Internet projects still need to be built by the Perl "hero.

But there is no choice. The latest recommended software is Python (spirit snake), which has a history of only 1/4 of Perl and has many similar features.

The name of Python comes from the famous funny Monty Python, which has many well-known actors and creates a screen image that is well known to all.

So what is the image of Python in the Internet world?

Faster and better Python

Like Perl, Python is also an interpreted language. It adopts an object-oriented approach and its language structure is between C and Perl. Although it is regarded as a very high language, its built-in interpreter functions are very basic, and only a few functions, operators and keywords constitute the entire programming language. Its advantages are not only that, but also that it can be expanded through a series of modules. It is faster and better than Perl and VB statements, and more scalable than C and C ++ languages.

Similarly, Python first converts program statements into a series of byte code through an interpretation and compilation system, which is then processed by the Python virtual machine. Python is designed very effectively, mainly because of its large function library. Therefore, users do not need to enter the system environment or operating system, nor reference modules. Using Python programming, small programs are still small programs, and there is no need to spend too much time and cost.

In addition to its Core platform, Python can run on MS-DOS, Amiga, BeOS, OS/2, VMS, QNX, and many other operating systems. You can even run Python programs on your handheld computer.
In the face of the eye-catching Pearl (Perl) light, the spirit snake (Python) is more powerful than ever before.

In addition to its Core platform, Python can run on MS-DOS, Amiga, BeOS, OS/2, VMS, QNX, and many other operating systems. You can even run Python programs on your handheld computer.

Python also has the following notable features:

Strong adaptability

Python can be supported by a series of platforms. It is compiled in a complete format that can run on Windows, MacOS, and its own Tk expansion platform, and can be used to develop user-friendly interfaces. On UNIX and other platforms, Python can be compiled by programmers as source code, or used together with other pre-compiled binary codes. Its strong adaptability makes it more suitable for cross-platform applications: it can be used on Mac and PC machines without any modification when it writes program segment 95% on UNIX platforms. Expand the support of Tk platform, and even allow GUI-based applications to switch freely on the above three platforms without the need for great flexibility, while maintaining a unified interface.

Because of this inherent cross-platform running capability, Python can also support expansion platforms of some platforms, thus simplifying and eliminating the need to adapt to other languages and environments. Sun audio device and SGI version with SunOS/Solaris tool with audio and video interfaces (including... full text>

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.