xxxxxxxxxx
list_1 = [9, 5, 7, 2, 5, 3, 8, 14, 6, 11]
for i in range(0, len(list_1), 2) :
print(list_1[i])
2 is the steps
output:
9
7
5
8
6
xxxxxxxxxx
# 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)
xxxxxxxxxx
# 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')
xxxxxxxxxx
for c in "banana":
print(c)
xxxxxxxxxx
scores = [70, 60, 80, 90, 50]
filtered = []
for score in scores:
if score >= 70:
filtered.append(score)
print(filtered)
Code language: Python (python)
xxxxxxxxxx
words=['zero','one','two']
for operator, word in enumerate(words):
print(word, operator)