Course Content
Python Loops Tutorial
Python Loops Tutorial
List & for Loop
We can employ various data structures when working with loops. So far, we've covered strings, and now we'll explore working with a list of numbers.
Examine the following code:
How does the code work?
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/1_4_1_njnjr.gif)
To work with a decreasing list, you can utilize the reversed()
function. This function allows you to reverse the order of elements in a list, effectively turning it into a decreasing list.
How does the code work?
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/1_4_2_djkdk.gif)
Task
To sum the elements inside the list, follow these steps:
- Configure the
for
loop to operate on thenumbers
, utilizingi
as an element of the list. - Calculate the sum by adding elements to the
counter
within the loop. - Print the value of the
counter
after the loop has completed.
Everything was clear?
Course Content
Python Loops Tutorial
Python Loops Tutorial
List & for Loop
We can employ various data structures when working with loops. So far, we've covered strings, and now we'll explore working with a list of numbers.
Examine the following code:
How does the code work?
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/1_4_1_njnjr.gif)
To work with a decreasing list, you can utilize the reversed()
function. This function allows you to reverse the order of elements in a list, effectively turning it into a decreasing list.
How does the code work?
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/1_4_2_djkdk.gif)
Task
To sum the elements inside the list, follow these steps:
- Configure the
for
loop to operate on thenumbers
, utilizingi
as an element of the list. - Calculate the sum by adding elements to the
counter
within the loop. - Print the value of the
counter
after the loop has completed.
Everything was clear?