python first experience - Conditions

1, branch execution

In fact, the computer program is not only the order of execution, also capable of branch execution . What branch execution ?

To understand the branch execution, we can imagine: driving a blue red HS7, driving on the highway, branch the freeway turnoff . If the program does not branch, we have the equivalent of driving a fork in the road is not on the highway. Suppose we want from Beijing to Shanghai, if the fork in the road can not be a choice, but also open to Shanghai?

We can use conditional statements to achieve branch execution. In Python by the if statement to achieve branch execution:

Second, conditions and Boolean

Analyzing conditions are conditional statement == arithmetic result of the operator. == arithmetic result of the operator only two situations, namely true (True) and false (False). The computer used to represent the true and false statements data type called Boolean type.

Boolean value, we generally also called a Boolean value . Conditional statement condition is a Boolean value. Boolean value that only two values, namely True and False .

Tips: to meet the conditions we generally also known as the condition is true , does not satisfy the condition known as the condition is false .

Development: Why indicates the type of true value and false value named Boolean type it? Boolean, English as Boolean, is named after George Boole, he's Queens College Cork, Ireland, British mathematician who first defined an algebraic system of logic in the mid-nineteenth century. Now, Boolean logic has many applications in electronics, computer hardware and software.

Third, multiple branches

Earlier we learned a simple conditional branch, it can be understood as a motorway fork in the road. In reality, the highway can be more than just fork in the road, the four fork also abound.

Python be achieved by adding multiple branches elif branches:

Four, if / else a triplet of expressions

Now we've written a lot of binary expressions, such as "programming" + "Chen" is the binary expressions (has two operands), which + is also known as binary operators.

A triplet of expressions is relatively rare, then we learn first triplet expression - IF / the else .

small assignment is if and else divided into three parts, wherein the second portion is the condition when the condition is satisfied, the return value of the first portion, otherwise the return value of the third portion.

 

Guess you like

Origin www.cnblogs.com/free-1124/p/11332190.html