Search Header Logo
Python tuple & Dictionary

Python tuple & Dictionary

Assessment

Presentation

Arts

1st Grade

Medium

Created by

s tobgyal

Used 40+ times

FREE Resource

45 Slides • 8 Questions

1

​Tuple
range
dictionary

set

2

Multiple Choice

What is the output of the following code?

my_tuple = (1, 2, 3)

print(my_tuple[1])

1

1

2

2

3

3

4

Error

3

Multiple Choice

How can you create a tuple with a single element?

1

single_element_tuple = (5,)

2

single_element_tuple = {5}

3

single_element_tuple = [5]

4

single_element_tuple = (5)

4

Multiple Choice

Which of the following is a correct way to create a tuple in Python?

1

my_tuple = [1, 2, 3]

2

my_tuple = {1, 2, 3}

3

my_tuple = (1, 2, 3)

4

my_tuple = 1, 2, 3

5

​tuple:
~are used to store
multiple items in a single variable
~items are
ordered and unchangeable/immutable
~ are written with round brackets-
parenthesis ( ).

media
media

6

media

7

Tuple items:
~ items are
ordered, unchangeable, and allow duplicate values.

~ items can be of any data type

~
zero based items

8

​Tuple item Indexing:
Positive(+ve)
~left to right
~zero based index

Negative (-ve)
~right to left
~starts from -1

media

9

Access Tuple Items:
We can access tuple items by referring to its index, inside square brackets [].



media

10

Tuple length function:
~to find out the number of tuple items
~use len() function

media

11

Tuple with one item:
~We have to add a comma after the item, otherwise Python will not recognize it as a tuple.

media

12

Join Items of tuples:
To join two or more tuples we can use the + operator

media
media

13

Tuple methods:
~we can use to manipulate on tuples items:

1.count()
Returns the
number of times a specified value occurs in a tuple..



media

14

  1. index()
    returns the position or index of the specified value

media
media

15

  1. len()
    used to determine the length or the number of elements in a tuple

media
  1. sum()
    Return the sum of all elements in the tuple



media

16

  1. max()
    used to find the maximum value occurs in a tuple.

media
  1. min()
    used to find the minimum value occurs in a tuple.

media

17

Activities:

Create a Python program to create a tuple named book_info to store information about english novel ‘The Giver’. Include attributes such as title, author, and year of publication. Assign appropriate values and print the tuple.

Write a Python program to receive user input for User_ID and password. Validate the provided User_ID and password against predefined values stored in a tuple. If both conditions are true, display the message "Welcome to your page"; otherwise, show the message "Sorry, you entered incorrect User_ID and Password."

18

range function()
is used to generate a sequence of numbers. It’s very commonly used in loops.

19

Multiple Choice

What is the output of the following code?

for i in range(3):

print(i)

1

1 2 3

2

0 1 2

3

0 1 2 3

4

Error

20

Multiple Choice

What does range(5, 15, 3) do?

1

Generates numbers 5 through 15, stepping by 3

2

Generates numbers 5, 8, 11, 14

3

Generates numbers 5, 8, 11

4

Generates numbers 5, 10, 15

21

​range() function:
returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number

It is commonly used in for loops to generate a series of numbers.

Syntax:

range(Start, Stop, step)
 

x=range(1,10,2)
for n in x:
print(n)
#indentation

22

​Using starting and ending:

to create a sequence of numbers from 3 to 5, and print each item in the sequence

Example:
x=range(3,5)
for n in x:
print(n)

output????

Using starting, ending, increment:

to create a sequence of numbers from 3 to 11, but increment by 2 instead of 1:

Example:
x=range(3,11,2)
for n in x:
print(n)

output????

23

start (optional) - starting value; the default value is 0.

stop (required) - ending number; this number is not included in the sequence; 

step (optional) - increment value; the default value is 1

media
media

24

Multiple Choice

Which of the following is the correct way to define a dictionary in Python?

1

my_dict = {"name": "John", "age": 25}

2

my_dict = [("name", "John"), ("age", 25)]

3

my_dict = "name": "John", "age": 25

4

my_dict = ("name", "John", "age", 25)

25

Multiple Choice

What will the following code output?

my_dict = {"name": "John", "age": 25}

print(my_dict.get("age"))

1

"age"

2

25

3

KeyError

4

None

26

Dictionary
my_dictionary={
"name": "Sonam",
"my_class":10
}

27

Python Dictionary

Dictionary:
~
are used to store data values in key:value pairs.

~
is a collection which is ordered, changeable and do not allow duplicates

28





~Dictionaries are written with curly brackets, and have keys and values { }

~Each key is separated from its value by a colon (:), the items are separated by commas..

x={
"School":"UA","Estd Year":"2002"
}
print(x.keys()


Example:

dict={'Name': 'Jigdrel',

'Sex':'Male',

'Age':'15'}

print(dict)

media

29

Accessing items:
~We can access the items of a dictionary by referring to its key name, inside square brackets..
~We can also use get() method....

media

30

Open Ended

Write a  Python program that acts as personal digital assistant, storing name, class and roll number of a student.

name - Karma, class- X, roll_number- 5


31

Dictionary Methods


Python has a set of built-in methods that can use on dictionaries.

1. clear()
Removes all the elements from the dictionary



media
media

32

  1. copy()
    Returns a copy of the dictionary.



media

33

​3. get()
Returns the value of the specified key.

media
  1. pop()

Removes the element with the specified key.

media

34

5. items()
The view object contains the key-value pairs of the dictionary, as tuples in a list..

media

35

  1. keys()

Returns a list containing the dictionary's keys.

  1. values()

Returns a list of all the values in the dictionary.

media
media

36

Change values:
We can change the value of a specific item by referring to its key name



media

37

Add items:
adds new items to an existing items
is done using new index key and assigning a value to it


media

38

update()
Updates the dictionary with the specified key-value pairs.

media

39

Activities: 2023 BCSE trial

One of the shops in Thimphu wants to display the items they have in stock and their cost

prices. The shop also wants to display the total cost price of all the items as shown below.

Therefore, the shop owner has requested you to write a Python program with the following

criteria:

i. Use a dictionary to store item lists and their cost as keys and values respectively

ii. Display the dictionary using a for loop

iii. Add the values from the dictionary and display as given in the output with the same format.

media

40

Set:

41


set
is an unordered collection of unique items. Sets cannot have duplicate values and their values can be changed (Mutable).

It is defined using curly braces {} or the built-in set() function and the elements are separated by commas.

media
media

42

media

43

​Set Methods
Python has a set of built-in methods that you can use on sets. Here are some of the common dictionary methods in Python:

​copy()
Returns a copy of the set

media

add()
Adds an element to the set

media

44

discard()
Remove the specified item

clear()
Removes all the elements from the set

media
media

45

difference ()
Returns a set containing the difference between two or more sets

media
media

46

intersection ()
Return a set that contains the items that exist in both set x, and set y

Output?????

media

47

union()
Return a new set containing all common elements from both sets without duplicates.

media
media

48

issubset()


Returns True if another set contains this set

media
media

49

issuperset()

checks whether the set contains all the elements of another set.

media
media

50

​isdisjoint():
Returns True if two sets have no elements in common

media

51

media

52

media

53

media

​Tuple
range
dictionary

set

Show answer

Auto Play

Slide 1 / 53

SLIDE