“Python pour la boucle” Réponses codées

Python pour la boucle

# 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
Unknown Species

Python pour la boucle

for i in range(1.10):
	print(i)
    
#output: 1,2,3,4,5,6,7,8,9,10
AMXX

Python pour la boucle

for x in range(6): #The Loop
  print(x) #Output 0,1,2,3,4,5
Vasilije Dimitrijevic

Python pour la boucle

# if you want to get items and index at the same time,
# use enumerate
fruits = ['Apple','Banana','Orange']

for indx, fruit in enumerate(fruits):
	print(fruit, 'index:', indx)
Short Circuit

Python pour la boucle

# 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 in range(len(values)):
    print(i, values[i])

# For loop with enumerate
# Provides a cleaner syntax
print('\nFor loop using builtin enumerate():')
for i, value in enumerate(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 tuple
print('\nAlternate method of using the for loop with builtin enumerate():')
for index_value in enumerate(values):
    print(index_value)

# Results for index_value for loop:
# (0, 'a')
# (1, 'b')
# (2, 'c')
# (3, 'd')
# (4, 'e')
YEP Python

Python pour la boucle

for x in range(10):
  print(x)
The Rabbit

Python pour la boucle

for i in range(27)
	print(i)
FlashingMustard

Python pour la boucle

 for item in ['mosh','john','sarah']:
    print(item)
Frightened Ferret

Python pour la boucle

for c in "banana":
    print(c)
Nervous Nightingale

Python pour la boucle

for a in range(10):
  	print(a)
Determined Dolphin

Réponses similaires à “Python pour la boucle”

Questions similaires à “Python pour la boucle”

Plus de réponses similaires à “Python pour la boucle” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code