Python Crash Course - Chapter 3: Lists

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

How are lists indicated in Python?

In Python, square brackets ([]) indicate a list, and individual elements in the list are separated by commas.

How do you remove an item from a Python list when the list item index is known?

When you want to remove an item from a list and the position is known, you can use the del statement. Example: motorcycles = ['honda','yamaha','suzuki'] del motorcycles[0]

What is a Python list?

A list is a collection of items in a particular order.

How do you tell Python which item in a list to access?

Access any element in a list by telling Python the position, or index, of the item desired. To access an element in a list, write the name of the list followed by the index or the item closed in square brackets (e.g., print(bicycles[0]) for position one). Index positions start at 0, not 1.

What is the special syntax Python has for accessing the last element in the list?

By asking for the item at index -1, Python always returns the last item in the list. The convention extends to other uses of a negative index number.

What does the pop method do to a list in Python?

By default, the pop() method removes the last element from a list. For example, the following code would print red, yellow, blue, and orange because pop removed green from the end of the list. myColors = ["red","yellow","blue","orange","green"] myColors.pop() print(myColors)

What is the remove() method useful for.

The remove() method allows you to remove an item when you don't know the position. motorcycles.remove('ducati') VS del motorcycles[3]

Why are lists important in Python?

Lists allow you to store sets of information in one place. Lists are one of Python's most powerful features.

What does the reverse() method do?

The reverse() method permanently sorts the list in reverse. It is the reverse of the list's existing order. It is not a reverse alphabetically or numerically. cars.reverse()

What does the len() function do?

The len() function retrieves the length of an object, or in the case of a string, the number of characteristics in the string. len(cars) to print print(len(cars))

What does the Python sort() method do?

The sort() method sorts a list alphabetically. The change is permanent. The sort() method can be used to put a list in reverse alphabetical order by using the reverse=true argument. It is important to capitalize true in reverse=True. cars.sort() cars.sort(reverse=True)

What does the sorted() function do?

The sorted() function is used to present a list in an alphabetical order temporarily. It does not permanently change the order the list is sorted in. print(sorted(cars)) print(sorted(cars, reverse=True))

How do you add an item to a list in Python in a specific index?

To add an item to Python list at a specific position use the insert() method. motorcycles = ['honda','yamaha','suzuki'] motorcycles.insert(0, 'ducati')

How do you modify an element of a list in Python?

To change an element, use the name of the list followed by the index of the element you want to change, and then provide the new value you want that item to have. Example: motorcycles = ['honda','yamaha','suzuki'] print(motorcycles) motorcycles[0] = 'ducati'

How do you add a new item to a list in Python?

You can add a new item to a list in Python by appending it. This will add the item to the end of the list. Example: motorcycles = ['honda','yamaha','suzuki'] motorcycles.append('ducati')

How do you use the pop() method to remove an item from a specific index.

You can use pop() to remove an item in a list at any positing by including the index of the item you want to remove in parentheses. motorcycles.pop(0) VS. motorcycles.pop()

How would you decide to use the del statement over the pop() mehtod?

You should use the del statement when removing something and there is no desire to use it again.


संबंधित स्टडी सेट्स

Section 13: Types of Mortgages and Sources of Financing

View Set

Chapter 49 Assessment Hepatic Disorders Exam 7 NSG 1010

View Set

ASCP Technologist in Molecular Biology board exam prep

View Set

Topic 5: Digestion and Nutrition PREPU

View Set

Ch 21: GI Disorders and Therapeutic Management jk

View Set

Maternal and Newborn Exam 2/Final

View Set

A pénzügyi szektor alapvetései

View Set

Compensation Administration - Chapter 11

View Set

Comp Sci Principles Semester 1 Final

View Set

Pharm Ch. 8 tetracyclines, macrolides, lincosamides (prep u, vocab + quizletA)

View Set