Basic Session 2
Basic Session 2
• There are four collection data types in the Python programming language:
• List is a collection which is ordered and changeable. Allows duplicate
members.
• Tuple is a collection which is ordered and unchangeable. Allows duplicate
members.
• Set is a collection which is unordered, unchangeable*, and unindexed. No
duplicate members.
• Dictionary is a collection which is ordered** and changeable. No duplicate
members.
Python Lists
• Lists are used to store multiple items in a single variable.
• Lists are one of 4 built-in data types in Python used to store collections of
data, the other 3 are Tuple, Set, and Dictionary, all with different qualities
and usage.
• Lists are created using square brackets:
fruit= ["apple", "banana", "cherry"]
print(fruit)
fruit = [10,20,30,40,20,80]
if 15 in fruit:
print("yes")
else:
print("No")
Change List Items
fruit = ["apple", "banana", "cherry"]
fruit[1] = "blackcurrant"
print(fruit)
for x in a:
// if "a" in x:
b.append(x)
print(b)
Condition
a = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in a if x != "apple"]
print(newlist)
Iterable
newlist = [x for x in range(10)]
print(newlist)
Sort Descending
fruit = ["orange", "mango", "kiwi", "pineapple", "banana"]
fruit.sort(reverse = True)
print(fruit)
Reverse Order
fruit = ["banana", "Orange", "Kiwi", "cherry"]
fruit.reverse()
print(fruit)
fruit = ["banana", "Orange", "Kiwi", "cherry"]
fruit.sort(key = str.lower)
print(fruit)
Copy Lists
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Python Tuples
• Tuples are used to store multiple items in a single variable.
• Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are
List, Set, and Dictionary, all with different qualities and usage.
• A tuple is a collection which is ordered and unchangeable.
• Tuples are written with round brackets.
• Tuple items are ordered, unchangeable, and allow duplicate values.
tuple1 = ("abc", 34, True, 40, "male") mytuple = ("apple", "banana", "cherry")
print(type(mytuple))
Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.
Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
Ordered
When we say that tuples are ordered, it means that the items have a defined order, and that order will not change.
Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created.
Allow Duplicates
Since tuples are indexed, they can have items with the same value:
a = ("apple",)
print(type(a))
#NOT a tuple
a = ("apple")
print(type(a))
Tuple Items - Data Types
Tuple items can be of any data type:
type()
From Python's perspective, tuples are defined as objects with the data type 'tuple':
<class 'tuple'>
Negative Indexing
y.append("orange")
tp = tuple(y)
Create a new tuple with the value "orange", and add that tuple:
tp = ("apple", "banana", "cherry")
y = ("orange",)
tp += y
print(tp)
Remove Items
Tuples are unchangeable, so you cannot remove items from it, but you can use the same
workaround as we used for changing and adding tuple items:
Convert the tuple into a list, remove "apple", and convert it back into a tuple:
tp = ("apple", "banana", "cherry")
y = list(tp)
y.remove("apple")
tp = tuple(y)
print(green)
print(yellow)
print(red)
print(green)
print(tropic) apple
['mango', 'papaya', 'pineapple']
print(red)
cherry
Python - Loop Tuples
Loop Through a Tuple
You can loop through the tuple items by using a for loop.
Iterate through the items and print the values:
Print all items, using a while loop to go through all the index numbers:
tp = ("apple", "banana", "cherry")
i = 0
while i < len(tp):
print(tp[i])
i = i + 1
Python - Join Tuples
Join Two Tuples
To join two or more tuples you can use the + operator:
Join two tuples:
Multiply Tuples
If you want to multiply the content of a tuple a given number of times, you can use the * operator:
Multiply the fruits tuple by 2:
Tuple Methods
Python has two built-in methods that you can use on tuples.
Method Description
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of where it was
found
Set
• Sets are used to store multiple items in a single variable.
• Set is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Tuple, and Dictionary, all with different qualities and usage.
• A set is a collection which is unordered, unchangeable*, and unindexed.
• Not allowed duplicate
Unordered
Unordered means that the items in a set do not have a defined order.
Set items can appear in a different order every time you use them, and cannot be referred to by index or key.
Unchangeable
Set items are unchangeable, meaning that we cannot change the items after the set has been created.
Once a set is created, you cannot change its items, but you can remove items and add new items.
print(thisset)
Get the Length of a Set
To determine how many items a set has, use the len() function.
type()
From Python's perspective, sets are defined as objects with the data type 'set':
<class 'set'>
What is the data type of a set?
myset = {"apple", "banana", "cherry"}
print(type(myset))
The set() Constructor
It is also possible to use the set() constructor to make a set.
for x in st:
print(x)
print("banana" in thisset)
Add Set Items
Once a set is created, you cannot change its items, but you can add new items.
To add one item to a set use the add() method.
thisset.add("orange")
print(thisset)
Add Sets
To add items from another set into the current set
for x in thisset:
print(x)
The union() method returns a new set with all items from both sets:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
Set Methods
Python has a set of built-in methods that you can use on sets.
Method Description
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing the difference between two or more sets
difference_update() Removes the items in this set that are also included in another, specified set
discard() Remove the specified item
intersection() Returns a set, that is the intersection of two other sets
intersection_update() Removes the items in this set that are not present in other, specified set(s)
isdisjoint() Returns whether two sets have a intersection or not
issubset() Returns whether another set contains this set or not
issuperset() Returns whether this set contains another set or not
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric differences of two sets
symmetric_difference_update() inserts the symmetric differences from this set and another
union() Return a set containing the union of sets
update() Update the set with the union of this set and others