Search Header Logo
Listing in python

Listing in python

Assessment

Presentation

Computers

9th Grade

Medium

Created by

Boazi Tarmo

Used 1+ times

FREE Resource

10 Slides • 18 Questions

1

LESSON 2: PYTHON LIST

media

How do we use list in python??

2

List

Lists are used to store multiple items in a single variable.
Lists are created using square brackets

thislist = ["apple", "banana", "cherry"]
print(thislist)

note:

List items are indexed, the first item has index [0], the second item has index [1] etc.

3

List Items

List items are ordered, changeable, and allow duplicate values.



1.Ordered

When we say that lists are ordered, it means that the items have a defined order, and that order will not change.

If you add new items to a list, the new items will be placed at the end of the list.

4

2.Changeable

The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.

5

3.Allow Duplicates

Since lists are indexed, lists can have items with the same value:
Example

Lists allow duplicate values:

thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)

6

List Length

To determine how many items a list has, use the len() function:
Example

Print the number of items in the list:

thislist = ["apple", "banana", "cherry"]
print(len(thislist))

7

List Items - Data Types

List items can be of any data type:

Example

String, int and boolean data types:

list1 = ["apple", "banana", "cherry"]
list2 = [
1, 5, 7, 9, 3]
list3 = [
True, False, False]
print(list1)

print(list2)

print(list3)

8

A list can contain different data types:


Example

A list with strings, integers and boolean values:

list1 = ["abc", 34, True, 40, "male"]
print(list1)

9

type()

From Python's perspective, lists are defined as objects with the data type 'list':

<class 'list'>
Example

What is the data type of a list?

mylist = ["apple", "banana", "cherry"]
print(type(mylist))

10


The list() Constructor

It is also possible to use the list() constructor when creating a new list.
Example

Using the list() constructor to make a List:

thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
print(thislist)

11

Multiple Choice

Lists are used to store single item in a single variable.

1

True

2

False

12

Multiple Choice

List items are ordered, changeable, and allow duplicate values.

1

True

2

False

13

Multiple Choice

List items are indexed, the first item has index [1], the second item has index [2] etc.

1

True

2

False

14

Multiple Choice

When we say that lists are ordered, it means that the items have a defined order, and that order will not change.

1

True

2

False

15

Multiple Choice

List items can be of any data type:

1

True

2

False

16

Multiple Choice

If you add new items to a list, the new items will be placed at the end of the list.

1

True

2

False

17

Multiple Choice

Which of these is the correct code for creating a list of names?

1

nameList = John, Harry, Jesse, John, Harry, Harry

2

nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")

3

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

4

nameList = [John, Harry, Jesse, John, Harry, Harry]

18

Multiple Choice

List items have an index number. In the following list, which item has the index number of 3?

["John", "Harry", "Jesse", "John", "Harry", "Harry"]

1

"John"

2

"Harry"

3

"Jesse"

19

Multiple Choice

Which of these pieces of code would return the name "Harry" from the following list?

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

1

nameList()

2

nameList[1]

3

NameList(4)

4

nameList["4"]

20

Multiple Choice

The list needs one more name added to the end - "Felipe". Which piece of code below would do this?

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

1

nameList.append(Felipe)

2

append(nameList,"Felipe")

3

nameList.append["Felipe",7]

4

nameList.append("Felipe")

21

Multiple Choice

What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry']
print(mylist[1])

1
orange
2
banana
3
cherry
4
apple

22

Multiple Choice

Question image
What output will this code produce?
1
blue
2
green
3
red
4
error

23

Multiple Choice

What will be the result of the following syntax:
mylist = ['apple', 'banana', 'banana', 'cherry']
print(mylist[2])

1
banana
2
cherry
3
apple
4
banana banana

24

Multiple Choice

Is this the correct code for a list?

register = {"Sam", "Pheobe", Georgia", Richard"}

1

Yes

2

No

25

Multiple Choice

True or False.
List items cannot be removed after the list has been created.

1
True
2
Lists are immutable after creation
3
Items can only be added to the list
4
False

26

Multiple Choice

Question image

The count() method returns the number of times the specified element appears in the list.

What is the output of this code?

1

3

2

2

3

1

4

0

27

Multiple Choice

Question image

Output of this code?

1

[2,4,6,8,10]

2

['2','4','6','8','10']

3

[0,2,4,6,8]

4

[10,8,6,4,2]

28

Multiple Choice

What is the index of “grape” in the list

[“apple”, “grape”, “orange”, “watermelon”]?

1

0

2

1

3

2

4

3

LESSON 2: PYTHON LIST

media

How do we use list in python??

Show answer

Auto Play

Slide 1 / 28

SLIDE