Array (List in Python)
Array (List in Python)
Array (List in Python)
Data structure
▪ Because variables cannot be used for real-life programming, data is
grouped together or, more precisely, structured in a specific format.
▪ The collection of data structured together is called a data structure.
▪ The different data structures in python are:
▪ Lists
▪ Arrays
▪ Tuples
4
Arrays
▪ An array is a data structure used to store a set of data elements in a certain
order.
Note: Array can be created in Python but it is less flexible as it is a
homogeneous collection of element which means an array may consist of
elements of only same data types
▪ Arrays can also be created using lists as it is a heterogeneous collection of
elements, which means a list may consist of elements of different data types. For
example,
myList = [‘six’, 7, 8, ‘nine’, ‘ten’]
▪ Similarly, a one-dimensional array is created by:
array1d=[10, 11, 12, 13, 14]
6
Lists
▪ A list is an ordered, sequential collection of zero or more elements.
▪ A list is a heterogeneous collection of elements, which means a list may
consist of elements of different data types.
▪ Lists are defined using elements in square brackets. For example,
myList=[1, 2, 3, 'four', 5]
7
Introduction
▪ A data structure is a logical model of a particular organisation of data.
▪ An array is an ordered collection of elements of a similar data type. Two
types of arrays : one-dimensional array and two-dimensional array.
▪ Lists: A list is an ordered, sequential collection of zero or more elements.
A list is a heterogeneous collection of elements, which means a list may
consist of elements of different data types.
▪ Tuples: Tuples are similar to lists, but tuples cannot be changed. It is like a
constant list.
Note: So we will be learning only list in Python for arrays concept.
Pseudocode for arrays will be later in Grade 9.
8
Lists
▪ Lists are defined using elements in square brackets. For example,
myList=[1, 2, 3, 'four', 5]
9
Lists
▪ For example,
fruits = ["apple", "banana", "watermelon", "kiwi", "grapes", "papaya"]
score = ["invalid", 10, 18, 20, 12, 15, "invalid", 19]
▪ A list can hold elements of different data types. Similar to arrays,
each element in a list is associated with a unique index.
10
fruits
watermelon 2
kiwi 3
grapes 4
papaya 5
11
List
▪ list1 is an empty list.
▪ None is a special value in Python
that holds a place when it has
nothing to say. In conditional
statements, it produces a False.
▪ list3 is a list with 10 elements.
12
len() function
• The len() function returns the number of elements in a list.
• For example, finding the number of elements in the list fruits,
14
Example
country tempList country=["Albania", "Algeria", "Afghanistan", "Andorra"]
tempList=[]
numOfValues=len(country)
Albania 38 for element in range(numOfValues):
print("Enter the temperature for", country[element],":“, end=" ")
Algeria 44 temp=int(input())
tempList.append(temp)
Afghanistan 38
Andorra 36
16
Algeria 44
Afghanistan 38
Andorra 36
17
Printing elements
▪ This program prints the country name along with its
temperature value.
Program: