You are on page 1of 2

Looping

Loop while

1. n = 1
while n < 5:
print (n)
n = n + 1
hasil:

2. num = float (input("Enter a positive number"))

while num <= 0:


    print ("That's not a positive number!")
    num = float(input("Enter a positive number: "))
hasil:
Enter a positive number

For loop

1. for letter in "Python":


print(letter)
hasil:
p
y
t
h
o
n

2. versi while
word = "python"
index = 0

while index < len(word):


    print(word[index])
    index = index + 1
hasil:
p
y
t
h
o
n
3. range() untuk for loop
for n in range (3):
    print("Python")
hasil:
python
python
python

4. for loop dengan range ()


for n in range (10,20):
    print(n*n)
hasil:
100
121
144
169

You might also like