Skip to content

Ilm o Irfan

Technologies

  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
Explore Courses

Introduction to Python

  • September 29, 2022
  • Home
  • Free Book

Table of Contents

Chapter 29 - Arrays in Python

  • Authors
  • Ms. Samavi Salman
  • Dr. Rao Muhammad Adeel Nawab
  • Supporting Material
  • Download Supporting Material (Code): here

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
    • 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
      • 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
    • Python Collections
      • In Python, there are five Collection Data Types, which are as follows
        • Array
        • List
        • Tuple
        • Set
        • Dictionary

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
  • 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
  • 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)
  • 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
  • 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
  • Suitable to Use
    • Whenever we want to store, retrieve, and manipulate multiple Data Values (having same Data Type)
      • we should use Arrays
  • 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
      • –
  • 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
      • –
  • 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
        • –
  • 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
        • –
  • 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
      • –
				
					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
  • 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
 
  • Execute ages[3] = 55
 
Index 0 1 2 3 4
Value 25 30 40 55 5
Address DD20 DD24 DD28 DD32 DD36
 
  • Execute ages[0] = ages[1] + ages[4]
 
Index 0 1 2 3 4
Value 35 30 40 55 5
Address DD20 DD24 DD28 DD32 DD36
 
  • Execute ages[2] = ages[3] – ages[0]
  
Index 0 1 2 3 4
Value 35 30 20 55 5
Address DD20 DD24 DD28 DD32 DD36
 
  • Execute ages[1] = ages[1] + 10
 
Index 0 1 2 3 4
Value 35 40 20 55 5
Address DD20 DD24 DD28 DD32 DD36
 
  • All statements are Executed. Program will terminate 😊
 
Initial Array Elements
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36

Array Elements After Modification
 
Index 0 1 2 3 4
Value       35       40       20      55      5
Address DD20 DD24 DD28 DD32 DD36
  
  • 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
  • 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

 

  • 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
  • Execute ages = [0] * 5

 

Index

0

1

2

3

4

Value

0

0

0

0

0

Address

DD20

DD24

DD28

DD32

DD36

 

  • Execute ages[0] = 25

 

Index

0

1

2

3

4

Value

25

0

0

0

0

Address

DD20

DD24

DD28

DD32

DD36

 

  • Execute ages[3] = 10

 

Index

0

1

2

3

4

Value

25

0

0

10

0

Address

DD20

DD24

DD28

DD32

DD36

 

  • Execute ages[4] = 5

 

Index

0

1

2

3

4

Value

25

0

0

10

5

Address

DD20

DD24

DD28

DD32

DD36

 

  • Execute ages[1] = 30

 

Index

0

1

2

3

4

Value

25

30

0

10

5

Address

DD20

DD24

DD28

DD32

DD36

 

  • Execute ages[2] = 40

 

Index

0

1

2

3

4

Value

25

30

40

10

5

Address

DD20

DD24

DD28

DD32

DD36

  • 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
  • Start Value of counter = Start Value of Index
  • End Value of counter = End Value of Index
Dry Run
  • Execute ages = []
 
Index 0 1 2 3 4
Value          
Address DD20 DD24 DD28 DD32 DD36
 
  • Output Screen
    • –
  • Execute the for loop
    • Iteration 1
      • Initialization
        • counter = 0
      • Check Condition: range(0, 5)
        • counter < 5
        • 0 < 5
        • True
      • Condition is True, Execute Body of for Loop
        • Execute print(“Enter value of ages[“,counter,”]: “)
          • Output Screen
            • Enter value of ages[0]:
            • –
          • Execute age = int(input())
            • Output Screen
              • Enter value of ages[0]:
              • 25
            • age[0] = 25
          • Execute ages += [age]
            • Output Screen
              • Enter value of ages[0]:
              • 25
            • ages[0] += [25]
 
