19
loading...
This website collects cookies to deliver better user experience
if
statementif … else
statementif … elif … else
statementif
statement is:if this is true
:
do this
:
and the indentation. Unlike other languages like Java and JavaScript that use curly braces to wrap a block of code, Python uses indentation instead. You will see the indentation syntax more often when you start dealing with functions and methods in python.command = 'Greet'
if command == 'Greet':
print('Hello, World!')
## output
# Hello, World!
command == Greet
and prints ”Hello, World!” to the console if it evaluates to true. This condition is known as a Boolean expression. An expression that evaluates to either true or false. Also, note the ==
operator that was used for the boolean expression. This is called the comparison operator and it’s different from the assignment operator =
. Operator | Meaning |
---|---|
== | is equal to |
!= | is not equal to |
> | is strictly greater than |
< | is strictly less than |
>= | is greater than or equal to |
<= | is less than or equal to |
True
or False
when used and they all accept two arguments, to the left, and to the right.command = 'Greet'
# when the boolean expression is not true
if command == 'greet':
print('Hello, World!')
print('Bye')
## output
# Bye
if
block was not executed because Greet
is not equal to greet
. They both have different Unicode values. The if
statement is the basic way our programs can make decisions by themselves.if … else
statement is:if <this is true>
:
<do this>
else:
<then do this>
if
statement, but the code in the else
block is executed only when the conditions in the if
block evaluate to false. The else
block is never executed when the if
block evaluates to true.greet = False
if greet:
print('Hello, World!')
else:
print('The system is unable to greet you')
print('Bye')
## output
# The system is unable to greet you
# Bye
greet
is used to hold a boolean value, which is what a boolean expression will return when executed. But greet
is false, which means the if
block won’t get executed, leaving the else
block to get executed. And the last line of the code that prints ”Bye” gets executed no matter what. if
statements allow you to nest an if
statement inside another if
statement. This is useful when you want to make decisions if a particular decision is true or not.i_am_human = True
height = 6
if i_am_human:
if height >= 6:
print('I am a tall human')
else:
print('I am an average height human')
else:
print('I am not a human')
## output
# I am a tall human
i_am_human
is true and height
is 6 or above. If i_am_human
was false, then the program won’t bother to check for the inner conditions, but rather jump to the else
block and output ” I am not a human”.if … elif … else
statements solve the problem when there exists more than one condition to be tested. In a more complex program, if a condition fails to be true, the system doesn’t quit immediately. Some other conditions need to be tested before quitting.if … elif … else
statements is:If <this is true>
:
<do this>
elif <the above is not true>
:
<do this>
else:
<do this if none of the above is true>
elif
is short for else if
. You can have as many elif
as desired in your programs. The last else
is optional. You can choose to add it or leave it out of your programs. Let's see how this works:age = 19
if age == 17:
print('I am 17 years old')
elif age == 18:
print('I am 18 years old')
elif age == 19:
print('I am 19 years old')
else:
print('Age not found')
## output
# I am 19 years old
age = 18
I am 18 years old
else
block is executed. When the age from the example above is set to 21, then none of the conditions are satisfied. Only then, the else block is executed.age = 21
Age not found