0% found this document useful (0 votes)
12 views

Lesson 5 Python Arrays and Related

Uploaded by

flyware600
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lesson 5 Python Arrays and Related

Uploaded by

flyware600
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Big Data Analytics Lesson 5

Lesson 5: Python Arrays and Related

Lesson 5: Python Arrays and Related .......................................................................................................... 1


5.1. Arrays ............................................................................................................................................ 2
5.2. List ................................................................................................................................................. 2
5.3. Tuples............................................................................................................................................ 3
5.4. Sets................................................................................................................................................ 3
5.5. Dictionaries ................................................................................................................................... 3
Lesson 5: Review Questions ..................................................................................................................... 4

Compiled by: Karari E.K email: [email protected] 1


Big Data Analytics Lesson 5

5.1. Arrays
Arrays are used to store multiple values in one single variable, and you can access the values by
referring to an index number. Use the len() method to return the length of an array (the
number of elements in an array).
You can use the append() method to add an element to an array.
You can use the pop() method to remove an element from the array.
You can also use the remove() method to remove an element from the array.

Example
Create an array containing car names:
cars = ["Ford", "Volvo", "BMW"]
Get the value of the first array item:
x = cars[0]
Modify the value of the first array item:
cars[0] = "Toyota"
Return the number of elements in the cars array:
x = len(cars)
Add one more element to the cars array:
cars.append("Honda")
Delete the second element of the cars array:
cars.pop(1)
Delete the element that has the value "Volvo":
cars.remove("Volvo")

5.2. List
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:
thislist = ["apple", "banana", "cherry"]
list1 = ["abc", 34, True, 40, "male"]
print(thislist)
print(list1)
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
Lists are ordered, that the items have a defined order, and that order will not change. If you
add new items to a list, the new items will be placed at the end of the list.
The list is changeable, meaning that we can change, add, and remove items in a list after it has
been created.
Since lists are indexed, lists can have items with the same value (duplicates).

Compiled by: Karari E.K email: [email protected] 2


Big Data Analytics Lesson 5

To determine how many items a list has, use the len() function.
List items can be of any data type. A list can contain different data types.

5.3. Tuples
Tuples are used to store multiple items in a single variable. 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.
Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
To determine how many items a tuple has, use the len() function.
thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)
To create a tuple with only one item, you have to add a comma after the item, otherwise
Python will not recognize it as a tuple.
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
5.4. Sets
Sets are used to store multiple items in a single variable.
A set is a collection which is unordered, unchangeable*, and unindexed.
Sets are written with curly brackets.
thisset = {"apple", "banana", "cherry"}
print(thisset)
To determine how many items a set has, use the len() function.
5.5. Dictionaries
Dictionaries are used to store data values in key: value pairs.
Dictionaries are written with curly brackets, and have keys and values.
Dictionary items are ordered, changeable, and does not allow duplicates.
Dictionary items are presented in key: value pairs, and can be referred to by using the key
name.
Example:
Create and print a dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Print the "brand" value of the dictionary:
print(thisdict["brand"])

Compiled by: Karari E.K email: [email protected] 3


Big Data Analytics Lesson 5

Lesson 5: Review Questions


1. Explain difference between array, set, list, tuple and dictionary.
2. Write a program to demonstrate how to to add an item in a dictionary.
3. Create an array that stores marks for five students.

Compiled by: Karari E.K email: [email protected] 4

You might also like