Skip to content

Latest commit

 

History

History
47 lines (34 loc) · 587 Bytes

Basic_forloop_application.md

File metadata and controls

47 lines (34 loc) · 587 Bytes

Prints out the numbers 0, 1, 2, 3, 4

for x in range(5):
   print(x)

Prints out 3, 4, 5

for x in range(3, 6):
    print(x)

Prints out 3, 5, 7

for x in range(3, 8, 2):
    print(x)

example

  • Program to find the sum of all numbers stored in a list

  • List of numbers

numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
  • variable to store the sum
sum = 0
  • iterate over the list
for val in numbers:
   sum = sum+val
  • Output: The sum is 48
print("The sum is", sum)