Python Language Part-2
Flow Control
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).
>>> spam = True
>>> spam
True
>>> true
Traceback (most recent call last):
File "", line 1, in
NameError: name 'true' is not defined
w
Comparison Operators
The not Operator
True
Elements of Flow Control
Flow control statements often start with a part called the condition, and all
are followed by a block of code called the clause. Before you learn about
Python’s specific flow control statements, I’ll cover what a condition and a
block are.
Conditions
The Boolean expressions you’ve seen so far could all be considered conditions, which are the same thing as expressions; condition is just a more
specific name in the context of flow control statements. Conditions always
evaluate down to a Boolean value, True or False. A flow control statement
decides what to do based on whether its condition is True or False, and
almost every flow control statement uses a condition.
Blocks of Code
Lines of Python code can be grouped together in blocks. You can tell when a
block begins and ends from the indentation of the lines of code. There are
three rules for blocks.
1. Blocks begin when the indentation increases.
2. Blocks can contain other blocks.
3. Blocks end when the indentation decreases to zero or to a containing
block’s indentation.
Program Execution
In the previous chapter’s hello.py program, Python started executing
instructions at the top of the program going down, one after another. The
program execution (or simply, execution) is a term for the current instruction
being executed. If you print the source code on paper and put your finger
on each line as it is executed, you can think of your finger as the program
execution.
Not all programs execute by simply going straight down, however. If you
use your finger to trace through a program with flow control statements,
you’ll likely find yourself jumping around the source code based on conditions, and you’ll probably skip entire clauses.
Comments
Post a Comment