conditional statement: if-then-else
if cond :
do-true
else:
do-false
if 3 > 2 :
print("yes")
else:
print("no")
while cond :
do-loop
i = 5
while i > 0 :
print(i)
i = i - 1
function range() is used to specify the range of value. Mostly it is
used to iterate the loop.
range()
returns list of integers
range(n)
returns [0,n) with increment 1
range(4)
--> 0,1,2,3
range(start,stop,step)
range(1,10,2)
--> 1,3,5,7,9
Observe what values this snippet of code print out.
x = 0
while x in range(10):
print(x)
x = x + 1
change the above program so that it prints 10.
for x in range(10)
print(x)
It is a bit more compact than while-loop. initial x and increment is
automatic.
try
for x in range(1,10,2)
print(x)
The above code is similar to this while-loop, observe the initialize of
variable and its increment.
x = start
while x < stop
do-body
x = x + step
We draw flowchart to prime our brain into the habit of breaking a big problem into smaller steps. Somebody may find "pseudo code" is easier to work with. I found that for "spatial" type of thinker, diagram works very well. Try it anyway at least to know what type you prefer.
sequential
if-then-else
while-loop
for-loop
---- end -----