“Как создать петлю в Python” Ответ

Питон для петли

# 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

Питон для петли

# 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

my_list = [1,2,3]
for number in my_list:
	print(number)
#outputs
#1
#2
#3
not_A_Developer

Питон для петли

words=['zero','one','two']
for operator, word in enumerate(words):
	print(word, operator)
Tremendous Enceladus

Питон для петли

#for loop without passing a argument

for _ in range(3):
  print("Hello")
Combative Crocodile

Питон для петли

n = int(input("All numbers in given range"))
for i in range(n):
  print(i)
Evil Eland

Ответы похожие на “Как создать петлю в Python”

Вопросы похожие на “Как создать петлю в Python”

Больше похожих ответов на “Как создать петлю в Python” по Python

Смотреть популярные ответы по языку

Смотреть другие языки программирования