0
Please can someone explain loops (for), and range to me like a child
3 Answers
+ 1
1- 'for' is used to iterate over a finite (list,dict,tuple,set ...)(iterable or generator) returning each element in their order on iterable:
#example
for i in [1,2,3,4]: # gets each element using 'i' local variable
print(i) # stuff to do with each 'i'
2- range() is used to get a 'range' object generating values from start=0(default) to stop(non-default) with step=1(default)
# example
print(range(4))# prints __repr__ of 'range' object
print(list(range(4))) # prints [0,1,2,3]
print(list(range(0,10,2))) # prints [0,2,4,6,8]
Notice that stop is not included in result and that all values can be changed
# 'range' is best friend with 'for'
for i in range(4):
print(i)
That's all (ć»āć»)
0
Thank you very much
0
Was a pleasure (ļ½”ā¢Ģį“-)ā§