Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ List Methods | Other Data Types
/
Introduction to Python
セクション 4.  2
single

single

bookList Methods

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

List Methods

Python provides several methods that you can use to manipulate lists. These methods make it easy to modify, search, and manage lists effectively.

Let's explore some of the most commonly used methods:

  • append(): adds an item to the end of the list;
  • remove(): removes the first occurrence of an item from the list;
  • sort(): sorts the items of the list in ascending (or descending) order.
Note
Note

To use list methods in Python, you must invoke them on a list object using dot notation. This involves appending the method name to the list name followed by parentheses, as shown here: list_name.append("new element").

The next example will demonstrate how to apply various list methods using dot notation.

Imagine you need to update your store's inventory by adding new items and removing outdated ones.

Here's how you can do it using list methods:

1234567891011121314
# Creating an inventory inventory = ["carrots", "bananas", "apples"] # Adding a new item inventory.append("oranges") # Removing an outdated item inventory.remove("bananas") # Sorting the inventory inventory.sort() # Checking the result print("Updated inventory:", inventory)
copy
タスク

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

You are managing a grocery store's inventory. Use the correct list methods to update the inventory as described below:

  • Add "apples" to the inventory list;
  • Remove "bread" from the inventory list;
  • Sort the inventory list in alphabetical order.

Make sure to use the appropriate list methods for each step. Do not create a new list; modify the existing inventory list directly.

解答

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

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

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

セクション 4.  2
single

single

AIに質問する

expand

AIに質問する

ChatGPT

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

some-alt