Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ The First for Loop | Section
Python Loops
セクション 1.  1
single

single

bookThe First for Loop

メニューを表示するにはスワイプしてください

Note
Definition

Loop is a series of instructions that continuously repeats until a specific condition is met. Loops are essential for automating repetitive tasks and efficiently processing data.

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

for item in sequence:
    # Perform some operation
  • 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.

1234567
word = 'iteration' letters = [] # Adding every letter in the word to the list for letter in word: letters.append(letter) print(letters)
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.

タスク

スワイプしてコーディングを開始

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.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  1
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt