0% found this document useful (0 votes)
33 views4 pages

Lab 10 - Set, Tuple and Dictionary

Python Programming - Lab 10 - Set, Tuple and Dictionary

Uploaded by

Marvella
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Lab 10 - Set, Tuple and Dictionary

Python Programming - Lab 10 - Set, Tuple and Dictionary

Uploaded by

Marvella
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming with Python

LAB 10: Set, Tuple and Dictionary

Part A: Learning Tuple

Different types of tuple

1. Generate the output from the following program:

# Empty tuple
tuple = ()
print(tuple)

# Tuple with integer value


tuple = (1, 2, 3)
print(tuple)

# Tuple with different datatypes


tuple = (1, "Tuple", 3.7)
print(tuple)

# nested tuple
tuple = ("cat", [100, 98, 65], (1, 2, 3))
print(tuple)

Tuple with One Element


ex1 = ("hello")
print(type(ex1))

ex2 = ("hello",) # creating tuple with 1 element


print(type(ex2))

ex3 = "hello", #creating tuple without parenthesis


print(type(ex3))

Accessing Tuple Element

1. Use Index
# Accessing tuple elements using indexing
characters = ("p", "y", "t", "h", "o", "n")

print(characters[0])
print(characters[5])
2. Negative index
# Accessing tuple elements using indexing
characters = ("p", "y", "t", "h", "o", "n")

print(characters[-1])
Programming with Python

print(characters[-3])

3. Slicing
# Accessing tuple elements using slicing
tuple=("p", "y", "t", "h", "o", "n")

print(tuple[1:4])

print(tuple[:-5])

print(tuple[3:])

print(tuple[:])

Part B: Learning Set

1. Multiple way to create set

setA = set()

setA = {}

setA = {067069, 076059, 065670, 065590}

vowel_letters = {'a', 'e', 'i', 'o', 'u'}


setA = {'King', 01, -2, 'The Land'}

2. Duplicate Items in a Set


numbers = {1, 2, 3, 3, 1, 5}
print(numbers)

3. Adding Items in a Set


Given a set with items as below:
numbers = {145, 100, 65, 79}

4. Use add() to add 94 to the set


5. Use update() to add list into set [23, 56, 78]
4. Remove item from set
Using the same set elements from Part B:3. Use discard() to remove 100.
Programming with Python

Part C: Learning Dictionary


1. Creating a dictionary
learnABC = { "a":"apple","b" :"ball","c":"cat","d":"doll"}

print(learnABC)
2. Valid and invalid dictionary
Which one is the valid or invalid dictionary?
myData = {
1: "Python",
(1, 2): 1,
3: [1, 2, 3]
}
print(myData)
myData = {
1: "Python",
[1, 2]: "2, 3",
}
print(myData)

3. Duplicate in Dictionary
myData = {
1:"hello",2:22,2:22,3:"hello",3:33
}

print(myData)

myData = {
1:"hello",2:22,3:22,4:"hello",5:33
}

print(myData)

4. Accessing the dictionary


a. Using key
languages = {"bm": " Bahasa Melayu", "Eng": "English",
"cn": "cantonese"}

print(languages["bm"])

b. Using items() method.


print(languages.items())
Programming with Python

Part D: Test yourself!

1. You were given a tuple with elements (2, 4, 5). You want to generate a total value from the
tuple elements. Write a Python program to unpack the tuple into several variables and display
the total value.
2. You were given a tuple named turtle = (1, “Hello”). Write a python program to
a. Add on item 5 using +
b. Add on item “single” using append ().
3. Write a Python program to convert a tuple of characters into a string.
4. Write a Python program to convert a list into tuple.
5. Write a Python program to check if the element exists between a tuple.
6. Given: tuple = (23, 45, 65, 78, 98, 9, 45, 56, 43). Write a Python program to reverse this tuple.
7. Given a set {1, 2, “Hello”, “Python”}. Write a Python program to iterate over the set.
8. Write a Python program to find element in setA but not in setB.
9. Write a Python program to convert string to set, set to list and set to tuple.
10. Using set, write a Python program to count number of vowels from given strings
“Programming with Python”.
11. Write a Python program to add an item (state: Kedah) into the dictionary. Given a
dictionary {name: “Amira”, “age”: 35}
12. From question 11 above, write a Python program to display all items in the dictionary
using loops.
13. Given a values numbers = {145, 100, 65, 79}, write a Python program to sum all the
values in the dictionary.
14. Given a dictionary keys and items myData = { 1:"hello",2:22,2:22,3:"hello",3:33}, write
a Python program to check if multiple keys exist in a dictionary.

You might also like