for loop as variable python code example

Example 1: how to only print final iteration of a for loop pyhton

prices = [10, 20, 30]
total = 0
for price in prices:
    total += price
	#print(f'total: {total}') will give all iterations in order
print(f'total: {total}') #make sure to print outside of loop for 
#only final iteration to print

Example 2: python for loop iterator

import numpy as np
# With array cycling
arr = np.array([1,2,3,4,5,6,7,8,9])

for i in range(len(arr)):
 # logic with iterator use (current logic replaces even numbers with zero)
    if arr[i] % 2 == 0: arr[i] = 0

print(arr)
# Output: [1, 0, 3, 0, 5, 0, 7, 0 , 9]