Answers for "for in loop python"

74

python loops

#x starts at 1 and goes up to 80 @ intervals of 2
for x in range(1, 80, 2):
  print(x)
Posted by: Guest on January-19-2020
17

for in python

for i in range(1,10,2): #(initial,final but not included,gap)
  print(i); 
  #output: 1,3,5,7,9
  
for i in range (1,4): # (initial, final but not included)
  print(i);
  #output: 1,2,3 note: 4 not included

for i in range (5):
  print (i);
  #output: 0,1,2,3,4 note: 5 not included

python = ["ml","ai","dl"];  
for i in python:
  print(i);
  #output:  ml,ai,dl
  
for i in range(1,5):	#empty loop...if pass not used then it will return error
  pass;
Posted by: Guest on May-18-2020
9

python for loop

#to print number from 1 to 10
for i in range(1,11):
    print(i)

#to print a list
l = [1,2,3,4]
for i in l:
    print(i)

r = ["a","b","c","d","e"]
s = [1,2,3,4,5]

#print two list with the help of zip function
for p,q in zip(r,s):
    print(p,q)
Posted by: Guest on December-04-2020
2

python for loop

a = 10 
for i in range(1, 10) # the code will move from the range from 1 to 10 but not including 10
	print(i)
Posted by: Guest on January-17-2021
4

python for loop

# A for loop in python is used to iterate (or loop) over a sequence.
# The sequence is usually range(start,end), but can also be a list.
# Example of for loop with range():
for num in range(1,10):
  print(num)
  # This prints all the numbers from 1 to 10, including 1 but excluding 10.

 # Here is an example of a for loop with a list:
lst = [1,2,3,4,5]
for item in lst:
  print(item)
  # This prints all items in the list.

# For loops are also used in list comprehensions.
lst = [1,2,3,4,5]
new_lst = [item for item in lst if i % 2 == 0]
# The list comprehension above generates a new list, which new_lst is put equal to.
# It iterates through the items in lst and only puts them in the new list if it is a multiple of 2.
Posted by: Guest on January-04-2021
1

python for loop

# Python for loop

for i in range(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
Posted by: Guest on January-09-2021

Python Answers by Framework

Browse Popular Code Answers by Language