0% found this document useful (0 votes)
5 views

Basic_Python_3

The document provides an overview of looping techniques in Python, specifically focusing on for loops and while loops. It explains the use of the range() function, as well as the continue and break statements, and introduces the concept of nested loops. Additionally, it includes quiz questions to reinforce understanding of the material presented.

Uploaded by

Akaruku Media
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Basic_Python_3

The document provides an overview of looping techniques in Python, specifically focusing on for loops and while loops. It explains the use of the range() function, as well as the continue and break statements, and introduces the concept of nested loops. Additionally, it includes quiz questions to reinforce understanding of the material presented.

Uploaded by

Akaruku Media
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Python Loop

For Loop
& While
Loop

Page 01
Content

For Loop While Loop

Understanding how for loop Understanding other choices of


works in python. looping technique in python.
❖ Range ❖ Continue
❖ Program Example ❖ Break
❖ Nested Loop

Page 02
Python: For Loop

Python has two loop commands:


1. for loops
2. while loops

A for loop is used for iterating over a sequence like below.

fruits = ["apple", "banana", "cherry"]


for x in fruits: # prints out apple, banana, cherry
print(x)

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

What is output from this code:

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]

You might also like