Examples of for loop in Python programming

0

loop is used to iterate over a sequence (list, tuple, string) or other iterable objects.

Example :

for val in sequence:
    loop body

All Contributions

using break statement, go out from loop when reaching the element "banana" in the fruit's list

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
  if x == "banana":
    break

print the characters in a string, character by character

for x in "banana":
  print(x)

print list contents in python using for loop

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

print english letters in small case

#print all english small letters
for i in range(97,123):
    print(chr(i))

print odd numbers between 1 to 100

#print all odd numbers between 1 to 100
for i in range(1, 101):
    if i%2!=0:
        print(i)

total contributions (14)

New to examplegeek.com?

Join us