Search Header Logo
VannKit: List and Dictionary

VannKit: List and Dictionary

Assessment

Presentation

Computers

4th Grade - University

Hard

Created by

Fabian Manning

FREE Resource

17 Slides • 10 Questions

1

​List and Dictionay

List and dictionary have a distinct role to play in python programming.

Some text here about the topic of discussion

2

​List and Dictionay

​In this lesson we will study List and Dictionary

Let us look on each type separately, and later in the lesson we will combine them.​

 

Some text here about the topic of discussion

3

List: Lists are written within square brackets [ ].

Note 1: This list consists of strings since each elements has " "

Example 1:

thislist 1= ["apple", "banana", "cherry"]

Example 2:

thislist 2= ["apple", "banana", "cherry", 1, 4. 6. true, false]

Note 2: This list consists of strings, intgers and bolian values

4

5

Audio Response

State two things you learn from the watching the video.

audio
Open Audio Recorder

6

Multiple Choice

What does the [ ] mean as shown below?

1

1. B = ["apple", "banana", "cherry"]

1

Line 1 are fruits

2

Line 1 is a list called B

7

​List length

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

Example: Print the number of items in the list:

D = ["apple", "banana", "cherry"]

print( len(D) )

Here we are asking to print the number of items in list called 'D'

The result should be 3​

8

Audio Response

What is this asking?

list1 = ["apple", "banana", "cherry"]

list2 = [1, 5, 7, 9, 3]

list3 = [True, False, False]

print(len (list2))

audio
Open Audio Recorder

9

Open Ended

Q1 part 1

Using the example shown below,

Create a list with four elements.

Estimate the number of elements in the list, using `len` function

Example: D = ["apple", "banana", "cherry"]

print( len(D) )

10

Extracting an element from a list

To show how to do this, we will use

thislist 2= ["apple", "banana", "cherry", 1, 4. 6. true, false]

Subject | Subject

Some text here about the topic of discussion

NOTES:​

  1. Each item in a list is said to be ordered.

  2. This means that items occupy a unique position in the list,.

  3. This position starts numbering sequence: 0, 1, 2, 3, 4...​

thislist 2= ["apple", "banana", "cherry", 1, 4. 6. true, false]

index 0

index 1

index 2

index 7

...​

11

Extracting an element from a list

Subject | Subject

Some text here about the topic of discussion

thislist 2= ["apple", "banana", "cherry", 1, 4. 6. true, false]

index 0

index 1

index 2

index 7

index 3

index 4

index 5

  • To extract an element from this list, all you need to know is the list position

  • Let's assume you want to extract, false from the list

  • "cherry holds position 7 in list called thislist 2

  • ​So to extract, we will write....

    • result=thislist 2​[7]

NOTE: The result list is similar to list 2, but it is saying that it wants only the item in index 7 of thislist 2.

index 6

12

Multiple Choice

What will be printed?

list1 = ["apple", "banana", "cherry"]

list2 = [1, 5, 7, 9, 3]

list3 = [True, False, False, 2, 4, 5]

print(len (list2))

1

3

2

5

3

6

13

Multiple Choice

What will be printed?

list1 = ["apple", "banana", "cherry"]

list2 = [1, 5, 7, 9, 3]

list3 = [True, False, False, 2, 4, 5]

print(len (list3))

1

3

2

5

3

6

14

Multiple Choice

What will be printed?

list1 = ["apple", "banana", "cherry"]

list2 = [1, 5, 7, 9, 3]

list3 = [True, False, False, 2, 4, 5]

print(len (list1))

1

3

2

5

3

6

15

​List: Data Type

Some text here about the topic of discussion

List items can be of any data type:

Example 1

String, int and boolean data types:

  1. list1 = ["apple", "banana", "cherry"]

  2. list2 = [1, 5, 7, 9, 3]

  3. list3 = [True, False, False]

16

​List: Data Type

Some text here about the topic of discussion

List items can be of any data type:

Example 2

A list with strings, integers and boolean values:

list1 = ["abc", 34, True, 40, "male"]

17

​We will end list for now, and start the Dictionary part of the lesson

Some text here about the topic of discussion

18

​Dictionary is similar to list, except that Dictionary provide more information.

Dictionary stores data values in value pairs. (that is, two at a time)

19

​List vs Dictionary

media

list = ["Eggs","milk", "cheese, "Yogurt", "Butter", "More Cheese"]

​Note:

Indexing starts 0,

  • 1st item, "eggs' is at index 1​

  • 2nd item, "milk" is at index 2​

  • and so on​

20

​List vs Dictionary

media

​dictionary = {  "Eggs": "2.59",  "Milk": "3.19",  "Cheese": 4.80, "Yogert": 1.35, "Butter": 2.59, "More Cheese: 6.19}

21

​List vs Dictionary

media
media

list1 = ["Eggs","milk", "cheese, "Yogurt", "Butter", "More Cheese"]

dictionary = {  "Eggs": "2.59",  "Milk": "3.19",  "Cheese": 4.80, "Yogert": 1.35, "Butter": 2.59, "More Cheese: 6.19}

22

​List vs Dictionary

dictionary = {  "Eggs": "2.59",  "Milk": "3.19",  "Cheese": 4.80, "Yogert": 1.35, "Butter": 2.59, "More Cheese: 6.19}

list = ["Eggs","milk", "cheese, "Yogurt", "Butter", "More Cheese"]

​NOTE:

  1. ​The list itemizes each items in the list

  2. The dictionary gives more information, it also give the pricing information

  3. The dictionary information is in pairs​

23

Using the key names

Items in a dictionary can be accessed using the key names.

​Note

The Key name is the 1st item in the ordered pair

​Example1 = { 

 "brand": "Ford",  

"model": "Mustang",  

"year": 1964}

​In the example1 dictionary, the key names are

  • " brand"

  • ​"model"

  • ​"year"

  • ​"year"

24

Using the key names

​Example1 = { 

 "brand": "Ford",  

"model": "Mustang",  

"year": 1964}

Let's say we want to extract "Mustang" from the directory.

The key name is "model"​

So we will write:

x = example1["model"]

25

Fill in the Blank

prices = {  "Eggs": "2.59",  "Milk": "3.19",  "Cheese": 4.80, "Yogert": 1.35, "Butter": 2.59, "More Cheese: 6.19}

Complete the statement that would assess the cost of 'Cheese' from the dictionary

x= dictionary[---------]

Q1 part2Q1\ part2  

26

Fill in the Blank

prices = {  "Eggs": "2.59",  "Milk": "3.19",  "Cheese": 4.80, "Yogert": 1.35, "Butter": 2.59, "More Cheese: 6.19}

Write the statement that would assess the cost of 'Milk' from the dictionary

Q1, part 2Q1,\ part\ 2   

27

Open Ended

Create a list with four elements and extract first element from the list

​List and Dictionay

List and dictionary have a distinct role to play in python programming.

Some text here about the topic of discussion

Show answer

Auto Play

Slide 1 / 27

SLIDE