Skip to content

Instantly share code, notes, and snippets.

@Sarthak-Sidhant
Created February 5, 2023 05:19
Show Gist options
  • Select an option

  • Save Sarthak-Sidhant/8dcf358a5ac5e19ac0f7007964888dae to your computer and use it in GitHub Desktop.

Select an option

Save Sarthak-Sidhant/8dcf358a5ac5e19ac0f7007964888dae to your computer and use it in GitHub Desktop.
Detailed Instruction And Different Usages Of The For Loop By Sarthak Sidhant
# Edited by: @Sarthak-Sidhant (GitHub) and Copyrights Reserved By https://sarthaksidhant.me/ & https://decodificate.tech/
# No Parts Of This Text or Code Shall be Reproduced Without Permission, Unless Used For Educational Purposes.
#Loops in python
#For Loop
#While Loop
#For Loop
#We Will Only Be Covering For Loop In This Tutorial
#For Loop is used to iterate over a sequence (list, tuple, string) or other iterable objects.
#Iterating over a sequence is called traversal.
print("For Loop")
for i in range(1,10): #the range function is used to generate a sequence of numbers, it starts from 0 in default, but here we have given the starting number as 1 and the ending number as 10, so the output will be numbers from 1 to 9
print(i) #the output will be numbers from 1 to 9, printed in a new line
#loop will continue to run unless it has achieved the ending number, in this case 10, however, it wont print 10, because the ending number is 10, and the loop will stop at 9
#another use of for loop is to iterate over a list
print("For Loop with list")
list = [1,2,3,4,5,6,7,8,9,10] #this is a list
for i in list: #here we are iterating over the list
print(i) #the output will be numbers from 1 to 10, printed in a new line
#another use of for loop is to iterate over a string
print("For Loop with string")
#we can also use for loop for different usages, like to print all the odd numbers from 1 to 36
#for this, we will use the range() function, and we will use the step parameter
#range function has three parameters which are seperated by commas
#the first parameter is the starting number, the second parameter is the ending number, and the third parameter is the step
#the step parameter is used to skip a number of numbers, for example, if we have a range of 1 to 10, and the step is 2, then the output will be 1,3,5,7,9
#the step parameter is optional, and if it is not given, then the step will be 1
print("For Loop with step")
for i in range(1,36,2): #here we are using the range function with the step parameter
print(i) #the output will be all the odd numbers from 1 to 36, printed in a new line
#there are other usage of for loop, like enumerate
#enumerate is used to iterate over a list, and it returns the index of the list, and the value of the list
print("For Loop with enumerate")
list = [1,2,3,4,5,6,7,8,9,10] #this is a list
for index, value in enumerate(list): #here we are using the enumerate function
print(index, value) #the output will be the index of the list, and the value of the list, printed in a new line
#we can also use for loop with the zip function
#zip function is used to iterate over two lists at the same time
print("For Loop with zip")
list1 = [1,2,3,4,5,6,7,8,9,10] #this is a list
list2 = [11,12,13,14,15,16,17,18,19,20] #this is a list
for i, j in zip(list1, list2): #here we are using the zip function
print(i, j) #the output will be the values of the two lists, printed in a new line
#nah thats hella much, thanks for being here.
@Sarthak-Sidhant

Copy link
Copy Markdown
Author

This Text/Code Shall Not Be Used For Commercial Purposes.
It acts as an external gist to my notion and python guide (https://sarthaksidhant.me/python)
For Any Doubts, Kindly Contact Me Through My Github.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment