Iterations More Patterns
Counting Loop
Code Example1
count = 0
print ('before',count)
for thing in [45, 45,67,23,1,23,78,90]:
count = count + 1
print (count , thing)
print('After' ,count)
we set the variable = 0 then add then each time the code run just to count how many time it run
Output
before 0
1 45
2 45
3 67
4 23
5 1
6 23
7 78
8 90
After 8
Example 2
rock = 0
print ('Before',rock)
for thing in [56,78,2,90,45]:
rock = thing +rock
print (rock,thing)
print ('After',rock)
we sum the thing and rock each time it run
Output
Before 0
56 56
134 78
136 2
226 90
271 45
After 271
Finding the Average in the loop
Code Example 3
count = 0
sum = 0
print ('Before',count ,sum)
for value in [7,89,4,67,25,33]:
count = count +1
sum = sum + value
print (count,sum,value)
print('After',count,sum,sum/count)
Output
Before 0 0
1 7 7
2 96 89
3 100 4
4 167 67
5 192 25
6 225 33
After 6 225 37.5
Filtering in the loop
Code Example 4
print('Before')
for value in [45,14,7,23,87]:
if value > 20:
print('large number:',value)
print ('After')
It will check and print out if the value is larger than 20 and go back and check again till the end of the value
Output
Before
large number: 45
large number: 23
large number: 87
After
Search using Boolean Variable
- Boolean has only Two value , True and False
code Example 5
found = False
print('Before',found)
for value in [56,34,67,8,12,3,0]:
if value == 8:
found = True
print (found,value)
print('After',found)
Output
Before False
False 56
False 34
False 67
True 8
True 12
True 3
True 0
After True
As you can see that it turns True when it found 8 and then even if the conditional is not true it still print out True(maybe i will find in exercise)
-
How to find the smallest value?
## Code Example 6
largest_so_far = -1 print ('Before',largest_so_far) for the_num in [45,2,67,8,12,44]: if the_num > largest_so_far: largest_so_far = the_num print(largest_so_far,the_num) print('After', largest_so_far)### Output
Before -1 45 45 45 2 67 67 67 8 67 12 67 44 After 67 -
Answer
Just change the > to <
smallest_so_far = -1 print ('Before',smallest_so_far) for the_num in [45,2,67,8,12,44]: if the_num < smallest_so_far: smallest_so_far = the_num print(smallest_so_far,the_num) print('After', smallest_so_far)### Output
Before -1 -1 45 -1 2 -1 67 -1 8 -1 12 -1 44 After -1if we make the first smallest_so_far to bigger number
smallest_so_far = 100 print ('Before',smallest_so_far) for the_num in [45,2,67,8,12,44]: if the_num < smallest_so_far: smallest_so_far = the_num print(smallest_so_far,the_num) print('After', smallest_so_far)Before 100 45 45 2 2 2 67 2 8 2 12 2 44 After 2
None
- None is a constant that has only one value is None
Code Example 7
smallest = None
print ('Before')
for value in [45,2,67,9,23]:
if smallest is None:
smallest = value
elif value < smallest:
smallest = value
print (smallest,value)
print ('After',smallest)
Output
Before
45 45
2 2
2 67
2 9
2 23
After 2
None is means nothing
is means ==
is not means False
Question
-
Which of these evaluates to False?
0 == 0.00 is 0.00 is not 0.00 = 0.0 -
Answer
0 is 0.0