Python 101 Part 3
We back again. we dive straight into loops.
In python we have 2 major loops:
- While loop
- For loop (Mostly used)
WHILE LOOPS
With while loops, statements are executed while the conditions are True.
e.g
i = 0
while i < 0:
i+=1
print(i)
let me breakdown down the code snippet:
- i = this is the variable. It is the indexing .
- while i < 0: = this is the statement which will be checked whether it checks out to True.
- i+=1 = this is to be executed when the condition doesn't check out to True.
- print(i) = displays output of the executed code.
Break
we use break to stop the loop amid the execution once a certain condition is met or check out to be True.
e.g
i = 0
while i < 10:
i+=1
print(i)
if i == 5:
break
print("You are halfway there")
i +=1
Continue
continues with the execution even if the condition is True.
i = 0
while i < 10:
i += 1
if (i == 5 ):
print(i,"You are halfway there")
continue
print(i)
Else
runs a the code when the condition is not True
i = 0
while i < 10:
print(i)
i += 1
else:
print(i,"i is greater than 10")
FOR LOOP
A programmers' favorite loop because it loops everything :-), Anyway a for loop is a loop which iterates a sequence.
The difference between a for loop and a while loop is that a for loop doesn't require indexing unlike a while loop which you must provide a n indexing(Base of the loop).
Looping through a string.
e.g
for x in "aimsec":
print(x)
let me break down this statement:
- for x in "aimsec": = this is the statement
- print(x) = this will print each and every character in the string "aimsec"
Break
With break, we can stop the looping before it has completed.
e.g
for x in "aimsec":
print(x)
if (x == "s"):
print("we stop here")
break
let me break it down:
- for x in "aimsec" = this statement iterates the string "aimsec" and assigning each character to x as a variable.
- print(x) = prints the variable
- if( x == "s") = this sets up a condition that if x == "s"
- print("We stop here") = this code is executed after which the condition is True
- break = this stops the iteration because the condition is True.
Continue
Were there is a break continue never misses. :-), we use continue to continue the iteration after a condition is met in a loop.
e.g
for x in "aimsec":
if (x == "s"):
print(x, "is omitted")
continue
print(x)
Range( )
loops through a sequence from 0 to a specific number and increments by 1.
for x in range(5):
print(x)
and in order to set a specific starting point in range you'll have to parse two arguments in the range function. e.g range(2, 5)
and for setting an incrementing value in the range, we parse 3 arguments e.g range(2, 5, 2)
Else
Executes a block of code when the loop is finished. e.g
for x in "aimsec":
print(x)
else:
print("Loop is complete")
Nested Loops
This is simply a loop inside a loop, whereby the inner loop is executed once at each iteration of the outer loop. e.g
for x in "aim":
for y in "sec":
print(x,y)
Pass
used instead of empty space. and returns nothing :-)
e.g
for x in "aimsec":
pass
Exercise: look into python conditional statements e.g IF, ELIF, ELSE
Stay tuned tomorrow we doing the IF statements.ADIOS!
Exercise: look into python conditional statements e.g IF, ELIF, ELSE
Stay tuned tomorrow we doing the IF statements.ADIOS!
Post a Comment: