Bucla while

Python intermediar

Hugo Bowne-Anderson

Data Scientist at DataCamp

if-elif-else

control.py

  • Parcurge structura o singură dată!
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
  • Bucla while = instrucțiune if repetată
Python intermediar

While

while condition :
    expression
  • Calcularea numerică a unui model
  • „repetarea unei acțiuni până la îndeplinirea condiției"
  • Exemplu
    • Eroarea inițială este 50
    • Eroarea se împarte la 4 la fiecare iterație
    • Continuă până când eroarea nu mai este > 1
Python intermediar

While

while condition :
    expression

while_loop.py

error = 50.0


while error > 1:
error = error / 4
print(error)
  • Eroarea inițială este 50
  • Eroarea se împarte la 4 la fiecare iterație
  • Continuă până când eroarea nu mai este > 1
Python intermediar

While

while condition :
    expression

while_loop.py

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

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 intermediar

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 intermediar

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 intermediar

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: sesiune deconectată
  • Sistem local: Control + C
Python intermediar

Să exersăm!

Python intermediar

Preparing Video For Download...