Index 0 1 2 3 4
Value 25        
Address DD20 DD24 DD28 DD32 DD36
 
  • Body of Loop is completely executed. Move to Increment Statement
    • Execute for counter in range(0,5):
      • counter = 1
      • Output Screen
        • Enter value of ages[0]:
        • 25
 
Index 0 1 2 3 4
Value 25        
Address DD20 DD24 DD28 DD32 DD36
 
  • Iteration 2
    • Check Condition
      • counter < 5
      • 1 < 5
      • True
    • Condition is True, Execute Body of for Loop
      • Execute print(“Enter value of ages[“,counter,”]: “)
        • Output Screen
          • Enter value of ages[0]:
          • 25
          • Enter value of ages[1]:
          • –
        • Execute age = int(input())
          • Output Screen
            • Enter value of ages[0]:
            • 25
            • Enter value of ages[1]:
            • 30
          • age[0] = 25
          • age[1] = 30
        • Execute ages += [age]
          • Output Screen
            • Enter value of ages[0]:
            • 25
            • Enter value of ages[1]:
            • 30
          • ages[1] +=[30]
 
Index 0 1 2 3 4
Value 25 30      
Address DD20 DD24 DD28 DD32 DD36
 
  • Body of Loop is completely executed. Move to Increment Statement
    • Execute for counter in range(0,5):
      • counter = 2
      • Output Screen
        • Enter value of ages[0]:
        • 25
        • Enter value of ages[1]:
        • 30
 
Index 0 1 2 3 4
Value 25 30      
Address DD20 DD24 DD28 DD32 DD36
 
  • Iteration 3
    • Check Condition
      • counter < 5
      • 2 < 5
      • True
    • Condition is True, Execute Body of for Loop
      • Execute print(“Enter value of ages[“,counter,”]: “)
        • Output Screen
          • Enter value of ages[0]:
          • 25
          • Enter value of ages[1]:
          • 30
          • Enter value of ages[2]:
          • –
        • Execute age = int(input())
          • Output Screen
            • Enter value of ages[0]:
            • 25
            • Enter value of ages[1]:
            • 30
            • Enter value of ages[2]:
            • 40
          • age[0] = 25
          • age[1] = 30
          • age[2] = 40
        • Execute ages += [age]
          • Output Screen
            • Enter value of ages[0]:
            • 25
            • Enter value of ages[1]:
            • 30
            • Enter value of ages[2]:
            • 40
          • ages[2] +=[40]
 
Index 0 1 2 3 4
Value 25 30 40    
Address DD20 DD24 DD28 DD32 DD36
 
  • Body of Loop is completely executed. Move to Increment Statement
    • Execute for counter in range(0,5):
      • counter = 3
      • Output Screen
        • Enter value of ages[0]:
        • 25
        • Enter value of ages[1]:
        • 30
        • Enter value of ages[2]:
        • 40
 
Index 0 1 2 3 4
Value 25 30 40    
Address DD20 DD24 DD28 DD32 DD36
 
  • Iteration 4
    • Check Condition
      • counter < 5
      • 3 < 5
      • True
    • Condition is True, Execute Body of for Loop
      • Execute print(“Enter value of ages[“,counter,”]: “)
        • Output Screen
          • Enter value of ages[0]:
          • 25
          • Enter value of ages[1]:
          • 30
          • Enter value of ages[2]:
          • 40
          • Enter value of ages[3]:
          • –
        • Execute age = int(input())
          • Output Screen
            • Enter value of ages[0]:
            • 25
            • Enter value of ages[1]:
            • 30
            • Enter value of ages[2]:
            • 40
            • Enter value of ages[3]:
            • 10
          • age[0] = 25
          • age[1] = 30
          • age[2] = 40
          • age[3] = 10
        • Execute ages += [age]
          • Output Screen
            • Enter value of ages[0]:
            • 25
            • Enter value of ages[1]:
            • 30
            • Enter value of ages[2]:
            • 40
            • Enter value of ages[3]:
            • 10
          • ages[3] +=[10]
 
