for loop and range
for loop is used for iterating over number of elements in specified iterable whereas while is condition-based iteration.
Iterables are datatypes which can be iterated such as str,range,list,set,tuple,dict. Iterable type are classes which object have collection of objects and to access those values one by one provides concept of iterator. Iterator is a programming feature by which we can access elements of iterable one by one without knowledge of inner arrangement of that iterable container.
Non iterable - primitive datatypes such as int,float,complex,bool
Iterable are the data structures on which we can apply concept of iterator. for loop only works on iterable *
Similar to while loop, all the transfer control statements like break,continue,pass can be used in for statement. we can also use else block to indicate normal execution without break.
range
- range is a class
- range is a datatype
- range is iterable
- range is a sequence (because position of elements is ensured)
- range elements are always of type int. NO other datatype.
- x = range (5) ==> this implies 0,1,2,3,4 (starting from 0)
Comments
Post a Comment