For and while in Python

by | Jul 15, 2017 | Python-example | 0 comments

Example: traversing a string with a FOR

seq = "Diego"
for letter in seq:
    print("Letter :", letter)
Letter : D
Letter : i
Letter : e
Letter : g
Letter : o

Example: traversing an array with a FOR

data = ["pears", "apples",  "bananas"]
for value in data:
    print("Value :", value)
Value : pears
Value : apples
Value : bananas

Example: traversing a loop to a condition with WHILE

i = 0
while (i < 9):
   print ("Numbers:", i)
   i = i + 1
Numbers: 0
Numbers: 1
Numbers: 2
Numbers: 3
Numbers: 4
Numbers: 5
Numbers: 6
Numbers: 7
Numbers: 8

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *