Basic_Python_3
Basic_Python_3
For Loop
& While
Loop
Page 01
Content
Page 02
Python: For Loop
Page 03
Python: Range
The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and stops before a specified number. The keyword is
range(start, stop, step).
x = range(6)
for n in x: # prints out 0, 1, 2, 3, 4, 5
print(n)
x = range(3, 6)
for n in x: # prints out 3, 4, 5
print(n)
x = range(3, 20, 2)
for n in x: # prints out 3, 5, 7, 9, 11, 13, 15, 17, 19
print(n)
Page 04
Quiz Session
Page 05
Quiz
Create program that receive n person name, then print all of the name.
Where n is in integer.
Buatlah program yang menerima nama orang sebanyak n, kemudian print setiap
namanya.
n adalah integer.
Page 06
Python: While Loop
With the while loop we can execute a set of statements as long as a condition is true.
while True:
# execute this
i=1
while i < 6:
print(i)
i += 1
Page 07
Python: Continue
With the continue statement we can stop the current iteration, and continue with the
next.
for i in range(5):
if i == 3:
continue
print(n)
Page 08
Python: Break
With the break statement we can stop the loop even if the while condition is true.
for i in range(5):
if i == 3:
break
print(n)
Page 09
Python: Nested Loop
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for each iteration of the "outer loop".
for i in range(2):
for j in range(3):
print("i:{},j:{}".format(i,j) ,end=" ")
print()
Page 10
Quiz Session
Page 11
Quiz
x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 5, 6]
z=0
for i in x :
for j in y :
if i == j :
z =z+1
print(z)
Page 12
Terima Kasih!
Indonesia AI | AI for Everyone, AI for Indonesia
[email protected]