Loops
Loops
Loops
• for loop
• while loop
for loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).This is less like the for keyword in other
programming languages, and works more like an iterator method as found in
other object-orientated programming languages with the for loop we can
execute a set of statements, once for each item in a list, tuple, set
etc.The for loop does not require an indexing variable to set beforehand.
Example : Print each fruit in a fruit list
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Looping Through a String
Even strings are iterable objects, they contain a sequence of characters
Example: Loop through the letters in the word "banana":
for x in "banana":
print(x)
The break Statement
With the break statement we can stop the loop before it has looped through all
the items
With the continue statement we can stop the current iteration of the loop, and
continue with the next
With the while loop we can execute a set of statements as long as a condition is
true.
The while loop requires relevant variables to be ready, in this example we need
to define an indexing variable, i, which we set to 1.
With the break statement we can stop the loop even if the while condition is
true:
With the continue statement we can stop the current iteration, and continue with
the next:
With the else statement we can run a block of code once when the condition no
longer is true:
• Equals: a == b
• Not Equals: a!= b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
Example : If statement
a = 33
b = 200
if b > a:
print("b is greater than a")
In this example we use two variables, a and b, which are used as part of the if
statement to test whether b is greater than a. As a is 33, and b is 200, we know
that 200 is greater than 33, and so we print to screen that "b is greater than a".
Indentation
The elif keyword is Python's way of saying "if the previous conditions were not
true, then try this condition".
Example
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Else
The else keyword catches anything which isn't caught by the preceding
conditions.
Example
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
In this example a is greater than b, so the first condition is not true, also
the elif condition is not true, so we go to the else condition and print to screen
that "a is greater than b".
You can also have an else without the elif:
Example
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Short Hand If
If you have only one statement to execute, you can put it on the same line as the
if statement.
If you have only one statement to execute, one for if, and one for else, you can
put it all on the same line:
The not keyword is a logical operator, and is used to reverse the result of the
conditional statement:
Example
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
if statements cannot be empty, but if you for some reason have an if statement
with no content, put in the pass statement to avoid getting an error.
Example
a = 33
b = 200
if b > a:
pass