LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Lesson 20: while Loops In Python
Generally loop is used when we don't know the number of times to iterate beforehand. The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.With the while loop we can execute a set of statements as long as a condition is true.
i = 1 while i < 10: print(i) i += 1 output will be 1 2 3 4 5 6 7 8 9Note: Remember to increment i, or else the loop will continue forever.
Using a While Loop: You can also loop through the list items by using a while loop.
Use the len() function to determine the length of the list, then start at 0 and loop your way through the list items by refering to their indexes. Remember to increase the index by 1 after each iteration.
Example: Print all items, using a while loop to go through all the index numbers.
output will be
apple
banana
cherry
grapes
The break Statement
Example: break statement we can stop the loop even if the while condition is true.
output will be
1
2
3
4
The continue Statement
Example: Continue to the next iteration if i is 4.
output will be
1
2
3
5
6
7
The else Statement
Example: Print a message once the condition is false.
output will be
1
2
3
4
5
6
i is no longer less than 6
How to Loop Through A List Using List Comprehension?
List Comprehension offers the shortest syntax for looping through lists.
Example: A short hand for loop that will print all items in a list.
listA = ["apple", "banana", "cherry", "grapes"]
[print(x) for x in listA]
output will be
apple
banana
cherry
grapes
More About List Comprehension
List comprehension offers a shorter syntax when you want to create a new list based on the
values of an existing list.
For instane based on a list of fruits, you want a new list, containing only the fruits with the
letter "a" in the name. Without list comprehension you will have to write a for statement with a
conditional test inside.
But with list comprehension you can do all that with only one line of code.
Example
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
output will be
['apple', 'banana', 'mango']
Important to Remember
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
When we say that lists are ordered, it means that the items have a defined order, and that order
will not change. If you add new items to a list, the new items will be placed at the end of the list.
Changeable: The list is changeable, meaning that we can change, add, and remove items in a list after
it has been created.
Since lists are indexed, lists can have items with the same value i.e. lists Allow Duplicates.
Example
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
output will be
['apple', 'banana', 'cherry', 'apple', 'cherry']
Python Collections (Arrays): There are four collection data types in the Python programming language.
List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
Dictionary is a collection which is ordered** and changeable. No duplicate members.