juergen d
0
Q:

how to use for loops 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
print(range(4))
>>> [0,1,2,3]
print(range(1,4))
>>> [1,2,3]
print(range(2,10,2))
>>> [2,4,6,8]
2
for index in range(len(list)): 
    print list[index]
5

  fruits = ["apple", "banana", "cherry"]
for 
  x in fruits:

	 
	print(x)
 
8
fruits = ["apple", "banana", "cherry"]
for 
  x in fruits:
	print(x) 
2
for x in range(0, 3):
    print("We're on time %d" % (x))
0

New to Communities?

Join the community