Skip to content

Instantly share code, notes, and snippets.

@Python3-8
Created December 18, 2020 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Python3-8/2b55a5d6ee0d5408107470f38225b105 to your computer and use it in GitHub Desktop.
Save Python3-8/2b55a5d6ee0d5408107470f38225b105 to your computer and use it in GitHub Desktop.
This is the If, Elif, and Else Statements Practice HTML document that I created. It is featured in my YouTube video If, Elif, and Else Statements in Python. You can use this doc to practice these statements.
<!DOCTYPE html>
<html>
<head>
<title>If, Elif, and Else Statements in Python</title>
<style>
table,
td,
th,
input {
color: yellow;
padding: 10px;
border: 0.5px dotted yellow;
border-radius: 10px;
background-color: rgb(41, 41, 41);
text-align: center;
}
h1,
h2,
p {
color: yellow;
}
input {
border: 1px solid darkgray;
}
body {
background-color: rgb(41, 41, 41);
}
code {
color: black;
background-color: lightgray;
border-radius: 4px;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<center>
<h1>If, Elif, and Else Statements in Python</h1>
</center>
<hr>
<center>
<h2>If Statements</h2>
</center>
<p>
<code>if</code> statements are used to run a code block if a certain condition is true. If the
condition appears to be true,
Python executes the code block.
<br>
</p>
<center>
<p>Let's print <code>'Hello'</code> if a variable named <code>a</code> is equal to 1.</p>
<textarea rows="7" cols="25">
# main.py
a = 1
if a == 1:
print('Hello')
# You can indent code with either one tab or 4 spaces per indent
</textarea>
<br>
<textarea rows="1" cols="25">$ python3 main.py</textarea>
<br><br>
<img src="https://www.wpclipart.com/dl.php?img=/signs_symbol/arrows/arrows_color/arrow_outline_yellow_down_T.png"
height="150" width="100">
<br><br>
<p>
Remember the <code>==</code> operator? You probably do if you watched my operators video. Since in the above
program
<code>a</code> is set to <code>1</code>, you know that <code>a == 1</code> will return <code>True</code>.
The <code>if</code>
statement runs the indented code (in this case, <code>print('Hello')</code>) which is a code block if the
condition returns <code>True</code>.
</p>
<br><br>
<img src="https://www.wpclipart.com/dl.php?img=/signs_symbol/arrows/arrows_color/arrow_outline_yellow_down_T.png"
height="150" width="100">
<br><br>
<p>This will output <input> because the variable <code>a</code> is set to <input>.</p>
</center>
<hr>
<center>
<h2>Else Statements</h2>
</center>
<p>
<code>else</code> statements are used to run a code block if the condition provided in the <code>if</code> block
is false. If the
condition appears to be false,
Python executes the <code>else</code> block.
<br>
</p>
<center>
<p>Let's print <code>'Hello'</code> if a variable named <code>a</code> is not equal to 1.</p>
<textarea rows="6" cols="15">
# main.py
a = 2
if a == 1:
pass # Do nothing
else:
print('Hello')
</textarea>
<textarea rows="6" cols="15">
# Better way
a = 2
if a != 1:
print('Hello')
</textarea>
<br>
<textarea rows="1" cols="34">$ python3 main.py</textarea>
<br><br>
<img src="https://www.wpclipart.com/dl.php?img=/signs_symbol/arrows/arrows_color/arrow_outline_yellow_down_T.png"
height="150" width="100">
<br><br>
<p>
Here, the <code>==</code> operator will return <code>False</code>. So the <code>else</code> block will get
executed.
</p>
<br><br>
<img src="https://www.wpclipart.com/dl.php?img=/signs_symbol/arrows/arrows_color/arrow_outline_yellow_down_T.png"
height="150" width="100">
<br><br>
<p>This will output <input> because the variable <code>a</code> is set to <input>.</p>
</center>
<hr>
<center>
<h2>Elif Statements</h2>
</center>
<p>
Imagine you have an <code>ifelse</code> statement in your code. But you don't want Python to go straight to
the else block if your <code>if</code> condition returns <code>False</code>. Instead you want to check for
another
or multiple other condition(s) before you move on to the <code>else</code> block. To achieve this, you can add
an <code>elif</code> block in between your <code>if</code> and <code>else</code> blocks.
<br>
</p>
<center>
<p>Let's print <code>'Hello'</code> if a variable named <code>a</code> is equal to 1. Else if it is equal to 2,
let's print <code>'Hello World'</code>. If it's equal to neither of them, let's print
<code>'Bye bye World'</code></code></p>
<textarea rows="9" cols="22">
# main.py - Better way
a = 2
if a == 1:
print('Hello')
elif a == 2:
print('Hello World')
else:
print('Bye bye World')
</textarea>
<textarea rows="9" cols="22">
# Harder way
a = 2
if a == 1:
print('Hello')
else:
if a == 2:
print('Hello World')
else:
print('Bye bye World')
</textarea>
<br>
<textarea rows="1" cols="48">$ python3 main.py</textarea>
<br><br>
<img src="https://www.wpclipart.com/dl.php?img=/signs_symbol/arrows/arrows_color/arrow_outline_yellow_down_T.png"
height="150" width="100">
<br><br>
<p>
Here, the <code>==</code> operator will return <code>False</code> in the <code>if</code> block. So Python will move on to the <code>elif</code> block.
The <code>elif</code> block will get executed.
</p>
<br><br>
<img src="https://www.wpclipart.com/dl.php?img=/signs_symbol/arrows/arrows_color/arrow_outline_yellow_down_T.png"
height="150" width="100">
<br><br>
<p>This will output <input> because the variable <code>a</code> is set to <input>.</p>
</center>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment