COMP1002 - Data Types

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

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

Operator operand 1 operand 2 Result Operator operand 1 operand 2 Result


int int int int
int // int
float float float float
int/ float complex complex int int
+ % int
int/ float Error float float
str
str str int int
list list list int
float float
int int *
int/ - int/ float complex complex
- float float
str/list int str/ list
int/ float complex complex int int
int
int int/ float float ** float float
/
int/ float complex complex int/ float complex complex
Lambo Ferrari Icona W Motors
Collection

Lambo Ferrari Icona W Motors


"Lambo" "Ferrari" "Icona" "W Motors"
List ["Lambo", "Ferrari", "Icona", "W Motors"]
List
["Lambo", "Ferrari", "Icona", "W Motors"]
myList = ["Lambo", "Ferrari", "Icona", "W Motors"]
<variable_name> = [<list_items_comma_sep>]

Lambo Ferrari Icona W Motors

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]

myList = ["Lambo", "Ferrari", "Icona", "W Motors"]


print(myList) #output => ['Lambo', 'Ferrari', 'Icona', 'W Motors']
print(myList[0]) #output => Lambo
print(myList[1]) #output => Ferrari
print(myList[2]) #output => Icona
print(myList[-1])#output => W Motors
print(myList[4])
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]

myList = ["Lambo", "Ferrari", "Icona", "W Motors"]


print(type(myList)) #output => <class 'list'>
print(type(myList[0])) #output => <class 'str'>
print(type(myList[1])) #output => <class 'str'>
print(type(myList[2])) #output => <class 'str'>
print(type(myList[-1])) #output => <class 'str'>
List Indexing
myList 5 7 9 -2

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]

myList = [5, 7, 9, -2]


print(type(myList)) #output => <class 'list'>
print(type(myList[0])) #output => <class 'int'>
print(type(myList[1])) #output => <class 'int'>
print(type(myList[2])) #output => <class 'int'>
print(type(myList[-1])) #output => <class 'int'>
List Indexing
myList 5 "PolyU" "9" -2

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]

myList = [5, "PolyU", "9", -2]


print(type(myList)) #output => <class 'list'>
print(type(myList[0])) #output => <class 'int'>
print(type(myList[1])) #output => <class 'str'> myStr = "Hello!"
print(type(myList[2])) #output => <class 'str'> print(myStr[1])
print(type(myList[-1])) #output => <class 'int'> # output => e
List Slicing
myList = [1, 5, 10.5, "PolyU", -2, "3", 5]

<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

myList = [5 , "PolyU" , [7,"University",9]]


print(type(myList[2]))
# output => <class 'list'>
Data Types

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.

You might also like