Update index.md (#25637)
Grammatical and spelling changes to improve readability.
This commit is contained in:
committed by
Randell Dawson
parent
663b293904
commit
5bd2e24820
@ -53,7 +53,7 @@ Give me a 'o'!
|
||||
Give me a 'n'!
|
||||
```
|
||||
|
||||
Some ways in which For loops are used:
|
||||
Some ways in which For loops are used:
|
||||
|
||||
**Iterate over the range() function**
|
||||
|
||||
@ -80,7 +80,7 @@ Output:
|
||||
9
|
||||
>
|
||||
```
|
||||
Additionally, one can specify the lower bound of the sequence and even the step of the sequence by adding a second and a third parameter.
|
||||
Additionally, one can specify the lower bound of the sequence and even the step of the sequence by adding a second and a third parameter.
|
||||
|
||||
```python
|
||||
for i in range(4,10,2): #From 4 to 9 using a step of two
|
||||
@ -144,13 +144,13 @@ orange #ffa500
|
||||
```
|
||||
**Iterate over two lists of same size in a single loop with the zip() function**
|
||||
|
||||
```python
|
||||
```python
|
||||
A = ["a", "b", "c"]
|
||||
B = ["a", "d", "e"]
|
||||
|
||||
for a, b in zip(A, B):
|
||||
print a, b, a == b
|
||||
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
@ -197,7 +197,7 @@ for index, item in enumerate(shopping_basket):
|
||||
print("Item", index, "is a", item)
|
||||
```
|
||||
**for/else statements**
|
||||
Python permits you to use else with for loops, the else case is executed when none of the conditions with in the loop body was satisfied. To use the else we have to make use of `break` statement so that we can break out of the loop on a satsfied condition. If we do not break out then the else part will be executed.
|
||||
Python permits you to use else with for loops. The else case is executed when none of the conditions within the loop were satisfied. To use the else we have to make use of `break` statement so that we can break out of the loop on a satisfied condition. If we do not break out then the else part will be executed.
|
||||
|
||||
```python
|
||||
week_days = ['Monday','Tuesday','Wednesday','Thursday','Friday']
|
||||
@ -216,7 +216,7 @@ In the above case the output will be `today is not a week day` since the break w
|
||||
We could also iterate inline using python, for example if we need to uppercase all the words in a list from a list we could simply do the following:
|
||||
|
||||
```python
|
||||
A = ["this", "is", "awesome", "shinning", "star"]
|
||||
A = ["this", "is", "awesome", "shining", "star"]
|
||||
|
||||
UPPERCASE = [word.upper() for word in A]
|
||||
print (UPPERCASE)
|
||||
@ -225,7 +225,7 @@ print (UPPERCASE)
|
||||
Output:
|
||||
```
|
||||
>
|
||||
['THIS', 'IS', 'AWESOME', 'SHINNING', 'STAR']
|
||||
['THIS', 'IS', 'AWESOME', 'SHINING', 'STAR']
|
||||
>
|
||||
```
|
||||
A interesting fact is that for loop is a bit faster compared to while loop in python. Because in for loop range/xrange is used which is implemented in C(in python's library) whereas in while loop we make use of some incremental statement specifically at the end of sentence like (i+=1) which is interpreted. Thus for loop is faster than while loop. Reference:
|
||||
@ -237,5 +237,3 @@ https://stackoverflow.com/questions/869229/why-is-looping-over-range-in-python-f
|
||||
- <a href='https://docs.python.org/2.7/tutorial/controlflow.html#for-statements' target='_blank' rel='nofollow'>Python2 for loop documentation</a>
|
||||
|
||||
- <a href='https://docs.python.org/3/tutorial/controlflow.html#for-statements' target='_blank' rel='nofollow'>Python3 for loop documentation</a>
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user