R3l1c
0
Q:

how to make a loop in python

n = 4
for i in range(0, n): 
    print(i) 
8
a_list = [1,2,3,4,5]

#this loops through each element in the list and sets it equal to x
for x in a_list:
	print(x)
1
#While Loop:
while ("condition that if true the loop continues"):
  #Do whatever you want here
else: #If the while loop reaches the end do the things inside here
  #Do whatever you want here

#For Loop:
for x in range("how many times you want to run"):
  #Do whatever you want here
5
#simple for loop to print numbers 1-99 inclusive
for i in range(1,100):
  print(i)
  
#simple for loop to loop through a list
fruits = ["apple","peach","banana"]
for fruit in fruits:
  print(fruit)
1
x = 0
while True: #execute forever
	x = x + 1
	print(x)
1
for index in range(len(list)): 
    print list[index]
5
fruits = ["apple", "banana", "cherry"]
for 
  x in fruits:
	print(x) 
2
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
  if x == "banana":
    break
1
# A Sample Python program to show loop (unlike many 
# other languages, it doesn't use ++) 
# this is for increment operator here start = 1,  
# stop = 5 and step = 1(by default) 
print("INCREMENTED FOR LOOP") 
for i in range(0, 5): 
   print(i) 
  
# this is for increment operator here start = 5,  
# stop = -1 and step = -1  
print("\n DECREMENTED FOR LOOP") 
for i in range(4, -1, -1): 
   print(i) 
0
for x in range(0, 3):
    print("We're on time %d" % (x))
0

New to Communities?

Join the community