Вы находитесь на странице: 1из 4

First let me show you what kind of diagram called a flow chart.

In a flow chart you


start at the start box

and then you follow the arrows to the


other boxes until finally

you get to the end box. You take a


different path depending on different conditions

A flow chart is good metaphor for how


the execution can move around your program.

For example, this flow chart shows you a


decision making process

for what to do if it's raining. You can put


your finger on the start,

then depending on answers to these yes/no questions can move around until finally
you get the end box.

Instructions called flow control


statements

can decide which Python instructions to execute under which conditions.

But before you learn about flow control


statements, you first need to learn about

how to represent those yes and no


options.

This involves three things: boolean


values, comparison operators, and Boolean operators.

Remember, we've already seen values and


operators. Here we're just introducing

some new ones, but it's the same concepts


as before. The boolean datatype hasonly two values: True and False.

Integers and strings effectively having an infinite number of different values.

You can just keep making them longer and larger and larger.

But the boolean datatype only has these two values:True and False.

Like any other value boolean values are used in expressions and can be stored in
variables.

Just be sure to type them with a capital


T or capital F and the rest of the word in lower case.

Next, let's look at comparison operators.


Comparison operators are used in

expressions just like the plus operator or


any other operator.
There six comparison operators: equal to, not equal to, less than, greater than,
less than or equal to, and greater than or equal to.

Expressions with comparison operators


evaluate to a boolean value.

Let's experiment a bit in the interactive shell.

42 is equal to 42.

This is an expression that evaluates


down to a boolean value True. Because it is. You know, 42 is the same as 42.

However, 42 is equal to 'Hello', that'll be False. Or 42 == 41.

That's also False. You know can also check with the "not equal to" operator.

2 is not equal to 3, that's True.

And then also the less than or equal


then operators

42 < 100, that's True. 22 >= 100, that's not True.

42 < 42, well that's False.

But 42

2 + 2 math problems we did before,


that means we can

make them as complicated as we want. we can use variables say, myAge equals 30...
uh...

26 and then myAge < 30 is an expression


that evaluates to True.

One thing they should notice in


Python is that integers and strings will

always *not* be equal to each other. If you have the string '42' and the integer
42, those aren't going to be same.

So even if the string has a number in it,


it's still a completely different thing

from any integer value. However, float


values an integer value can be equal to each other.

You might have noticed that the "equal to" operator has choose equal signs and it
will be variable assignment operator just has

one equal sign. It's really easy to get


these two confused

so here's a good trick just remember


which is which. You can always think of

the comparison "is equal to" operator as being two characters just like how the
"is not equal to" operator is two
characters: the explanation point

and the equal sign. There are also three


Boolean operators:and, or, and not.

You might have used them in search engines. Let's do some experiments in the
interactive shell with the

"and" Boolean operator. The "and" operator evaluates an expression

to True if both boolean values are True.


Otherwise it evaluates to False.

So something like "True and True" will evaluate to True, but it one or both but
these are False,

then enter expression evaluates to


False.

There's a concept called truth tables that numerate every possible combination.

of these two values: True and True evaluates to True and otherwise it's False.

The or operator will evaluate to true if either or both of the values are True. As
long as it's not

"False or False" it'll be True. So "True


or True"

is True. The only time that it's False is when both of them are False.

Here's the truth table for "or": Everything is True except for "False or False"
which evaluates to False.

And then the "not" operator just evaluates to the opposite boolean value. "not
True" is False

and "not False" is True. So unlike the

"and" and "or" operators, the "not" operator only operates on one boolean value. So
its truth table is

fairly simple. You'll often mix boolean and comparison operators together in the
same expression.

You can have myAge = 26, myPet = 'cat'

Then we can have a complicated expression like myAge > 20 and myPet == 'cat'.

This evaluates to True because both parts on either side of the "and" operator
evaluate to True.

and that's it for this lesson. We now


know everything we need to start covering

flow control statements in the next


lesson.
Just to recap: The two values of the
boolean data type

are True and False. That's with a capital


T and capital F.

The six comparison operators are: equal to, not equal to,

less than, greater than, less than or


equal to, greater than or equal to.

The == operator is the


comparison operator

this has two equal signs while the single


equal sign operator is the assignment

operator that's used on assignment


statements like

spam = 42 to assign value 42


to the variable spam

The three boolean operators are "and", "or", and "not".

"And" is True both sides are True,


otherwise it's False. "Or" is True

as long as anything in it is True. It's


only False if

both of them are False. And the "not" operator simply evaluates to the opposite
boolean value.

Вам также может понравиться