

Python tuple & Dictionary
Presentation
•
Arts
•
1st Grade
•
Medium
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
2
3
Error
3
Multiple Choice
How can you create a tuple with a single element?
single_element_tuple = (5,)
single_element_tuple = {5}
single_element_tuple = [5]
single_element_tuple = (5)
4
Multiple Choice
Which of the following is a correct way to create a tuple in Python?
my_tuple = [1, 2, 3]
my_tuple = {1, 2, 3}
my_tuple = (1, 2, 3)
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 ( ).
6
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
9
Access Tuple Items:
We can access tuple items by referring to its index, inside square brackets [].
10
Tuple length function:
~to find out the number of tuple items
~use len() function
11
Tuple with one item:
~We have to add a comma after the item, otherwise Python will not recognize it as a tuple.
12
Join Items of tuples:
To join two or more tuples we can use the + operator
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..
14
index()
returns the position or index of the specified value
15
len()
used to determine the length or the number of elements in a tuple
sum()
Return the sum of all elements in the tuple
16
max()
used to find the maximum value occurs in a tuple.
min()
used to find the minimum value occurs in a tuple.
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 2 3
0 1 2
0 1 2 3
Error
20
Multiple Choice
What does range(5, 15, 3) do?
Generates numbers 5 through 15, stepping by 3
Generates numbers 5, 8, 11, 14
Generates numbers 5, 8, 11
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) | |
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
24
Multiple Choice
Which of the following is the correct way to define a dictionary in Python?
my_dict = {"name": "John", "age": 25}
my_dict = [("name", "John"), ("age", 25)]
my_dict = "name": "John", "age": 25
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"))
"age"
25
KeyError
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)
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....
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
32
copy()
Returns a copy of the dictionary.
33
3. get()
Returns the value of the specified key.
pop()
Removes the element with the specified key.
34
5. items()
The view object contains the key-value pairs of the dictionary, as tuples in a list..
35
keys()
Returns a list containing the dictionary's keys.
values()
Returns a list of all the values in the dictionary.
36
Change values:
We can change the value of a specific item by referring to its key name
37
Add items:
adds new items to an existing items
is done using new index key and assigning a value to it
38
update()
Updates the dictionary with the specified key-value pairs.
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.
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.
42
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
add()
Adds an element to the set
44
discard()
Remove the specified item
clear()
Removes all the elements from the set
45
difference ()
Returns a set containing the difference between two or more sets
46
intersection ()
Return a set that contains the items that exist in both set x, and set y
Output?????
47
union()
Return a new set containing all common elements from both sets without duplicates.
48
issubset()
Returns True if another set contains this set
49
issuperset()
checks whether the set contains all the elements of another set.
50
isdisjoint():
Returns True if two sets have no elements in common
51
52
53
Tuple
range
dictionary
set
Show answer
Auto Play
Slide 1 / 53
SLIDE
Similar Resources on Wayground
48 questions
body parts
Presentation
•
KG
49 questions
Restaurant Bill Splitter (independent)
Presentation
•
1st Grade
48 questions
repaso
Presentation
•
1st Grade
48 questions
G1 P3
Presentation
•
1st Grade
46 questions
8.5 DISPERSION OF LIGHT
Presentation
•
1st Grade
49 questions
Eric Quiz Guidelines
Presentation
•
1st Grade
48 questions
Soft sign at the end of Russian nouns
Presentation
•
1st Grade
Popular Resources on Wayground
6 questions
Secondary Safety Quiz
Presentation
•
9th - 12th Grade
10 questions
MyPath Diagnostic Overview
Presentation
•
6th - 8th Grade
20 questions
Lab Safety Quiz
Quiz
•
6th Grade
21 questions
Continents and Oceans
Quiz
•
6th Grade
20 questions
Adding and Subtracting Decimals
Quiz
•
5th Grade
20 questions
Parts of Speech
Quiz
•
5th Grade
16 questions
Subject & Predicate
Quiz
•
5th Grade
21 questions
Lab Safety
Quiz
•
10th Grade
Discover more resources for Arts
20 questions
Addition Facts within 20
Quiz
•
1st - 3rd Grade
15 questions
Addition and Subtraction
Quiz
•
1st Grade
17 questions
Greetings and Farewells in Spanish
Quiz
•
1st - 6th Grade
20 questions
Addition and Subtraction facts
Quiz
•
1st - 3rd Grade
22 questions
Addition and Subtraction Facts
Quiz
•
1st - 2nd Grade
20 questions
Place Value
Quiz
•
KG - 3rd Grade
10 questions
Exploring Place Value Up to Hundred Millions
Interactive video
•
1st - 5th Grade
21 questions
Addition and Subtraction Fact Fluency
Quiz
•
1st Grade