Home » Python Programming » Python Programming – For Loop and Range

Python Programming – For Loop and Range

All sequences are made up of elements. In the below example, each letter is an element making up the sequence.
The for loop marches through (iterates) a sequence one element at a time.

# Code Example:
word = input(“Enter a word: “)
print (“\nHere’s each letter in your word:”)
for letter in word:
print(letter)

Range function within a For Loop:
You can use a for loop to count. This example provides a range(), telling the for loop how many times to count.

# Code Example:
print (“Counting:”)
for i in range(100):
print(i,””, end=””)

When you give range 3 values;
Start point, End point, and Number to count by

# Code Example:
print (“\n\nCounting by fives:”)
for i in range(0,50,5):
print(i,””, end=””)

# Counting Backwords

# Code Example:
print (“\n\nCounting by ones:”)
for i in range(10,0,-1):
print(i,””, end=””)


Leave a comment

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