Python and Python

Source: Internet
Author: User

Python and Python
Python Removal

Various changes are used to show the general expression. There are a total of three hundred and sixty changes.

Used to crackJianfa. Although the "sword-breaking" is only one style, the main elements of each sword-breaking method in the world are well-formed. Although "no moves" are based on the tactics of the whole world, as a result, the change is far less than the overall decision.

From Jin Yong Swordsman

The first two chapters introduce the basic syntax format of python-code block usage and a brief introduction to python.

This chapter describes the syntax of the control statement in python.

In any computer language, you can learn how to declare and use variables and how to use control statements. To achieve these two points, even getting started. The declaration of Python variables does not need to be mastered. That is to say, you can now understand how to use control statements.

There are only three control statements:

Loop (while for), select (if-elise), turn (break, continiue)

In fact, all the program logic we use is composed of the above three statements. These three statements constitute various programs. Is it a bit like the general expression of the solitary sword-various changes, used to show the general expression. There are a total of three hundred and sixty changes.

1 Condition Statement 1.1 Simple Condition Statement

If <expression>:

If_suite # code block in the if statement

1.2 if-else Structure

If <expression>:

If_suite #

Else:

Else_suite ##

1.3 if-elseif Statement (multi-branch judgment)

If <expression>:

If-suite

Elif <expression>:

Elif -- suite

Else:

Else_suite

1.4 chicken ribs

When you look at the condition statement, you may ask: does python have a three-object operator? I have always thought that the three-eye operation is a weakness. Java and c ++ are available, but few will be used.

Python has no three goals for a long time. Guido Van rosum himself refused to add such a feature because he believed that the Code should be kept simple, making it difficult for programmers to make mistakes. But many years later, he found that he was wrong, mainly because in the past few years, people tried to use and or to simulate the Three-object operator, and most of them were wrong. Therefore, he provided a reasonable solution:

C? X: Y is the expression of a ternary operator. In python, it should be written:

If C:

X

Else:

Y

The answer is wrong.

---- "Guido Van rosum I think the correct one should be:

X if C else Y ### is PS quite strange;

Demo:

When comparing the size:

Res = x if x <y else y

Print res

2. Loop statement 2.1 while LOOP

The while statement is basically the same as the while statement.

While <exp>:

While-suite

There is another thing like C ++: Wireless Loop

2.2 for Loop

In python, The for loop is not the same as the traditional c and C ++ for loop (counter loop). It is more like the for-each iteration loop in shell scripts or js. (A bit like java's enhanced for loop)

In python, for accepts iteratable objects as parameters, and one element of each iteration.

For provides the most powerful loop structure in python. It can traverse the members of the sequence and can be used in parsing and generator expressions. It will automatically call the next () function of the iterator. This is the same as java's enhanced for loop.

For example:

Written in java as follows:

In python:

Running effect:

2.2.1 for usage:

This section describes how to use:

1.Used for iteration of sequence type

Note the following:

The len () function is used to obtain the size of the set.

Range () is used for iteration.

2.Used for iterator type

3.PassRange() Function Iteration

Range () provides two different method class calls. The complete syntax is:
Range (start, end, step = 1)

Step indicates the step, start indicates the start, and end indicates the end.

Range (end)

Details about xrange and range

Xrange is similar to range, but xrange is more suitable when you have a large range of lists. Because Xrange does not create a complete copy of the list in the memory, its performance is much higher than range, and xragne returns an iteration object.

The two are used in the same way.

4.AboutForSummary

Here we hope you can take a look at the xrange () and range () functions ..... I will not go into details.

Break, continue, pass

In python, break functions like C and java: used to jump out of the current loop.

For example:

Continiue is used to terminate the loop and ignore the statement used by continiue, and then return to the top of the loop.

For example:

Pass statement:

Because python does not use the traditional {} class to mark the code quickly, it is required to have code in syntax in some places, while python does not have a pair of null code or a; number, if you leave the code block empty and do not write any statements, the python interpreter will prompt you with a syntax error. This is embarrassing. My God, why didn't I have {} And;? How can I write an empty statement ???

It doesn't matter. The python designer has already figured out the countermeasure for you: using the pass statement, he does not do anything --- NOP (No OPeration ). At the same time, pass can also be used as a developer tips to mark the code you will complete later.

For example:

Let's look at else.

In python, else can be used not only with if, but also with while.

While expression:

Statement Block

Else:

Else statement Block

Let's look at the iterator.

The iterator is added when python2.2. It provides a class sequence interface for class sequence objects. In fact, if we have learned the design pattern, we should know: iterator pattern.

The design pattern is explained as follows:

The Iterator mode provides a method to access various elements in an aggregate object sequentially without exposing the internal representation of the object.

When you need to access an aggregate object and traverse all these objects, you should consider using the iterator mode. In addition, you can consider using the iterator mode when multiple aggregation methods are required. The iterator mode provides unified interfaces for Traversing different clustering structures, such as start, next, end, and current.

In python, the iter (obj) function returns an iteration object. Use the next () method for iteration.

For example:

In this case, an exception occurs:

In python, you should do this:

Here, the while + iterate form is equivalent to the for loop. This is also true for-eache in java.

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.