#the number is how many times you want to loopfor i inrange(number):#here where you put your looping code#for examplefor i inrange(4):print(Hello World)print(Hi World)#output
Hello World
Hi World
Hello World
Hi World
Hello World
Hi World
Hello World
Hi World
#check out more:https://www.askpython.com
#From a minimum (inclusive) to maximum (exclusive).#Minimum is 0 unless otherwise specifiedfor i inrange(4)print(i)#0, 1, 2, 3for i inrange(2,4)print(i)#2, 3for i inrange(1,7,2):print(i)#1, 3, 5
# Planet list#twitter ----------->: @MasudHanif_# Happy Coding..
planet =["Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"]for planets in planet:print(f"{planets} from solar system")
# if you want to get items and index at the same time,# use enumerate
fruits =['Apple','Banana','Orange']for indx, fruit inenumerate(fruits):print(fruit,'index:', indx)
A for loop iterates through an iterable, may that be an array, a string,or a range of numbers
#Example:
myArr =["string 1","string 2"]for x in myArr:print(x)#Returns "string 1", "string 2". In here the x is an iterator and does not need to be defined.
# Python for loopfor i inrange(1,101):# i is automatically equals to 0 if has no mention before# range(1, 101) is making the loop 100 times (range(1, 151) will make it to loop 150 times)print(i)# prints the number of i (equals to the loop number)for x in[1,2,3,4,5]:# it will loop the length of the list (in that case 5 times)print(x)# prints the item in the index of the list that the loop is currently on
# For loop where the index and value are needed for some operation# Standard for loop to get index and value
values =['a','b','c','d','e']print('For loop using range(len())')for i inrange(len(values)):print(i, values[i])# For loop with enumerate# Provides a cleaner syntaxprint('
For loop using builtin enumerate():')for i, value inenumerate(values):print(i, value)# Results previous for loops:# 0, a# 1, b# 2, c# 3, d# 4, e# For loop with enumerate returning index and value as a tupleprint('
Alternate method of using the for loop with builtin enumerate():')for index_value inenumerate(values):print(index_value)# Results for index_value for loop:# (0, 'a')# (1, 'b')# (2, 'c')# (3, 'd')# (4, 'e')
# Program to find the sum of all numbers stored in a list# List of numbers
numbers =[6,5,3,8,4,2,5,4,11]# variable to store the sumsum=0# iterate over the listfor val in numbers:sum=sum+val
print("The sum is",sum)
# Range:for x inrange(5):print(x)# prints 0,1,2,3,4# Lists:
letters =['a','b','c']for letter in letters:print(letter)# prints a, b, c# Dictionaries:
letters ={'a':1,'b':2,'c':3}for letter, num in letters.items():print(letter, num)# prints a 1, b 2, c 3
# a 'while' loop runs until the condition is broken
a ="apple"while a =="apple":
a ="banana"# breaks loop as 'a' no longer equals 'apple'# a 'for' loop runs for the given number of iterations...for i inrange(10):print(i)# will print 0, 1, 2, 3, 4, 5, 6, 7, 8, 9# ... or through a sequence
array =[3,6,8,2,1]for number in array:print(number)# will print 3, 6, 8, 2, 1
iteration_number =10# can be any amount or how many times you want to iteratefor iteration inrange(iteration_number):# how many iterations - iteration_number variableprint(iteration)
for _ inrange(1,10,2):#(initial,final but not included, gap) (the "_" underscore symbol mean there are no variables initialize in this loop)print("hi");
for item in["a","b","c"]:for i inrange(4):# 0 to 3for i inrange(4,8):# 4 to 7for i inrange(1,9,2):# 1, 3, 5, 7for key, val indict.items():for index, item inenumerate(list):