Please Subscribe My YouTube Channel

Friday, February 10, 2023

For access list items/values using for loop and while loop.


For access list items/values using for loop and while loop.

Python for Loop

In this blog we'll learn about to use a for loop in Python.
In computer programming, loops are used to repeat a block of code.
For example, if we want to show a message 50 or more  times, then we can use a loop.
There are 2 types of loops in Python: for loop and while loop
Python for Loop
In Python, the for loop is used to run a block of code for a certain number of times. It is used to iterate over any sequences such as list, tuple, string, etc. Loop continues until we reach the last item in the sequence.

Flowchart of Python for Loop

Working of Python for loop 


Example: Loop over Python List
os = ['mac', 'window', 'linux', 'unix']

#Here we can access items value of a list using for loop
for i in os:
    print(i)

#OUTPUT:-
mac
window
linux
unix

Python for Loop with Python range()

A range is a series of values between two numeric intervals.
Here we use a built-in function range() to define a range of values.
For example,
values = range(5)
Here, 5 inside range() defines a range containing values 0, 1, 2, 3 and 4.

In Python, we can use for loop to iterate over a range.
For example:-
# use of range() to define a range of values
values = range(5)

# iterate from i = 0 to i = 4
for i in values:
    print(i)
Result:- 
0
1
2
3
4
In the above example, we have used the for loop to iterate over a range from 0 to 4.
The value of i is set to 0 and it is updated to the next number of the range on each iteration. This process continues until 4 is reached.

Python for loop with else
A for loop can have an optional else block as well. The else part is executed when the loop is finished. 
For example:-
listb = [0, 3, 6]
for i in listb:
    print(i)
else:
    print("Items or value not found in the listb")

#Result:-
0
3
6
Items or value not found in the listb.
Here, the for loop prints all the items of the listb. When the loop finishes, it executes the else block and prints ‘Items or value not found in the listb’.


Python while Loop

Python while loop is used to run a block code until a certain condition is met or finded. A while loop evaluates the condition. If the condition evaluates to True, the code inside the while loop is executed, condition is evaluated again. This process continues until the condition is False, and when condition evaluates to False, the loop will stop.

Flowchart of Python while Loop




Example: Python while Loop
# program to display numbers from 1 to 5

# here we initialize two variable i and n
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1

#Output:-
1
2
3
4
5

Example 2: Python while Loop

# program to calculate the sum of numbers
# until the user enters zero

total = 0

number = int(input('Enter a number: '))

# add numbers until number is zero
while number != 0:
    total += number    # total = total + number
    
    # take integer input again
    number = int(input('Enter a number: '))
    

print("total is =", total)


#output:-
Enter a number: 18
Enter a number: 6
Enter a number: -5
Enter a number: 0
('total is =', 19)

In the above example, the while iterates until the user enters zero. When the user enters zero, the test condition evaluates to False and the loop ends or stop.

Infinite while Loop with else in Python

If the condition of a loop is always True, the loop runs for infinite times (until the memory is full). For example,
age = 30
# the test condition is always True
while age > 18:
    print('You are eligible for vote')
else:
    print('You are not eligible for vote')

#if you change age less than 18 then it will else condition.

In the above example, the condition always evaluates to True. Hence, the loop body will run for infinite times.

Example 2:-
counter = 0

while counter < 3:
    print('we are looping')
    counter = counter + 1
else:
    print('we stop bcz value is >= 3')