xxxxxxxxxx
#the number is how many times you want to loop
for i in range (number):
#here where you put your looping code
#for example
for i in range (4):
print(Hello World)
print(Hi World)
#output
Hello World
Hi World
Hello World
Hi World
Hello World
Hi World
Hello World
Hi World
#check out more:https://www.askpython.com
xxxxxxxxxx
# (initial,final but not included,gap)
for i in range(1, 10, 2):
print(i)
# output: 1,3,5,7,9
# (initial, final but not included)
for i in range(1, 4):
print(i)
# output: 1,2,3 note: 4 not included
for i in range(5):
print(i)
# output: 0,1,2,3,4 note: 5 not included
python = ["ml", "ai", "dl"]
for i in python:
print(i)
# output: ml,ai,dl
# empty loop...if pass not used then it will return error
for i in range(1, 5):
pass
# break out loop
for i in range(1, 5):
if i == 3:
break
print(i)
# when you do not need to know the value of items use an underscore
for _ in range(1, 5):
pass
# continue to the start of the loop
for i in range(10):
if i == 3: # skips if i is 3
continue
print(i)
# The else statement is only reached if the for loop
# has run all the way through without breaking
list = [99, 98, 97, 96, 95, 94]
for number in list:
if number == 2:
print("There is a 2 in the list")
break
else:
print("There are no 2's in the list")
num = 1
# while loop
while num <= 5:
print(num)
num += 1for i in range(1, 10, 2): # (initial,final but not included,gap)
print(i);
# output: 1,3,5,7,9
for i in range(1, 4): # (initial, final but not included)
print(i);
# output: 1,2,3 note: 4 not included
for i in range(5):
print(i);
# output: 0,1,2,3,4 note: 5 not included
python = ["ml", "ai", "dl"];
for i in python:
print(i);
# output: ml,ai,dl
# empty loop...if pass not used then it will return error
for i in range(1, 5):
pass;
# break out loop
for i in range(1, 5):
if i == 3:
break
print(i)
# when you do not need to know the value of items use an underscore
for _ in range(1, 5):
pass
# continue to the start of the loop
for i in range(10):
if i == 3: # skips if i is 3
continue
print(i)
# The else statement is only reached if the for loop
# has run all the way through without breaking
list = [99, 98, 97, 96, 95, 94]
for number in list:
if number == 2:
print("There is a 2 in the list")
break
else:
print("There are no 2's in the list")
num = 1
# while loop
while num <= 5:
print(num)
num
xxxxxxxxxx
##### while
while condition:
# code block executed while condition True
else:
# code block executed when condition False and no break
##### for
for item in iterator
# code block executed for each item in iterator
else:
# code block executed when for finished and no break
for key, value in dictionary.items(): # example iterate over dictionary
##### Control Statements
break # jump out of (inner) loop
continue # jump to next repetition of (inner) loop
xxxxxxxxxx
text = "Hello World"
for i in text:
print(i)
#Output
#H, e, l, l, o, , W, o, r, l, d
for i in range(10):
print(i)
#1, 2, 3, 4, 5, 6, 7, 8, 9, 10
xxxxxxxxxx
def take_inputs2():
stop_word = 'stop'
data = None
inputs = []
while data != stop_word:
data = raw_input('please enter something')
inputs.append(data)
print inputs
xxxxxxxxxx
# There are 2 types of loops in python
# while loops and for loops
# a while loops continues for an indefinite amount of time
# until a condition is met:
x = 0
y = 3
while x < y:
print(x)
x = x + 1
>>> 0
>>> 1
>>> 2
# The number of iterations (loops) that the while loop above
# performs is dependent on the value of y and can therefore change
######################################################################
# below is the equivalent for loop:
for i in range(0, 3):
print(i)
>>> 0
>>> 1
>>> 2
# The for loop above is a definite loop which means that it will always
# loop three times (because of the range I have set)
# notice that the loop begins at 0 and goes up to one less than 3.