Last active
May 22, 2017 02:39
-
-
Save mGalarnyk/aa5bc03e01ead7f65c9c97e695d8bb5a to your computer and use it in GitHub Desktop.
Python Elif Statements for the blog post https://medium.com/@GalarnykMichael/python-basics-5-elif-statements-b8950dc71cf9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## elif statement" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Must be after an if statement. elif statement statement allows you to check multiple expressions for True and execute a block of code as soon as one of the conditions evaluates to True.\n", | |
"\n", | |
"Similar to the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"num = 21\n" | |
] | |
} | |
], | |
"source": [ | |
"num = 21\n", | |
"if num > 50:\n", | |
" print('num is larger than 50')\n", | |
"elif num == 21:\n", | |
" print('num = 21')\n", | |
"else:\n", | |
" print('Catchall condition')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 71, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Your number is odd\n" | |
] | |
} | |
], | |
"source": [ | |
"my_num = 5\n", | |
"if my_num % 2 == 0:\n", | |
" print(\"Your number is even\")\n", | |
"elif my_num % 2 != 0:\n", | |
" print(\"Your number is odd\")\n", | |
"else: \n", | |
" print(\"Are you sure your number is an integer?\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Later on in the tutorial series, we will go over simulating dice rolls and talking about the distribution" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"You rolled a 1. Great job!\n" | |
] | |
} | |
], | |
"source": [ | |
"# You can have mulitple elif statements. \n", | |
"# Remember only the first True statement has its block of code executed. \n", | |
"\n", | |
"dice_value = 1\n", | |
"if dice_value == 1:\n", | |
" print('You rolled a {}. Great job!'.format(dice_value))\n", | |
"elif dice_value == 2:\n", | |
" print('You rolled a {}. Great job!'.format(dice_value))\n", | |
"elif dice_value == 3:\n", | |
" print('You rolled a {}. Great job!'.format(dice_value))\n", | |
"elif dice_value == 4:\n", | |
" print('You rolled a {}. Great job!'.format(dice_value))\n", | |
"elif dice_value == 5:\n", | |
" print('You rolled a {}. Great job!'.format(dice_value))\n", | |
"elif dice_value == 6:\n", | |
" print('You rolled a {}. Great job!'.format(dice_value))\n", | |
"else:\n", | |
" print('None of the conditions above (if elif) were evaluated as True')" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [conda env:py36]", | |
"language": "python", | |
"name": "conda-env-py36-py" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment