Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære The First For Loop | The For Loop
Python Loops Tutorial
course content

Kursusindhold

Python Loops Tutorial

Python Loops Tutorial

1. The For Loop
2. The while Loop
3. Nested Loops
4. List and Dictionary Comprehensions

book
The First For Loop

Using loops you can iterate over sequences like lists, strings, or numerical ranges, they allow to process large amounts of data with minimal code.

py
  • item is a variable that takes the value of each element in the sequence one at a time;
  • sequence is the data you are iterating through, such as a list, string, or range;
  • for statement block is executed for every item in the sequence.

Imagine you have a string variable and want to print each letter of it in a column. Since a string is a sequence of letters, you can use a loop to achieve this.

123456
word = 'iteration' letters = [] # Adding every letter in the word to the list for letter in word: letters.append(letter)
copy
  • The word variable holds the string 'iteration'.
  • The for loop iterates over each character in the string.
  • Each character is appended to the letters list in each iteration.
  • After the loop, letters contains all the characters from 'iteration' as individual elements.

Make sure to name the item variable meaningfully. For example, if you iterate through a list called people, the appropriate variable name should be person.

Opgave

Swipe to start coding

You are a traveler who wants to create a travel list. You have a list of countries and need to add them to your travel list.

  • Iterate through the countries list using a for loop.
  • Update travel_list so that it contains only the countries from countries.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1
toggle bottom row

book
The First For Loop

Using loops you can iterate over sequences like lists, strings, or numerical ranges, they allow to process large amounts of data with minimal code.

py
  • item is a variable that takes the value of each element in the sequence one at a time;
  • sequence is the data you are iterating through, such as a list, string, or range;
  • for statement block is executed for every item in the sequence.

Imagine you have a string variable and want to print each letter of it in a column. Since a string is a sequence of letters, you can use a loop to achieve this.

123456
word = 'iteration' letters = [] # Adding every letter in the word to the list for letter in word: letters.append(letter)
copy
  • The word variable holds the string 'iteration'.
  • The for loop iterates over each character in the string.
  • Each character is appended to the letters list in each iteration.
  • After the loop, letters contains all the characters from 'iteration' as individual elements.

Make sure to name the item variable meaningfully. For example, if you iterate through a list called people, the appropriate variable name should be person.

Opgave

Swipe to start coding

You are a traveler who wants to create a travel list. You have a list of countries and need to add them to your travel list.

  • Iterate through the countries list using a for loop.
  • Update travel_list so that it contains only the countries from countries.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt