Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save Sarthak-Sidhant/23008db5b7bab619a6cf8f190c22da57 to your computer and use it in GitHub Desktop.
Detailed Instruction And Usage Of While 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.
#While Loop
#We Will Only Be Covering While Loop In This Tutorial
#While Loop is used to iterate over a block of code as long as the test expression (condition) is true.
#It is also called indefinite iteration.
print("While Loop")
i = 1 #this is a variable, and we have assigned the value 1 to it
while i < 10: #this is the test expression, and it will check if the value of i is less than 10
print(i) #the output will be numbers from 1 to 9, printed in a new line
i += 1 #this is the increment operator, and it will increment the value of i by 1
#the loop will continue to run until the value of i is 10, and it will stop at 9
#another use of while loop is to iterate over a list
print("While Loop with list")
list = [1,2,3,4,5,6,7,8,9,10] #this is a list
i = 0 #this is a variable, and we have assigned the value 0 to it
while i < len(list): #this is the test expression, and it will check if the value of i is less than the length of the list
print(list[i]) #the output will be numbers from 1 to 10, printed in a new line
i += 1 #this is the increment operator, and it will increment the value of i by 1
#the loop will continue to run until the value of i is 10, and it will stop at 9
#we can also use while loop to print odd/even numbers of a list
print("While Loop with list and if statement")
list = [1,2,3,4,5,6,7,8,9,10] #this is a list
i = 0 #this is a variable, and we have assigned the value 0 to it
while i < len(list): #this is the test expression, and it will check if the value of i is less than the length of the list
if list[i] % 2 == 0:
print(list[i])
i += 1 #this is the increment operator, and it will increment the value of i by 1
#the loop will continue to run until the value of i is 10, and it will stop at 9
#we cam also use while loop with the break statement
print("While Loop with break statement")
i = 1 #this is a variable, and we have assigned the value 1 to it
while i < 10: #this is the test expression, and it will check if the value of i is less than 10
print(i) #the output will be numbers from 1 to 9, printed in a new line
if i == 5:
break
i += 1 #this is the increment operator, and it will increment the value of i by 1
#the loop will continue to run until the value of i is 5, and it will stop at 5
#we can also use while loop with the continue statement
print("While Loop with continue statement")
i = 1 #this is a variable, and we have assigned the value 1 to it
while i < 10: #this is the test expression, and it will check if the value of i is less than 10
if i == 5:
i += 1
continue
print(i) #the output will be numbers from 1 to 9, printed in a new line
i += 1 #this is the increment operator, and it will increment the value of i by 1
#the loop will continue to run until the value of i is 10, and it will stop at 9
#we can also use while loop with the else statement
print("While Loop with else statement")
i = 1 #this is a variable, and we have assigned the value 1 to it
while i < 10: #this is the test expression, and it will check if the value of i is less than 10
print(i) #the output will be numbers from 1 to 9, printed in a new line
i += 1 #this is the increment operator, and it will increment the value of i by 1
else:
print("i is no longer less than 10")
#the loop will continue to run until the value of i is 10, and it will stop at 9
#the else statement will be executed after the loop is finished
#we can also use while loop with the pass statement
print("While Loop with pass statement")
i = 1 #this is a variable, and we have assigned the value 1 to it
while i < 10: #this is the test expression, and it will check if the value of i is less than 10
pass #the pass statement is used when a statement is required syntactically but you do not want any command or code to execute
i += 1 #this is the increment operator, and it will increment the value of i by 1
#the loop will continue to run until the value of i is 10, and it will stop at 9
#the pass statement will be executed after the loop is finished
#we can also use while loop with enumerate
print("While Loop with enumerate")
list = [1,2,3,4,5,6,7,8,9,10] #this is a list
i = 0 #this is a variable, and we have assigned the value 0 to it
while i < len(list): #this is the test expression, and it will check if the value of i is less than the length of the list
print(i, list[i]) #the output will be numbers from 1 to 10, printed in a new line
i += 1 #this is the increment operator, and it will increment the value of i by 1
#the loop will continue to run until the value of i is 10, and it will stop at 9
#we can also use while loop with zip
print("While 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
i = 0 #this is a variable, and we have assigned the value 0 to it
while i < len(list1): #this is the test expression, and it will check if the value of i is less than the length of the list
print(list1[i], list2[i]) #the output will be numbers from 1 to 10, printed in a new line
i += 1 #this is the increment operator, and it will increment the value of i by 1
#the loop will continue to run until the value of i is 10, and it will stop at 9
#we can also use while loop with enumerate and zip together
print("While Loop with enumerate and 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
i = 0 #this is a variable, and we have assigned the value 0 to it
while i < len(list1): #this is the test expression, and it will check if the value of i is less than the length of the list
print(i, list1[i], list2[i]) #the output will be numbers from 1 to 10, printed in a new line
i += 1 #this is the increment operator, and it will increment the value of i by 1
#the loop will continue to run until the value of i is 10, and it will stop at 9
#yeah thats enough loops. enjoy looping <3
@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