Chapter 29 - Arrays in Python
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material
Quick Recap
- Quick Recap – Data Structures in Python
In previous Chapter, I presented
- Data Structure
- Definition 1
- A Data Structure is defined as a collection of Data Values and relationship among them
- Definition 2
- A Data Structure is defined as a logical or mathematical model to properly organize Data Values
- Definition 3
- A Data Structure is defined as a way of organizing Data Values, so that they can be used efficiently
- Operations on Data Structures
- We mainly perform the following eight Operations to manipulate the Data Values stored in a Data Structure
- Operation 1 – Creation
- Operation 2 – Insertion
- Operation 3 – Traversal
- Operation 4 – Searching
- Operation 5 – Sorting
- Operation 6 – Merging
- Operation 7 – Updation
- Operation 8 – Deletion
- We mainly perform the following eight Operations to manipulate the Data Values stored in a Data Structure
- Types of Data Structures
- The two main Types of Data Structures are
- Linear Data Structures
- Non-linear Data Structures
- Linear Data Structures
- Definition
- Linear Data Structure is defined as a Data Structure in which Data Values arranged sequentially or linearly
- Definition
- Non-linear Data Structures
- Definition
- A Non-linear Data Structure is defined as a Data Structure in which Data Values are not arranged sequentially or linearly
- Definition
- The two main Types of Data Structures are
- Python Collections
- In Python, there are five Collection Data Types, which are as follows
- Array
- List
- Tuple
- Set
- Dictionary
- In Python, there are five Collection Data Types, which are as follows
- Definition 1
Basics of Arrays
- Main Limitation of Variables
- Question
- What is the main limitation of Variables?
- Answer
- One Variable can store only One Value
- However, in Real-world there are many situations in which we want to store more than one value in a single Variable
- For e.g., storing marks obtained by students in Python course, storing registration numbers of students studying Python course
- However, in Real-world there are many situations in which we want to store more than one value in a single Variable
- One Variable can store only One Value
- Example - Main Limitation of Variables
- Real-world Task / Problem
- There are 50 students registered in the Python course. Your task as a Software Developer is to
- arrange the marks obtained by 50 students in the Python course in ascending order?
- Possible Solution 1
- Construct 50 different Variables to store marks of 50 students in the Python course i.e., One Variable will store marks of only One student
- Possible Solution 2
- Construct One Variable (called Array), which has the capability of store marks of all 50 students in the Python course
- Analysis – Possible Solution 1
- It will be very difficult to store and arrange marks of 50 students in 50 different Variables
- It will be very difficult to perform various Operations (e.g., Storing Data, Retrieving Data etc.) on 50 different Variables
- Analysis – Possible Solution 2
- It will be relatively easy to store and arrange marks of 50 students in a single Variable (called Array)
- It will be relatively easy to perform various Operations (e.g., Storing Data, Retrieving Data etc.) on a single Variable (called Array)
- Conclusion
- It will be more appropriate to use Arrays (compared to Variables), when we want to store and manipulate multiple values
- There are 50 students registered in the Python course. Your task as a Software Developer is to
- How to Overcome the Limitation of Variables
- Question
- How can we overcome the limitation of Variables?
- Answer
- Use Array (a.k.a. Subscripted Variable)
- Array
- Definition
- An Array (a.k.a. Subscripted Variable) is defined as a Collective Name assigned to a group of similar quantities
- Note
- The Data Type of all the elements of an Array must be exactly same
- Purpose
- The main purpose of an Array is to
- store and manipulate multiple values (which have same Data Type)
- The main purpose of an Array is to
- Importance
- Array is one of the oldest and most important Data Structures and used in almost every Program
- Applications
- In a range of Real-world Applications (for e.g., sorting marks of students in Python course in ascending order), we need to store and manipulate multiple (similar) values and a Software Developer can
- efficiently handle multiple (similar) values using an Array
- In a range of Real-world Applications (for e.g., sorting marks of students in Python course in ascending order), we need to store and manipulate multiple (similar) values and a Software Developer can
- Strengths
- Using Arrays, we can easily store, retrieve, and manipulate multiple values of same Data Type
- Using Arrays, we can easily implement other important Data Structures such as Linked List, Stack, Queue, Tree, Graph etc.
- Weaknesses
- The Size of Array (i.e., Number of Elements to be stored in the Array) should be known in advance i.e.,
- If we allocate more memory then requirement then it will lead to wastage of Memory Space and
- If we allocate less memory then requirement then it will also create problems
- Since an Array is a Static Data Structure e.,
- Once the Size of Array is declared then: (1) Size of Array cannot be changed and (2) Memory allocated to an Array cannot be increased or decreased
- In an Array, the Insertion (of new element) and Deletion (of an existing element) Operations are quite expensive because
- Array Elements are stored at consecutive Memory Locations
- The Size of Array (i.e., Number of Elements to be stored in the Array) should be known in advance i.e.,
- Suitable to Use
- Whenever we want to store, retrieve, and manipulate multiple Data Values (having same Data Type)
- we should use Arrays
- Whenever we want to store, retrieve, and manipulate multiple Data Values (having same Data Type)
- Syntax - Variable Initialization vs Array Initialization
Syntax – Variable Initialization
NAME_OF_VARIABLE = DATA_VALUE
Syntax – Array Initialization
arrayName = array(typecode, [Initializers])
- Note
- TYPECODE are the codes that are used to define the type of data values the array will hold
- Example 1 - Variable Initialization vs Array Initialization
Code – Integer Variable Initialization
age = 38
Analysis – Variable age
Variable age holds an Integer Value i.e.,
38
Code – Integer Array Initialization
ages = [26, 31, 38]
Analysis – Variable age
Array ages holds three Integer Values i.e.,
26
31
38
- Example 2 - Variable Initialization vs Array Initialization
Code – Float Variable Initialization
marks = 88.5
Analysis – Variable age
Variable marks holds one Float Value i.e.,
88.5
Code – Integer Array Initialization
marks = [50.5, 90.2, 80.0, 40.5, 25.2]
Analysis – Variable age
Array marks holds five Float Values i.e.,
50.5
90.2
80.0
40.5
25.2
- Example 3 - Variable Initialization vs Array Initialization
Code – Character Variable Initialization
grade = 'A'
Analysis – Variable age
Variable grade holds one Character Value i.e., A
Code – Character Array Initialization
marks = [50.5, 90.2, 80.0, 40.5, 25.2]
Analysis – Variable age
Array grades holds two Character Values i.e.,
F
A
- Lecture Focus
- The focus of this Lecture will be on Integer and Float Arrays
- In Sha Allah, we will discuss Array of Characters (a.k.a. String) in next Chapter
Accessing Array Elements
- Array vs Array Element
- Array
- is a collection of Array Elements
- Array Element
- Array Element is an individual member of an Array
- Accessing Array vs Accessing Array Element
- Accessing Array
- Use name of Array
- Accessing Array Element
- Use name of Array[Subscript] OR name of Array[Index]
- Important Note
- In an Array, all Operations (for e.g., storing value, retrieving value, modifying value etc.) are performed on Array Elements
- Array Element vs Normal Variable
- Each Array Element behaves like a Normal Variable e.,
- All Operations that we can perform on a Normal Variable can also be performed on an Array Element
- Storage of Array Elements in Memory
- Question
- How are Array Elements stored in the Memory?
- Answer
- Array Elements are stored at consecutive Memory Locations
- Start Value of Index (Subscript) of an Array
- Question
- What is the Start Value of Index (Subscript) of an Array?
- Answer
- 0
- End Value of Index (Subscript) of an Array
- Question
- What is the End Value of Index (Subscript) of an Array?
- Answer
- Size of Array – 1
- Memory and Index (Subscript) Representation of an Array
- In this Example, I am assuming that an Integer takes
- 4 Bytes of Memory
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
ages = [25, 30, 40, 10, 5]
Memory and Index (Subscript) Representation of Array ages
Index | 0 | 1 | 2 | 3 | 4 |
Value | 25 | 30 | 40 | 10 | 5 |
Address | DD20 | DD24 | DD28 | DD32 | DD36 |
- Example – Accessing Individual Array Elements
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
ages = [25, 30, 40, 10, 5]
Start Value of Index (Subscript) of Array ages
End Value of Index (Subscript) of Array ages
Size of Array – 1
5 – 1
4
Memory and Index (Subscript) Representation of Array ages
Index | 0 | 1 | 2 | 3 | 4 |
Value | 25 | 30 | 40 | 10 | 5 |
Address | DD20 | DD24 | DD28 | DD32 | DD36 |
Accessing Individual Array Elements
- Accessing First Array Element
- ages[0]
- Accessing Second Array Element
- ages[1]
- Accessing Third Array Element
- ages[2]
- Accessing Forth Array Element
- ages[3]
- Accessing Fifth Array Element
- ages[4]
Printing Values of Individual Array Elements on Output Screen
- Example 1 – Printing Values of Individual Array Elements on Output Screen
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this program is to Print Values of Individual Array Elements on Output Screen
'''
# Input + Processing + Output
try:
ages = [25, 30, 40, 10, 5]
print("Value of First Array Element is: \n", ages[0])
print("Value of Second Array Element is: \n", ages[1])
print("Value of Third Array Element is: \n", ages[2])
print("Value of Fourth Array Element is: \n", ages[3])
print("Value of Fifth Array Element is: \n", ages[4])
except:
print("Error Occurred")
Memory and Index (Subscript) Representation of Array ages
- Execute ages = [25, 30, 40, 10, 5]
Index | 0 | 1 | 2 | 3 | 4 |
Value | 25 | 30 | 40 | 10 | 5 |
Address | DD20 | DD24 | DD28 | DD32 | DD36 |
- Output Screen
- –
- Execute print(“Value of First Array Element is: “, ages[0])
- Output Screen
- Value of First Array Element is: 25
- –
- Output Screen
- Execute print(“Value of Second Array Element is: “, ages[1])
- Output Screen
- Value of First Array Element is: 25
- Value of Second Array Element is: 30
- –
- Output Screen
- Execute print(“Value of Third Array Element is: “, ages[2])
- Output Screen
- Value of First Array Element is: 25
- Value of Second Array Element is: 30
- Value of Third Array Element is: 40
- –
- Value of First Array Element is: 25
- Output Screen
- Execute print(“Value of Fourth Array Element is: “, ages[3])
- Output Screen
- Value of First Array Element is: 25
- Value of Second Array Element is: 30
- Value of Third Array Element is: 40
- Value of Fourth Array Element is: 10
- –
- Value of First Array Element is: 25
- Output Screen
- Execute print(“Value of Fifth Array Element is: “, ages[4])
- Output Screen
- Value of First Array Element is: 25
- Value of Second Array Element is: 30
- Value of Third Array Element is: 40
- Value of Fourth Array Element is: 10
- Value of Fifth Array Element is: 5
- –
- Output Screen
Value of First Array Element is: 25
Value of Second Array Element is: 30
Value of Third Array Element is: 40
Value of Fourth Array Element is: 10
Value of Fifth Array Element is: 5
- Important Observation
- Each Array Element is behaving like a Normal Variable
Modifying Values of Individual Array Elements
- Example – Modifying Values of Individual Array Elements
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this program is to Modify Values of Individual Array Elements
'''
# Processing
try:
ages = [25, 30, 40, 10, 5]
ages[3] = 55
ages[0] = ages[1] + ages[4]
ages[2] = ages[3] - ages[0]
ages[1] = ages[1] + 10
except:
print("Error Occurred")
Memory and Index (Subscript) Representation of Array ages | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Initial Array Elements
Array Elements After Modification
|
- Important Observation
- Each Array Element is behaving like a Normal Variable
Methods to Store Values in an Array
- Methods to Store Values in an Array
- The three main Methods to store Values in an Array are
- Method 1: Array Initialization
- Method 2: Store Values Element by Element at Code Time
- Method 3: Store Values Element by Element at Run Time
- Example - Method 1: Array Initialization
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# The main purpose of this program is to Initialize an Array
ages = [25, 30, 40, 10, 5]
Dry Run | ||||||||||||||||||
|
- Note
- Using this Method, the order of Array Elements is important in Memory
- Array Element which comes first will be stored first in the Memory
- Example - Method 2: Store Values Element by Element at Code Time
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this program is to Store Values Element by Element at Code Time
'''
try:
# Five elements (i.e., 0) will be stored in ages array
# Create Array of Size = 5 and assign 0 to all indexes
ages = [0] * 5 // comment?
ages[0] = 25
ages[3] = 10
ages[4] = 5
ages[1] = 30
ages[2] = 40
except:
print("Error!!! Index out of Range")
Dry Run | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
- Note
- Using this Method, when we store Values, the order of Array Elements is not important
- What matters is the Index of Array Element
- Example - Method 3: Store Values Element by Element at Run Time
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of this program is to Store Values Element by Element at Run Time
'''
# Input + Processing + Output
try:
ages = []
for counter in range(0,5):
print("Enter value of ages[",counter,"]: ")
age = int(input())
ages += [age]
print("Values of Ages Inserted in an Empty Array are: ",ages)
except:
print("Error Occurred")
Important Note |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Dry Run |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Enter value of ages[ 0 ]:
25
Enter value of ages[ 1 ]:
30
Enter value of ages[ 2 ]:
40
Enter value of ages[ 3 ]:
10
Enter value of ages[ 4 ]:
5
Values of Ages Inserted in an Empty Array are: [25, 30, 40, 10, 5]
Methods to Display Values Stored in an Array
- Methods to Display Values Stored in an Array
- The two main Methods to display Values stored in an Array are
- Method 1: Display Values Stored in an Array without Loop
- Method 2: Display Values Stored in an Array with Loop
- Method 3: Display Entire Array at Once
- Example - Method 1: Display Values Stored in an Array without Loop
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this program is to Display Values Stored in an Array without Loop
'''
try:
ages = [25, 30, 40, 10, 5]
print("Value of First Array Element is: \n", ages[0])
print("Value of Second Array Element is: \n", ages[1])
print("Value of Third Array Element is: \n", ages[2])
print("Value of Fourth Array Element is: \n", ages[3])
print("Value of Fifth Array Element is: \n", ages[4])
except:
print("Error Occurred")
Memory and Index (Subscript) Representation of Array ages | ||||||||||||||||||
|
Value of First Array Element is: 25
Value of Second Array Element is: 30
Value of Third Array Element is: 40
Value of Fourth Array Element is: 10
Value of Fifth Array Element is: 5
- Example - Method 2: Display Values Stored in an Array with Loop
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this program is to Display Values Stored in an Array with Loop
'''
# Input + Processing + Output
try:
ages = [25, 30, 40, 10, 5]
counter = 0
for age in ages:
print(age)
counter = counter + 1
except:
print("Error Occurred")
Important Note |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Dry Run |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TODO and Your TurnTodo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
Your Turn Tasks
Your Turn Task 1
Chapter Summary
In this Chapter, I presented the following main concepts:
In Next Chapter
Share this article!
Facebook
Twitter
LinkedIn
About UsIlm O Irfan Technologies believes that honest dedicated hard work with the ethical approach of commitment true to every word, and trust-building is the road to success in any field of business or life. Useful Links© 2022 Ilmo Irfan. All Rights Reserved. |