Skip to content

Instantly share code, notes, and snippets.

@masasa27
Created August 1, 2020 20:10
Show Gist options
  • Select an option

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

Select an option

Save masasa27/d64dfbdda51acd1bbfd59d905f1b2d57 to your computer and use it in GitHub Desktop.
loops
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Loops"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For loop"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"animels = ['cat', 'dog', \"mouse\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cat\n",
"dog\n",
"mouse\n"
]
}
],
"source": [
"for animel in animels:\n",
" print(animel)\n",
"# this iterates over the list, and assign the next value into animel each time"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n"
]
}
],
"source": [
"for i in range(3): # range gives numbers from 0 to 3\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## While loops"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"3\n",
"2\n",
"1\n",
"-4\n",
"-3\n",
"-2\n",
"-1\n"
]
}
],
"source": [
"x = 4\n",
"while x:\n",
" print(x)\n",
" x = x - 1\n",
"# when x is 0, the while statement is false, and we leave\n",
"\n",
"# this is the same as\n",
"x = -4\n",
"while x != 0:\n",
" print(x)\n",
" x = x + 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Continue and Break"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# continue moves into the next line of iteration, break exit the loop"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cat doesnt have an o in its name\n"
]
}
],
"source": [
"for animel in animels:\n",
" if 'o' in animel:\n",
" continue\n",
" else:\n",
" print(animel, 'doesnt have an o in its name')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"9\n",
"8\n"
]
}
],
"source": [
"x = 10\n",
"while True: # suppose to never end\n",
" print(x)\n",
" x = x - 1\n",
" if x == 7:\n",
" break # saves us from an endless loop"
]
}
],
"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