OneBite.Dev - Coding blog in a bite size

use a basic loop in python

Code snippet on how to use a basic loop in python

  numbers = [5, 3, 2, 10]
  total = 0
  for num in numbers:
    total += num
  print('Total:', total)

This code uses a for loop to iterate through a list of numbers, adding each number to the total. The code begins by creating a list of numbers and initializing a total to 0. The loop then starts, with num taking the value of each item in the list. In each loop iteration, the value of num is added to the total. Finally, the total is printed. In this example, the output will be “Total: 20”.

python