COMP1002 - Data Types
COMP1002 - Data Types
COMP1002 - Data Types
Lec03
Muhammad Tayyab
Variables
• Numbers
• int (-10, 0, 5, 812358)
• float (-2.5, 0, 1.7532, 3.00, 2e3)
• Complex
• 2+3j
• complex(2,3)
• Strings
• “this is a string”
• “2”
• “2.3”
• Dynamic typing
• Arithmetic operations
Common operators
1 2 3 4
0 1 2 3
Index
List Indexing
myList Lambo Ferrari Icona W Motors
Index 0 1 2 3
-4 -3 -2 -1
myList[0] myList[1] myList[2] myList[3]
myList[-4] myList[-3] myList[-2] myList[-1]
Index 0 1 2 3
-4 -3 -2 -1
myList[0] myList[1] myList[2] myList[3]
myList[-4] myList[-3] myList[-2] myList[-1]
Index 0 1 2 3
-4 -3 -2 -1
myList[0] myList[1] myList[2] myList[3]
myList[-4] myList[-3] myList[-2] myList[-1]
Index 0 1 2 3
-4 -3 -2 -1
myList[0] myList[1] myList[2] myList[3]
myList[-4] myList[-3] myList[-2] myList[-1]
<list_name>[<index>]
<list_name>[<start>:<end>]
slice = myList[2:6]
print(slice)
# output => [10.5, 'PolyU', -2, '3']
print(myList[2:])
# output => [10.5, 'PolyU', -2, '3', 5]
print(myList[:-3])
# output => [1, 5, 10.5, 'PolyU']
print(myList[2:-1])
# output => [10.5, 'PolyU', -2, '3']
Dynamic List Elements
myList = [1, 5, 10.5, "PolyU", -2, "3", 5]
myList[2] = "Hi"
print(myList)
# output => [1, 5, 'Hi', 'PolyU', -2, '3', 5]
print(myList[2] + myList[3])
# output => HiPolyU
print(myList[3][0])
# output => P
List Python Functions
Method Description
append() Adds an element at the end of the list. Syntax: <list>.append(<item>)
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 first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
*sum() Sum all elements. Syntax: sum(<list>)
*len() Get length of list
List
• List
• Ordered collection of items (any data type)
• List of lists
https://www.w3schools.com/python/python_datatypes.asp
List, Tuple, Set, Dictionary
List vs Tuple vs Set
Use cases
Data Structure Best Use Cases
- Immutable sequence of elements.
- Store a collection of items that shouldn't be modified.
Tuple
- Use a collection as a key in a dictionary.
- Return multiple values from a function.
- Store a collection of unique elements.
Set - Perform set operations such as union, intersection, or difference.
- Efficiently check membership or eliminate duplicates from a collection.
- Ordered collection of elements that can be modified.
- Add, remove, or modify elements frequently.
List
- Access elements by their index.
- Preserve the order of insertion.
- Store and retrieve values based on a unique key.
- Associate a value with a specific identifier.
Dictionary
- Quickly look up values based on a key.
- Store key-value pairs with no specific order.