Python Primer 103
Loops
AL NAFI,
A company with a focus on education,
wellbeing and renewable energy.
© 2018 Al-Nafi. All Rights Reserved. 1
Dua of the day
The righteous dua that Abu Bakr (r) made at the end of his lifetime.
“O Allah, let the best of my lifetime be its ending, and my best deed be that which I seal [my life
with], and the best of my days the day I meet You.”
Reference: The Dua is taken from Sheikh Omer Sulaiman Dua Compilation.
© 2018 Al-Nafi. All Rights Reserved. 2
Study, Rinse and Repeat
• Please subscribe to our YouTube Channel to be on top of your studies.
• Please logon to our website https://alnafi.com/login/
• Use your username and password to logon
• Please keep an eye on
[email protected] emails
• Please review the videos of 30 minutes daily
• Please review the notes daily.
• Please take time for clearing up your mind and reflect on how things
are proceeding in your studies.
© 2018 Al-Nafi. All Rights Reserved. 3
© 2018 Al-Nafi. All Rights Reserved. 4
Using Loops
To print Salam five times in Python, you could do the following:
print(“Salam")
print(“Salam")
print(“Salam")
print(“Salam")
print(“Salam")
© 2018 Al-Nafi. All Rights Reserved. 5
Python Loops
Use for while
creating loops
and for
© 2018 Al-Nafi. All Rights Reserved. 6
The Range function can be used to create a list of numbers
ranging from a starting number up to the number just before the
ending number.
That may sound a little confusing. Let’s combine the range function
with the list function to see exactly how this works.
© 2018 Al-Nafi. All Rights Reserved. 7
But this is rather tedious. Instead, you can use a for loop to reduce the
amount of typing and repetition, like this:
for x in range(0, 5):
print('salam')
© 2018 Al-Nafi. All Rights Reserved. 8
Using range and list together
print(list(range(10, 20)))
© 2018 Al-Nafi. All Rights Reserved. 9
In the case of the for loop, the code at is actually telling Python to do
the following:
Start counting from 0 and stop before reaching 5.
For each number we count, store the value in the variable x.
for x in range(0, 5):
print('salam %s' %x)
© 2018 Al-Nafi. All Rights Reserved. 10
If we remove the for then what happens!
x=0
print('salam %s' % x)
x=1
print('salam %s' % x)
x=2
print('salam %s' % x)
x=3
print('salam %s' % x)
x=4
print('salam %s' % x)
© 2018 Al-Nafi. All Rights Reserved. 11
Not using range and list
wizard_list = ['spider legs','toe of frog', 'snail tongue',
'bat wing', 'slug butter', 'bear burp']
for i in wizard_list:
print(i)
© 2018 Al-Nafi. All Rights Reserved. 12
Without the loop we have a long code
wizard_list = ['spider legs', 'toe of frog', 'snail tongue',
'bat wing', 'slug butter', 'bear burp']
print(wizard_list[0])
print(wizard_list[1])
print(wizard_list[2])
print(wizard_list[3])
print(wizard_list[4])
print(wizard_list[5])
© 2018 Al-Nafi. All Rights Reserved. 13
Printing it twice
hugehairypants = ['huge', 'hairy', 'pants']
for i in hugehairypants:
print(i)
print(i)
© 2018 Al-Nafi. All Rights Reserved. 14
Mind the gap
© 2018 Al-Nafi. All Rights Reserved. 15
hugehairypants = ['huge', 'hairy', 'pants']
for i in hugehairypants:
print(i)
print(i)
© 2018 Al-Nafi. All Rights Reserved. 16
Gaps or spaces needs to be consistent
hugehairypants = ['huge', 'hairy', 'pants']
for i in hugehairypants:
print(i)
for j in hugehairypants:
print(j)
© 2018 Al-Nafi. All Rights Reserved. 17
Another kind of loop while
A for loop isn’t the only kind of loop you can make in Python. There’s
also the while loop.
Afor loop is a loop of a specific length, whereas a while
loop is a
loop that is used when you don’t know ahead of time when it needs to
stop looping.
© 2018 Al-Nafi. All Rights Reserved. 18
x = 45
y = 80
while x < 50 and y < 100:
x=x+1
y=y+1
print(x, y)
© 2018 Al-Nafi. All Rights Reserved. 19
ه للا جزاك
Please send us your questions at
[email protected]We will only answer our Nafi Members. So please
quote your membership number within the email.
© 2018 Al-Nafi. All Rights Reserved. 20