Last active
June 9, 2019 06:26
-
-
Save guocheng/040601475522c094dbe2868bd5bd77f3 to your computer and use it in GitHub Desktop.
Python for and while loop
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": [ | |
"This is a summary of the [\"How to change index of for loop in Python?\n", | |
"\"](https://stackoverflow.com/questions/14785495/how-to-change-index-of-for-loop-in-python) post on Stackoverflow." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Problem\n", | |
"\n", | |
"I want to change the value of i in a for loop:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n" | |
] | |
} | |
], | |
"source": [ | |
"for i in range(5):\n", | |
" print(i)\n", | |
" i += 2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The value of i does not change in a for loop, becaue" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### The essense of a for loop" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"```python\n", | |
"for i in iterable: \n", | |
" # some code with i\n", | |
"```\n", | |
"\n", | |
"is a shorthand for\n", | |
"\n", | |
"```python\n", | |
"iterator = iter(iterable)\n", | |
"while True:\n", | |
" try:\n", | |
" i = next(iterator) # i is reassigned at the beginning of every iteration \n", | |
" except StopIteration:\n", | |
" break\n", | |
" # some code with i\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"`while` should be used instead if you intend to change the value of i:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0\n", | |
"2\n", | |
"4\n" | |
] | |
} | |
], | |
"source": [ | |
"i = 0\n", | |
"while i < 6:\n", | |
" print(i)\n", | |
" i+=2" | |
] | |
} | |
], | |
"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.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment