# Repetition Structure

Acknowledgement : the contents of this notebook are partially based on the book "Starting Out with Python (3rd Edition)".

A repetition structure causes a statement or set of statements to execute repeatedly.

In [3]:
# calculate an average of some numbers

number1 = float(input('number 1 : '))
number2 = float(input('number 2 : '))
number3 = float(input('number 3 : '))
number4 = float(input('number 4 : '))
number5 = float(input('number 5 : ')) # we need to call input function 5 times!

average = (number1+number2+number3+number4+number5)/5

print('Average = ',average)

number 1 : 1
number 2 : 2
number 3 : 3
number 4 : 4
number 5 : 5
Average =  3.0

In [3]:
# calculate an average of some numbers using the repetition structure

num = int(input('How many number? '))

average = 0.0
for i in range(num):
number = float(input('Number : ')) # here we only write this line only once!
average = average + number

average = average / num
print('Average = ',average)

How many number? 5
Number : 1
Number : 2
Number : 3
Number : 4
Number : 5
Average =  3.0


### The while Loop : A Condition-Controlled Loop

A condition-controlled loop causes a statement or set of statements to execute repeatedly as long as a condition is true.

while condition:
statement
statement
etc.

In the code above, the block of statements inside the while loop is executed repeatedly until the condition becomes false. The while loop is a pretest loop.

In [8]:
# while-loop example

print('Congratulation! you entered a correct password.')

Password? 123456
Congratulation! you entered a correct password.

In [3]:
# an infinite loop

while True:
print('hahaha')


### The for loop : A Count-Controlled Loop

A count-controlled loop iterates a specific number of times.

for var in [value1,value2,etc]:
statement
statement
etc.

In the code above, the block of statements is executed repeatedly and the number of times it gets executed equals to length of the list [value1,value2,etc]. Moreover, at each iteration, the value of the corresponding element in the list will be assigned to the variable var.

In Python, a comma-separated sequence of data items that are enclosed in a set of brackets is called a list.

In [4]:
# for-loop example

for i in [1,2,3,4,5,6,7,8,9,10]:
print(i)

1
2
3
4
5
6
7
8
9
10

In [5]:
# using the range function with the for loop (an iterable object contains a sequence of values
# that can be iterated over)

for i in range(15,0,-2):
print(i)

15
13
11
9
7
5
3
1

In [7]:
# example : the total sum of numbers

start = int(input('start: '))
stop = int(input('stop: '))

# swap the values of start and stop if start is greater than stop
if start > stop:
tmp = start
start = stop
stop = tmp

sum_value = 0
for i in range(start,stop+1):
sum_value += i

print(sum_value)

start: 30
stop: 5
455

In [13]:
# the augmented assignment operators
number = 2
number /= 2 # number = number / 2
print(number)

number += 10 # number = number + 10
print(number)

number -= 5 # number = number - 5
print(number)

number *= 2 # number = number * 2
print(number)

1.0
11.0
6.0
12.0


### Nested Loop

A nested loop refers to a structure of a loop inside another loop.

In [16]:
# print patterns

width = int(input('Width : '))
height = int(input('Height : '))

for h in range(height):
for w in range(width):
print('*',end='')
print('')

Width : 20
Height : 10
********************
********************
********************
********************
********************
********************
********************
********************
********************
********************

In [18]:
width = int(input('Width : '))

for w in range(width):
for i in range(w+1):
print('*',end='')
print('')

Width : 20
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
*******************
********************

In [ ]: