Python branching, looping, conditional statement usage

Source: Internet
Author: User
Tags in python

Python branching, looping, conditional statement usage

I've learned two types of data, 1 are numbers, and 1 are strings. Numbers can be mathematically calculated, and strings can be

Text information. Where the number is divided into, integers, floating-point numbers. strings, of course, are strings. integers, floating point numbers are

Is the data type that describes the number. And then, to do something meaningful, you need to put it all together

Some of the processing (is the expression). Also note that the Python interpreter did a data type check before making an explanation.

, such as numbers and strings cannot be added, and if you want to add it must be converted to a string with a function such as STR (). But

is why the Python interpreter does not default to the number as string processing by default! The reason is this,

After the program is written big in the event of a bad debugging! Hey! debugging, I used today, debugging very good ah! It's like losing a

Things to find things, according to the original road step-by-step to see where things have been lost (this analogy does not know appropriate).

But there's a problem here, since Python does type checking, the following code I write will definitely do the wrong thing.

!!! :

>>> "3" < 3
False

Results can be executed correctly, depressed. How can python do that! What does python do with this result?

。 I looked at the relevant data and understood that it was a comparison of the Azkar code value of "3" and 3, since the number was in the post of the letter

, so Python has output this result! Collapse! Alas! Now that I think of Python, this little boa constrictor suddenly doesn't

's Cute! Whatever it is!

Also, when it comes to complex expressions, it's best to use parentheses, because it's easy to see who and who wants to combine

Thinking about the variables. I now feel that the variable should not be thought of as a box Ah! Because:

>>> x = 1 + 2
>>> y = 2 + 3
>>> z = x
>>> Z
3
>>> x
3


Should want to become a kite more suitable point ah! The variable is the kite-flying man! Through this man can find it put the "wind

Zheng "! Then the 1-value kite can have two lines. You can put a kite with two people. Then the kite-flying man can also put

Their own line cut off and then connected to someone else's kite! Well, I think that's the right thing to understand. And the type of kite that determines

The type of the variable, as long as the type of kite changes, the type of variable will change! Like what:

>>> x = 7
>>> x = "haha"
>>> x
' Haha '


Alas! Variable, so it's changed! Well! This feeling is not very good ah! Or not to change, to be forgotten

Just remember, you're done!

Two: branches, conditions, loops

Now, when I look back to the first chapter, the code I wrote is really good enough to stay. Like playing CS games, a lot of fools know

Road a straight forward, do not know how to choose the enemy less places to go, is really stupid head! And then, Python is still very

Smart, for example:

x = 3
y = raw_input ("y:")
If x < y:
print ' haha '

Else:print ' Heihei '
‘’‘’‘’‘’‘’‘’‘’‘
Y:5
haha


When talking about branching and looping, pay special attention to the forced indentation in the Python code.

Let's look at the branches first:

(1) Simple If-else
Python code
1. A = ' 1 '
2. If a = = 1: #注意后面有一个冒号. where "= =" is the Equal judgment
3. Print 1 #注意print function is preceded by a TAB key, which is the forced indent of Python
4. Else: #注意else后面的冒号
5. Print 0 #注意缩进
6.
7.
8. if (a = = 1): #可以添加园括号
9. Print 1
. else:
One. Print 0

The output is:
1
1

(2) and logical judgment
Python code
1. A = 1
2. B = 0
3. If a = = 1 and b = 1: #and is a logical "and" operation, natural "or" is the logical "or" operation


4. Print 1
5. Else:
6. Print 0

The output is:
0

(3) Branch If-else if

A closer Look:
Python code

1. #else If
2. A = 1
3. B = 0
4. If a < 1:
5. Print 1
6. Elif B < 1: #注意这里不是else if, but elif.
7. Print 0

The output is:
0

The above three have finished the branch judgment. Let's talk about loops.

(a) Start is for loop:

Its for loop is essentially an element's traversal:

Such as:
Python code

1. For I in range (0, 5): #注意range是一个函数
2. Print I
3.
4. The output is:
5.0
6.1
7.2
8.3
9.4

The output is:
0
1
2
3
4

Where range is a function that represents a sequence that produces a [0,5]. The mathematical expression "[0,5]" is used here

is to indicate that it is greater than or equal to 0, less than 5. is a half open and half closed interval. Note that in Python, half open and half closed are used.

Interval (I have not seen any other form, perhaps I can achieve it myself).

and "For I in range (0, 5):" Meaning is from the "0,1,2,3,4" in this sequence, each fetch a

The element assigns a variable i, and every time the print function is executed, the value of the element i is printed.

There is a colon at the end of the if and for statements to tell the compiler that the current line is finished and should be interpreted after

Side of the line.

With this colon, we can actually execute the print function without wrapping the line.

Python code
1. For I in range (0, 5):p rint I

(ii) While loop
While loop, when the while condition is established, executes the program segment within the while.
Python code
1. i = 10
2. While i > 0:
3. Print I
4. I-= 1

#注意python不支持i--, i++,--i,++i operations.

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.