Index 0 1 2 3 4
Value 25 30 40 10  
Address DD20 DD24 DD28 DD32 DD36
 
  • Body of Loop is completely executed. Move to Increment Statement
    • Execute for counter in range(0,5):
      • counter = 4
      • Output Screen
        • Enter value of ages[0]:
        • 25
        • Enter value of ages[1]:
        • 30
        • Enter value of ages[2]:
        • 40
        • Enter value of ages[3]:
        • 10
 
Index 0 1 2 3 4
Value 25 30 40 10  
Address DD20 DD24 DD28 DD32 DD36
 
  • Iteration 5
    • Check Condition
      • counter < 5
      • 4 < 5
      • True
    • Condition is True, Execute Body of for Loop
      • Execute print(“Enter value of ages[“,counter,”]: “)
        • Output Screen
          • 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]:
          • –
        • Execute age = int(input())
          • Output Screen
            • 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
          • age[0] = 25
          • age[1] = 30
          • age[2] = 40
          • age[3] = 10
          • age[4] = 5
        • Execute ages += [age]
          • Output Screen
            • 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
          • ages[4] +=[10]
 
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36
  
  • Body of Loop is completely executed. Move to Increment Statement
    • Execute for counter in range(0,5):
      • counter = 5
      • Output Screen
        • 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
      • age[0] = 25
      • age[1] = 30
      • age[2] = 40
      • age[3] = 10
      • age[4] = 5
 
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36
 
  • Iteration 06
    • Check Condition: range(0, 5)
      • counter < 5
      • 5 < 5
      • False
    • Condition is False. Loop will terminate.
 
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36
 
				
					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
  • Execute ages = [25, 30, 40, 10, 5]

 

Index01234
Value253040105
AddressDD20DD24DD28DD32 DD36

 

    • Output Screen
      • –
  • Execute print(“Value of First Array Element is: “, ages[0])
    • Output Screen
      • Value of First Array Element is: 25
      • –
  • 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
      • –
  • 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
      • –
  • 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
      • –
  • 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
      • –
				
					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
  • Start Value of counter = Start Value of Index
  • End Value of counter = End Value of Index
Dry Run
  • 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 counter = 0
 
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36
  • Output Screen
  • –
  • Execute the for loop
    • Iteration 1
      • Execute for age in ages:
    • age = ages[counter]
    • age = 25
      • Condition is True, Execute Body of for Loop
        • Execute print(age)
          • Output Screen
            • 25
            • –
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36
 
  • Execute counter = counter + 1
  • counter = 0 + 1
  • counter = 1
    • Output Screen
      • 25
      • –
    • Body of Loop is completely executed
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36
 
  • Iteration 2
    • Execute for age in ages:
  • age = ages[counter]
  • age = 30
    • Condition is True, Execute Body of for Loop
      • Execute print(age)
        • Output Screen
          • 25
          • 30
          • –
 
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36
 
  • Execute counter = counter + 1
  • counter = 1 + 1
  • counter = 2
    • Output Screen
      • 25
      • 30
      • –
    • Body of Loop is completely executed
  • Iteration 3
    • Execute for age in ages:
  • age = ages[counter]
  • age = 40
    • Condition is True, Execute Body of for Loop
      • Execute print(age)
        • Output Screen
          • 25
          • 30
          • 40
          • –
 
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36
 
  • Execute counter = counter + 1
  • counter = 2 + 1
  • counter = 3
    • Output Screen
      • 25
      • 30
      • 40
      • –
    • Body of Loop is completely executed
  • Iteration 4
    • Execute for age in ages:
  • age = ages[counter]
  • age = 10
    • Condition is True, Execute Body of for Loop
      • Execute print(age)
        • Output Screen
          • 25
          • 30
          • 40
          • 10
          • –
 
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36
 
  • Execute counter = counter + 1
  • counter = 3 + 1
  • counter = 4
    • Output Screen
      • 25
      • 30
      • 40
      • 10
      • –
    • Body of Loop is completely executed
  • Iteration 5
    • Execute for age in ages:
  • age = ages[counter]
  • age = 5
    • Condition is True, Execute Body of for Loop
      • Execute print(age)
        • Output Screen
          • 25
          • 30
          • 40
          • 10
          • 5
          • –
 
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36
 
  • Execute counter = counter + 1
  • counter = 4 + 1
  • counter = 5
    • Output Screen
      • 25
      • 30
      • 40
      • 10
      • 5
      • –
    • Body of Loop is completely executed
  • Iteration 6
    • Execute for age in ages:
      • age = ages[counter]
      • age = –
    • Condition is False. Loop will terminate.
 
