Python While Loop - TECH WORTHY MIND
Most of the programming languages support the concept of "While Loop" and Python is one of them.
Here in this article, we will discuss how we can use While Loop in Python.
The While Loop
With the while loop, we can execute a set of items as long as the condition is true.
We can loop through the elements of set, tuple, dictionary, etc by using the While loop.
Note- we can loop through the item of the set, only first of all convert set into a tuple/list and then access the elements of that tuple and again convert that list/tuple into the set.
In Python, we use the len function to determine the length of the list.
mylist=["apple","banana","mango"]
print(len(mylist))
Output: 3
So, by using this concept we can access the elements of the list by using While Loop
Example-
mylist=["apple","banana","mango"]
I=0
while I<len(mylist):
print(mylist)
I=I+1
Guessing Game
Secret_number= 15
I=0
while I<=3:
guess=int(input ("Guess:")
I+=1
if guess==I:
print("You Win!")
break
else:
print("Sorry, You fail")
Generate the "*" pattern by using while loop
i=1
while i<=5:
print('*'*i)
i=i+1
print("Done!")
Output:
*
**
***
****
*****
Done!
Comments
Post a Comment
DON'T COMMENT LINK.