In [ ]:
for i in range(10):
print(i)
In [ ]:
for i in range(15):
print(i)
range(x)
gives a set of values between 0
and x-1
. Using for
, you can loop over the range
call.
In [ ]:
for i in range(11):
print(i)
In [ ]:
for i in range(21): # <- notice, you need 201 to get all the way to 20
print(i)
In [ ]:
x = 0
for i in range(1002):
x = x + i
print(x)
In [ ]:
for i in range(10):
if i > 5:
print(i)
In [ ]:
for i in range(10):
if i > 5:
print(i)
In [ ]:
for i in range(21):
if i < 8:
print(i)
In [ ]:
for i in range(10):
if i > 5:
break
print(i)
break
stops a currently running loop.
In [ ]:
for i in range(10):
print("HERE")
if i > 5:
continue
print(i)
continue
skips to the next iteration of the loop.
In [ ]:
x = 0
for i in range(1,100001):
x = x + i
if x > 30000:
break
print(x)
In [ ]:
x = 1
while x < 10:
print(x)
#x = x + 1
In [ ]:
x = 1000
while x > 100:
print(x)
x = x - 10
while
keeps running the loop if the condition is met.
In [1]:
x = 0
while x < 20:
print(x*x)
x = x + 1
In [ ]:
x = 5
while x < 12:
print(x*x)
x = x + 1
In [2]:
import math
x = 0
while x <= 2*math.pi:
x = x + math.pi/4
print(math.sin(x))
In [ ]: