-->

Flow Control [Python]

So you know the basics of individual instructions and that a program is just a series of instructions. But the real strength of programming isn’t just running (or executing) one instruction after another like a weekend errand list. Based on how the expressions evaluate, the program can decide to skip instructions, repeat them, or choose one of several instructions to run. In fact, you almost never want your programs to start from the first line of code and simply execute every line, straight to the end. Flow control statements can decide which Python instructions to execute under which conditions.

Flow Control [Python]
Flow Control [Python]


These flow control statements directly correspond to the symbols in a flowchart, so I’ll provide flowchart versions of the code discussed in this chapter. The Figure below will shows a flowchart for what to do if it’s raining. Follow the path made by the arrows from Start to End.

A flowchart to tell you what to do if it is raining
A flowchart to tell you what to do if it is raining

In a flowchart, there is usually more than one way to go from the start to the end. The same is true for lines of code in a computer program. Flowcharts represent these branching points with diamonds, while the other steps are represented with rectangles. The starting and ending steps are represented with rounded rectangles.

But before you learn about flow control statements, you first need to learn how to represent those yes and no options, and you need to understand how to write those branching points as Python code. To that end, let’s explore Boolean values, comparison operators, and Boolean operators.

Boolean Values

While the integer, floating-point, and string data types have an unlimited number of possible values, the Boolean data type has only two values: True and False. (Boolean is capitalized because the data type is named after mathematician George Boole.) When typed as Python code, the Boolean values True and False lack the quotes you place around strings, and they always start with a capital T or F, with the rest of the word in lowercase.

Enter the following into the interactive shell. (Some of these instructions are intentionally incorrect, and they’ll cause error messages to appear.)

  1. 1
  2. >>> spam = True
  3. >>> spam
  4. True

  5. 2
  6. >>> true
  7. Traceback (most recent call last):
  8. File "<pyshell#2>", line 1, in <module>
  9. true
  10. NameError: name 'true' is not defined

  11. 3
  12. >>> True = 2 + 2
  13. SyntaxError: assignment to keyword



Like any other value, Boolean values are used in expressions and can be stored in variables u. If you don’t use the proper case v or you try to use True and False for variable names w, Python will give you an error message.


Comparison Operators

Comparison operators compare two values and evaluate down to a single Boolean value. Table 2-1 lists the comparison operators.

Operator Meaning

==  : Equal to
!=   : Not equal to
<    : Less than
>    : Greater than
<=  : Less than or equal to
>=  : Greater than or equal to

These operators evaluate to True or False depending on the values you give them. Let’s try some operators now, starting with == and !=.

  1. >>> 42 == 42
  2. True
  3. >>> 42 == 99
  4. False
  5. >>> 2 != 3
  6. True
  7. >>> 2 != 2
  8. False

As you might expect, == (equal to) evaluates to True when the values on both sides are the same, and != (not equal to) evaluates to True when the two values are different. The == and != operators can actually work with values of any data type.

  1. >>> 'hello' == 'hello'
  2. True
  3. >>> 'hello' == 'Hello'
  4. False
  5. >>> 'dog' != 'cat'
  6. True
  7. >>> True == True
  8. True
  9. >>> True != False
  10. True
  11. >>> 42 == 42.0
  12. True
  13. >>> 42 == '42'
  14. False

Note that an integer or floating-point value will always be unequal to a string value. The expression 42 == '42' u evaluates to False because Python considers the integer 42 to be different from the string '42'. The <, >, <=, and >= operators, on the other hand, work properly only with integer and floating-point values.

  1. >>> 42 < 100
  2. True
  3. >>> 42 > 100
  4. False
  5. >>> 42 < 42
  6. False
  7. >>> eggCount = 42
  8. u >>> eggCount <= 42
  9. True
  10. >>> myAge = 29
  11. v >>> myAge >= 10
  12. True

The Difference Between the == and = Operators

You might have noticed that the == operator (equal to) has two equal signs, while the = operator (assignment) has just one equal sign. It’s easy to confuse these two operators with each other. Just remember these points:

• The == operator (equal to) asks whether two values are the same as each other.
• The = operator (assignment) puts the value on the right into the variable on the left.

To help remember which is which, notice that the == operator (equal to) consists of two characters, just like the != operator (not equal to) consists of two characters.

You’ll often use comparison operators to compare a variable’s value to some other value, like in the eggCount <= 42 u and myAge >= 10 v examples. (After all, instead of typing 'dog' != 'cat' in your code, you could have just typed True.) You’ll see more examples of this later when you learn about flow control statements.

0 Response to "Flow Control [Python]"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel