print(3 < 5)
print(-1 < 0)
print(5 < 3)
print(3 < 3)
True True False False
<=
is less than or equal to.
print(4 <= 5)
print(5 <= 5)
print(6 <= 5)
True True False
>
is greater than.
print(4 > 5)
print(5 > 5)
print(6 > 5)
False False True
>=
is great than or equal to.
print(4 >= 5)
print(5 >= 5)
print(6 >= 5)
False True True
==
is equal to.
print(4 == 5)
print(5 == 5)
print(5 == 5.0)
print(5 == "5")
False True True False
Note that I started mixing up the data types.
For example,
5 == 5.0
is
True
but
5 == "5"
is not.
this again has to do with comparing different data types.
Python can reason that an
int
can be turned into a
float
for comparison, but it is not going to even try to convert a
string
to a numeric type.
However, instead of throwing an error it just returns
False
.
!=
is not equal to.
-
4 != 5
isTrue
-
5 != 5
isFalse
-
"5" != 5
isTrue
print(4 != 5)
print(5 != 5)
print("5" != 5)
True False True
However, none of this means I cannot compare string.
print("Hello" == "Hello")
print("Hello " == "Hello")
print("Whats up" != "doc")
True False True
if ¶
In programming, an essential construct for controlling the flow of your code is the
if
statement, more formally known as a conditional statement.
This powerful tool allows you to control whether a specific block of code is used based on something is
True
or
False
, called the condition.
We hinted at this in
functions
where we said 'if the tool is equal to
"whisk"
, then grab a whisk'.
In Python, this would look like this.
if tool == "whisk":
grab_whisk()
The
==
operator compares two things and sees if it is equal; this is different than assigning a variable with
=
.
In our case, if the data stored in the variable
tool
is
"whisk"
, then
tool == "whisk"
would evaluate to
True
.
Since this is
True
, we go "inside the
if
statement" specified after the
:
and indented by four spaces.
number = 10
if number > 0:
print("The number is positive.")
print("Done!")
The number is positive. Done!
In the case above, Python will compare
10 > 0
and see that it is
True
and go inside the
if
statement.
Remember that we have to tell Python when we have stopped defining code after
:
?
Well, we do it there as well where
print("Done!")
is not indented and will run no matter is
number > 0
.
number = -10
if number > 0:
print("The number is positive.")
print("Done!")
Done!
else ¶
In Python, the
else
statement serves as a dynamic tool to extend the functionality of
if
statements, providing a means to execute a block of code when the specified condition in the
if
statement evaluates to
False
.
This invaluable feature enhances the flexibility and control you have over your code execution.
The
else
statement is typically used in conjunction with an
if
statement.
It follows the body of the
if
block and introduces an alternative block of code to be executed when the condition of the
if
statement is not met.
number = -10
if number > 0:
print("The number is positive.")
else:
print("The number is not positive.")
print("Done!")
The number is not positive. Done!
number = 10
if number > 0:
print("The number is positive.")
else:
print("The number is not positive.")
print("Done!")
The number is positive. Done!
elif ¶
In Python, the
elif
(short for "else if") statement emerges as a powerful tool for introducing multiple tests within the decision-making process.
This feature is especially useful when you need to provide several choices, each contingent on its own set of conditions.
Understanding the nuances of
elif
is key to enhancing the flexibility and sophistication of your programs.
For example, what about 0? Zero is not positive, or negative, so we have another conditional.
number = 0
if number > 0:
print("The number is positive.")
elif number == 0:
print("The number is zero.")
else:
print("The number is negative.")
print("Done!")
The number is zero. Done!
Note that we can change our conditionals if we like. We will still get the same behavior.
number = 0
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
print("Done!")
The number is zero. Done!