Contenido del Curso
Python Loops Tutorial
Python Loops Tutorial
If/Else in a while Loop
We previously used the if/else
structure in conjunction with for
loops, and we can apply the same concept to while
loops.
An if/else
structure within a while
loop functions when the while
condition becomes False
.
Examine the code below:
numbers = [1, 2, 4, 5, 7] i = 0 # Checking even and odd while i < len(numbers): if numbers[i] % 2 == 0: print(numbers[i], 'is an even number') i += 1 else: print(numbers[i], 'is an odd number') i += 1
How does the code work?
Tarea
Multiply all the negative numbers in the list.
- Initialize a
while
loop, utilizingi
to interact withnumbers
. - Establish the condition if an element is less than 0.
- Multiply the element and accumulate it in the
counter
. - Update the value of
i
to change the list index. - Display the value stored in the
counter
.
¡Gracias por tus comentarios!
If/Else in a while Loop
We previously used the if/else
structure in conjunction with for
loops, and we can apply the same concept to while
loops.
An if/else
structure within a while
loop functions when the while
condition becomes False
.
Examine the code below:
numbers = [1, 2, 4, 5, 7] i = 0 # Checking even and odd while i < len(numbers): if numbers[i] % 2 == 0: print(numbers[i], 'is an even number') i += 1 else: print(numbers[i], 'is an odd number') i += 1
How does the code work?
Tarea
Multiply all the negative numbers in the list.
- Initialize a
while
loop, utilizingi
to interact withnumbers
. - Establish the condition if an element is less than 0.
- Multiply the element and accumulate it in the
counter
. - Update the value of
i
to change the list index. - Display the value stored in the
counter
.
¡Gracias por tus comentarios!
If/Else in a while Loop
We previously used the if/else
structure in conjunction with for
loops, and we can apply the same concept to while
loops.
An if/else
structure within a while
loop functions when the while
condition becomes False
.
Examine the code below:
numbers = [1, 2, 4, 5, 7] i = 0 # Checking even and odd while i < len(numbers): if numbers[i] % 2 == 0: print(numbers[i], 'is an even number') i += 1 else: print(numbers[i], 'is an odd number') i += 1
How does the code work?
Tarea
Multiply all the negative numbers in the list.
- Initialize a
while
loop, utilizingi
to interact withnumbers
. - Establish the condition if an element is less than 0.
- Multiply the element and accumulate it in the
counter
. - Update the value of
i
to change the list index. - Display the value stored in the
counter
.
¡Gracias por tus comentarios!
We previously used the if/else
structure in conjunction with for
loops, and we can apply the same concept to while
loops.
An if/else
structure within a while
loop functions when the while
condition becomes False
.
Examine the code below:
numbers = [1, 2, 4, 5, 7] i = 0 # Checking even and odd while i < len(numbers): if numbers[i] % 2 == 0: print(numbers[i], 'is an even number') i += 1 else: print(numbers[i], 'is an odd number') i += 1
How does the code work?
Tarea
Multiply all the negative numbers in the list.
- Initialize a
while
loop, utilizingi
to interact withnumbers
. - Establish the condition if an element is less than 0.
- Multiply the element and accumulate it in the
counter
. - Update the value of
i
to change the list index. - Display the value stored in the
counter
.