Index 0 1 2 3 4
Value 25 30 40 10 5
Address DD20 DD24 DD28 DD32 DD36
				
					25
30
40
10
5
				
			
  • Example - Methods 3: Display Entire Array at Once
				
					'''
__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 Entire Array at Once

# Input + Output

try:  
    ages = [25, 30, 40, 10, 5]
    print(ages)
except:
    print("Error Occurred")


				
			
Memory and Index (Subscript) Representation of Array ages
  • Execute ages = [25, 30, 40, 10, 5]

 

Index01234
Value253040105
AddressDD20DD24DD28DD32 DD36
				
					25
30
40
10
5
				
			

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • Questions
    • Write Function in Python Programs for all the Tasks given below by following the Steps given in this Lecture considering the following variation of Function Prototype i.e.,
      • Solving Programming Tasks using Functions with Function Prototype: 
        • No Return Type, No Arguments / Parameters
        • No Return Type, Arguments / Parameters
        • Return Type, No Arguments / Parameters
        • Return Type, Arguments / Parameters

                        Task 1

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called radius_of_circle()) which takes a circumference (Float Number) as Input from User and prints its radius of Circle on the Output Screen.

·        r = C / 2PI

Required Output

Enter Circumference of Circle: 20.0

Radius of Circle =20 / 2*3.14 = 3.18

                        Task 2

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called area_of_sphere()) which takes radius (Float Number) as Input and prints surface area of sphere on the Output Screen.
    • surface area of sphere =  4πr2

Required Output

Enter Radius of Sphere: 9.45

Surface Area of Sphere = 4 * 3.14 * 9.45 = 118.7

                        Task 3

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called volume_of_cube()) which takes edge length (Float Number i.e., a) as Input and prints volume of cube on the Output Screen.

·        Volume of Cube =  a3

Required Output   

Enter Edge Length of Cube: 1.65

Volume of Cube = 4.49

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 different Tasks which are very similar to the ones given in the TODO Tasks
    • For each Task write a Python Program using the following Steps mentioned in this Lecture considering the following variation of Function Prototype i.e.,
      • Solving Programming Tasks using Functions with Function Prototype: 
        • No Return Type, No Arguments / Parameters
        • No Return Type, Arguments / Parameters
        • Return Type, No Arguments / Parameters
        • Return Type, Arguments / Parameters

Chapter Summary

  • Chapter Summary

In this Chapter, I presented the following main concepts:

  • Array
    • An Array (a.k.a. Subscripted Variable) is defined as a collective name assigned to a group of similar quantities
  • 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]
  • Array Element vs Normal Variable
    • Each Array Element behaves like a Normal Variable i.e.,
      • All Operations that we can perform on a Normal Variable can also be performed on an Array Element
  • 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
  • Methods to Store Values 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

In Next Chapter

  • In Next Chapter
  • In Sha Allah, in the next Chapter, I will present a detailed discussion on
  • Strings in Python
Chapter 28 - Data Structures in Python
  • Next
Chapter 30 - Strings
  • Next
Share this article!
Facebook
Twitter
LinkedIn
About Us

Ilm 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.

Quick Links
  • About Us
  • Contact
Useful Links
  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • Support
  • FAQ
Subscribe Our Newsletter
Facebook Twitter Linkedin Instagram Youtube

© 2022 Ilmo Irfan. All Rights Reserved.