A control statement is a statement that allows the computer to select or repeat an action. Statements generally get run sequentially.
Let's start by looking at loops, which are control statements and is fundamental to programming as whole and not just python.
The thing computers know to do best is do something repeatedly and fast. And certainly loops are what facilitate this ability.
Loops are also known as repetition statements, they repeat an action.
Every repeat is known as a pass or an iteration.
Loops can be divided into two:
For loops are the most commonly used type of loops. They perform an action a given number of times.
For loops have the following syntax:
for value in iterable: pass
Here's a for loop example:
for greeting in range(5): print('Am sending you greeting ',greeting)
Let's put it in a class for a more complete snippet like we always do:
class Greetings: def send(self): for greeting in range(5): print('Am sending you greeting ',greeting) greetings=Greetings() greetings.send()
Am sending you greeting 0 Am sending you greeting 1 Am sending you greeting 2 Am sending you greeting 3 Am sending you greeting 4
print() function is executed 5 times since we have put in a loop.
In our loop you can see that we have the loop header, which is that first line in the loop. The number
5 denotes the number of repeats we want to be performed.
We end the loop header with a colon(:).
After the loop header we have the loop body. It can consists of several statements.However, in this case we have only one, the
You always have to indent the statements in the loop body in the same column since python as a language is indentation-sensitive.
The statements get executed in the order they are defined.
Note that in the above example our
range() function will start counting at
0 by default.
We can change this by specifying the start ourselves:
class Hello: def send(self): for num in range(5,10): print('Am sending you hello ',num) hello=Hello() hello.send()
Am sending you hello 5 Am sending you hello 6 Am sending you hello 7 Am sending you hello 8 Am sending you hello 9
Let's look at example 2.
We want to create a function that can potentially replace the exponentiation operator in python. The exponentiation operator is an operator that raises a number to a given power.
3 ** 3 = 3*3*3 = 27. That is 3 raised to power 3 is 9.
class Exponentier: def get_power(self,number,exponent): product = 1 for num in range(exponent): product = product * number print(number,'raised to power',exponent,'is',product) exponentier = Exponentier() exponentier.get_power(3,3)
3 raised to power 3 is 27
selfis mandatory and refers to the instance of this class which will execute this method. The other two(
exponent) are positional arguments and will be passed when this method is invoked.
range()function. It will define the total amount of repeats we will have.
numberfor each iteration and hold the result in the
get_power()method passing in the parameters
The most common uses of loops is not even what simple count-controlled repetitions we've looked above. It is to traverse sequences of data.
The sequences may include
Here's the syntax:
for <variable> in <sequence>: # do something
On each pass through the loop the variable is assigned value in the sequence.
Let's look at List Traversal
class Spacecraft: def showSpacecrafts(self): for spacecraft in ['Casini','Kepler','Spitzer','Voyager A','WMAP']: print(spacecraft) s=Spacecraft() s.showSpacecrafts()
Casini Kepler Spitzer Voyager A WMAP
We can also use
for loops to traverse strings.
Strings are sequences of Unicode characters.
class Camposha: def spell_site(self): for letter in 'Camposha': print(letter) c=Camposha() c.spell_site()
C a m p o s h a