Loops: let's take a example of list. eg: students_names = ["Abhinaw", "Aman", "Ashish", "Himanshu'] if you want to print single element then print(student_names[0]) is fine but print every element of list we need some kind of loop. There are two main loops in Python for loop and while loop.lets have a for loop example. for name in student_names: print("Students name is {0}" .format(name)) So it will print all the elements of list student_names.Simply name is a variable. other programming for loop: for(var i=0; I<someArray.length; I++) { var element=someArray[i]; console.log(element); } However this is not the case with the Python.this is not the way Python for loop works.pyhton does hide many things.it automatically assumes that you want to start from the first element from the list and also it knows when to stop.Pyhton does not have foreach loop. range function let's have an example. x =0 for index in range(10): x+=10 print("The value of a is {0}".format(x)) output will be The value of a is 10 The value of a is 20 The value of a is 30 The value of a is 40 The value of a is 50 The value of a is 60 The value of a is 70 The value of a is 80 The value of a is 90 The value of a is 100 So whats the range function do.basically it takes a range value and kind of converts it in list.it will execute 10 times.So in this case the index will be the element of the list.So if you don't want to start your for loop to start from 0 then range function also supports two argument like this range(5,10) in this case index value will be [5,6,7,8,9] And also if you want to increment the index value by 2 every time it execute it also supports such as range(5,10,2) in this case 2 denots the increment.So our list will be [5,7,9] you can also exit the loop in between based on some logic using break and continue.
top of page
bottom of page