# How To use for Loop Function in python !

What is for loop – In computer programming, a for loop is a sequence of instruction s that is continually repeated until a certain condition is reached. Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number.and for loop in Python is used to iterate over a sequence (listtuplestring).

Flowchart of for Loop: Now, Lets some example for loop.

So, you have a list of the numbers and sum of the numbers into the list with for loop.

Example-1

testnum = [1, 2, 3, 4, 5, 6, -7, 8, 9, 10]  ## list of num for sum
sumof=0 ## Variable for store the sum

for x in testnum:
sumof += sumof+x  ## iterate over the list
print(“Total sum is”, sumof)

Output:-
Total sum is 41

Range() Function:

`Range()` is a built-in function of Python.It is used when a user needs to perform an action for a specific number of times.We can generate a sequence of numbers using `range()` function. `range(100)` will generate numbers from 0 to 99 (100 numbers).
In simple terms, `range()` allows user to generate a series of numbers within a given range. Depending on how many arguments user is passing to the function, user can decide where that series of numbers will begin and end as well as how big the difference will be between one number and the next.

Following Example:

Example-1

print(range(5)) ## Output is range(0, 5)
print(list(range(5))) ## Output is [0, 1, 2, 3, 4]
print(list(range(1,5))) ## Output is [1, 2, 3, 4]
print(list(range(1,15,3))) ## Output is [1, 4, 7, 10, 13]

Now, We can use range() function in for loop.

Example-2 ## this is a without use range function example

fruits = [“Apple”, “Orange”, “Mango”] ## a list

for fruit in fruits:
print(“i like”,fruit)

Output:-

i like Apple
i like Orange
i like Mango

Example-3 ## with use range function

fruits = [“Apple”, “Orange”, “Mango”]

for a in range(len(fruits)): ## use len fuction for convert integers
print(“i like”,fruits[a]) ## you can see ‘fruits’ list and [a] is starting with 0,1,2 because we have 3 items in a list , so when you hit run without ‘fruits’ you see a number instead fruits name.
Output-3 ##without ‘fruits’ variable print print(“i like”,a)
i like 0 ## a list 0 called apple
i like 1 ## called orange
i like 2 ## called mango

Output-3.1 ## with’fruits’ variable print print(“i like”,fruits[a])

i like Apple
i like Orange
i like Mango

Now, for loop with use else statement:

Example – 4

number = [11, 22, 33, 44, 55]
for x in number:
print(x)
else:
print(“the end”)

Output – 4

11
22
33
44
55
the end

if , you use break function before else then :
number = [11, 22, 33, 44, 55]
for x in number:
print(x)
break ## a function build in python
else:
print(“the end”)

Output-4.1
11 ## when use break function then No Output show after 11.

for loop with use if/else statement:

Example – 5
Make a Program with if/else statement

for i in range(1, 16):
if i % 3 ==0 and i % 5 == 0:
print(“Fizzbuzz”)
elif i % 3 == 0:
print(“Fizz”)
elif i % 5 == 0:
print(“buzz”)
else:
print(“-“)

Output – 5

1
2
Fizz
4
buzz
Fizz
7
8
Fizz
buzz
11
Fizz
13
14
Fizzbuzz

Thank you ❤️

Check out – Shell Script to take backup of Application