Skip to content

Instantly share code, notes, and snippets.

@masasa27
Last active August 1, 2020 19:59
Show Gist options
  • Select an option

  • Save masasa27/a44be472e0f2f943e3372fd389fc171f to your computer and use it in GitHub Desktop.

Select an option

Save masasa27/a44be472e0f2f943e3372fd389fc171f to your computer and use it in GitHub Desktop.
flow control
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Flow Control"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"a = 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Boolean test"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a == 5"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a == 4"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"any number but 0 is true\n",
"\n",
"note that if 0 did nothing\n"
]
}
],
"source": [
"if 0:\n",
" print(\"this won't do anything\")\n",
"if 1:\n",
" print(\"any number but 0 is true\")\n",
"\n",
"print(\"\\nnote that if 0 did nothing\")\n",
"# an empty string, list and 0 evaluate to False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The concept"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a is equal or less than 5\n"
]
}
],
"source": [
"if a > 5:\n",
" # 4 spaces indentation\n",
" print(\"a is bigger then 5\")\n",
" \n",
"else:\n",
" print(\"a is equal or less than 5\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a is 5\n"
]
}
],
"source": [
"if a > 5:\n",
" # 4 spaces indentation\n",
" print(\"a is bigger then 5\")\n",
"\n",
"elif a < 5:\n",
" print(\"a is bigger then 5\")\n",
"\n",
"else:\n",
" print('a is 5')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a is not equal to 4\n"
]
}
],
"source": [
"if a != 4: # ! evaluate to true if the opposite is correct, is a is not equal to 4\n",
" print('a is not equal to 4')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, False)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a == 5, a == 4 # == checks a condition, does not assign a value!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Multiple checks, or & and"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a is between 3 and 7\n"
]
}
],
"source": [
"if a > 3 and a < 7: # both must be true for the statement to occur\n",
" print(\"a is between 3 and 7\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a is greater than 3, or less than -5\n"
]
}
],
"source": [
"if a > 3 or a < -5: # # obly one must be true for the statement to occur\n",
" print(\"a is greater than 3, or less than -5\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment