semperos
0
Q:

how to do a for loop python

n = 4
for i in range(0, n): 
    print(i) 
8
text = "Hello World"
for i in text:
  print(i)
#Output
#H, e, l, l, o, , W, o, r, l, d
for i in range(10):
  print(i)
#1, 2, 3, 4, 5, 6, 7, 8, 9, 10
1
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
#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
print(range(4))
>>> [0,1,2,3]
print(range(1,4))
>>> [1,2,3]
print(range(2,10,2))
>>> [2,4,6,8]
2
# 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) 
Output-1: INCREMENTED FOR LOOP
0
1
2
3
4
Output-2: DECREMENTED FOR LOOP
4
3
2
1
0
2

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

	 
	print(x)
 
8
for index in range(len(list)): 
    print list[index]
5
 for x in range(6):

	 
	print(x)
3

New to Communities?

Join the community