perulangan while

Python Tingkat Menengah

Hugo Bowne-Anderson

Data Scientist at DataCamp

jika-elif-jika tidak

control.py

  • Hanya melewati konstruksi sekali saja!
z = 6
if z % 2 == 0 :  # True
    print("z is divisible by 2")  # Executed
elif z % 3 == 0 :
    print("z is divisible by 3")
else :
    print("z is neither divisible by 2 nor by 3")

...   # Moving on
  • Perulangan while = pernyataan if yang diulang-ulang
Python Tingkat Menengah

While

while condition :
    expression
  • Model perhitungan numerik
  • "mengulangi tindakan hingga kondisi terpenuhi"
  • {{3}}Contoh
    • Error dimulai pada 50
    • Membagi error dengan 4 pada setiap eksekusi.
    • Lanjutkan hingga error tidak lagi > 1
Python Tingkat Menengah

While

while condition :
    expression

while_loop.py

error = 50.0


while error > 1:
error = error / 4
print(error)
  • Error dimulai pada 50
  • Bagi error dengan 4 pada setiap eksekusi.
  • Lanjutkan hingga error tidak lagi > 1
Python Tingkat Menengah

While

while condition :
    expression

while_loop.py

error = 50.0
#     50
while error > 1:    # True
      error = error / 4
      print(error)
12.5
Python Tingkat Menengah

While

while condition :
    expression

while_loop.py

error = 50.0
#     12.5
while error > 1:    # True
      error = error / 4
      print(error)
12.5
3.125
Python Tingkat Menengah

While

while condition :
    expression

while_loop.py

error = 50.0
#     3.125
while error > 1:    # True
      error = error / 4
      print(error)
12.5
3.125
0.78125
Python Tingkat Menengah

While

while condition :
    expression

while_loop.py

error = 50.0
#     0.78125
while error > 1:    # False
    error = error / 4
    print(error)
12.5
3.125
0.78125
Python Tingkat Menengah

While

while condition :
    expression

while_loop.py

error = 50.0
while error > 1 :    # always True
    # error = error / 4
    print(error)
50
50
50
50
50
50
50
...
  • DataCamp: Sesi terputus
  • Sistem lokal: Ctrl + C
Python Tingkat Menengah

Ayo berlatih!

Python Tingkat Menengah

Preparing Video For Download...