SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Arrays
An Introduction
What is an array?
● Definition: "An array is a collection of items stored at contiguous memory locations." It is a
form of data structure
● Example: "An array of integers, an array of floating point numbers, an array of characters,
etc."
What is a data structure?
A data structure is a way of organizing and storing data in a computer so that it can be accessed and modified efficiently.
Arrays are a fundamental data structure that stores a sequence of elements, all of the same type, in contiguous memory
locations. The elements in an array are accessed by their index, which is an integer value that corresponds to the position
of the element in the array.
Uses of Arrays
Arrays are used in many types of applications, from low-level memory management to high-level data manipulation.
1. Storing large amounts of data: Arrays are a simple and efficient way to store large amounts of data in a single data
structure. They can be used to store large amounts of data such as images, videos, or audio files.
2. Iteration and Traversal: Arrays are used to traverse through all of its elements, one by one, which is an essential
operation in many algorithms.
3. Mathematical operations: Arrays can be used to perform mathematical operations on large sets of data, such as
finding the average, median, or standard deviation of a set of numbers.
4. Dynamic Programming: Arrays can be used to store and retrieve information in dynamic programming problems,
which are used to solve complex problems by breaking them down into smaller sub-problems.
Uses of Arrays
1. Searching and Sorting: Arrays are used as the underlying data structure for many searching and sorting algorithms,
such as binary search, bubble sort, and quick sort.
2. Representing tables: Arrays can be used to represent tables of data, such as a spreadsheet. Two-dimensional arrays
are particularly useful for this, as they can be used to store the rows and columns of a table.
3. Implementing other data structures: Arrays can be used as the underlying data structure for other data structures,
such as linked lists, stacks, and queues.
4. Graphs: Arrays can be used to represent graphs, where each element in the array represents a node in the graph and
the connections between them can be represented by arrays of pointers to other elements in the array
Types of Arrays
1. one dimensional ( We will be doing this today)
2. Two dimensional ( future lessons)
Two-dimensional arrays
A two-dimensional array, also known as a matrix, is an array
of arrays. It is a data structure that stores a collection of
elements, where each element is itself an array.
In Python, you can create a two-dimensional array using a
list of lists. For example:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Why use arrays?
Advantages:
● Efficient to access elements using indexes
● Can store elements of the same data type
● Can be used to store large amounts of data
Declaring and initializing arrays
● Syntax for declaring an array: var_name[size]
● Syntax for initializing an array: var_name[] = [val1,
val2, ..., valn]
Accessing elements of an array
● Syntax: "array_name[index]"
● Example: "arr[0] = 1, arr[1] = 2, arr[2] = 3, etc."
Modifying elements of an array
● Syntax: "array_name[index] = value"
● Example: "arr[2] = 10"
Common operations on arrays
● Traversing an array
● Inserting an element
● Deleting an element
● Searching an element
Create an array using python
To create an array in Python, you can use the array module
from the Python Standard Library:
Alternatively, you can use a list to create an array:
Lists in Python are very similar to arrays, but they can store elements of different data types and can be resized dynamically.
REMEMBER WHEN WE ARE DOING ACTIVITIES, WE WILL
LIMIT THE AMOUNT OF LIBRARY FUNCTIONS WE USE TO
ANSWER QUESTIONS. THIS IS MANDATORY AS IT WILL HELP
YOU IMMENSELY
Example
Array of students First name in L5CS
We write the pseudocode just like how we write in python
lists
Fname = ["Elayna", "Jaya", "Hazel", "Anisa", "Seraya",
"Tilly", "Izzy", "Ana", "Abigail", "Natasha", "Ruby",
"Belinda", "Lexi"]
Create Array continued
But what if we wanted to input the data using a loop.
arr = []
for i in range(13):
value = input("Enter an element: ")
arr.append(value)
print(arr)
Notice we created an empty array first
This code creates an empty array called arr, then uses a for loop to iterate
13 times. On each iteration, it prompts the user to enter an element using
the input() function, and appends the value to the arr using the append()
method. After the loop is finished, it prints the resulting array.
You can also use this for loop for specific data types, for instance a loop for integers:
arr = []
for i in range(13):
value = int(input("Enter an
integer: "))
arr.append(value)
print(arr)
Note that input() function always returns a string, so if you want to store integers or
floating-point numbers, you'll need to convert the input to the appropriate data type
using int() or float() functions before appending them to the array.
Traversing an array
Traversing an array means iterating through all of its elements, one by one. The process of traversing an array is also known as
"iterating" or "looping through" an array.
There are a few ways to traverse an array in Python, the most common of which is using a for loop. Here is an example of
how you can use a for loop to traverse an array:
arr = [1, 2, 3, 4, 5]
for element in arr:
print(element)
In this example, the for loop iterates over the arr array, and assigns each element in turn to the variable element. The code
inside the loop, print(element), is then executed for each element in the array.
arr = [1, 2, 3, 4, 5]
i = 0
while i < len(arr):
print(arr[i])
i += 1
Another way to traverse an array is with the while loop:
In this example, we initialize a variable i to 0, and use it as the index for the array.
And inside the while loop we check if i is less than the length of the array, if so we
print the element at index i and then increase the value of i by 1.
Traversing an array allows you to perform operations on all the elements of the array, such as printing them, summing them,
counting the number of occurrences of a specific element, or even modifying the array by applying some operations.
It's important to mention that the order of traversing an array is important, depending on the algorithm or data structure you are
implementing, you might need to traverse the array in a specific order like ascending or descending.
Search
To access the elements of the list "Fname" in Python, you can use the following syntax:
You can also use a loop to iterate over the elements of the list. For example:
This will print each name in the list on a new line. You can also use the len() function to
get the length of the list, and use this value as the stopping condition in a loop. For
example:
for i in range(len(Fname)):
print(Fname[i])
This will also print each name in the list on a new line.
x = ['p','y','t','h','o','n']
print(x.index('o'))
Python has a method to search for an element in an array, known as index().
We can find an index using:
Fname = ["Elayna", "Jaya", "Hazel", "Anisa", "Seraya", "Tilly", "Izzy", "Ana",
"Abigail", "Natasha", "Ruby", "Belinda", "Lexi"]
print (Fname[2])
The above will print the third person in the array.
Use the for loop in python to find an element in the List
To use a for loop to find an element in a list in
Python, you can use the following code:
# Search for the element "Ruby" in the list
search_element = "Ruby"
for name in Fname:
if name == search_element:
print(f"'{search_element}' was found in the list.")
break
else:
print(f"'{search_element}' was not found in the
list.")
You can also use the in operator to check if an element
is in the list. For example:
if search_element in Fname:
print(f"'{search_element}' was found in the list.")
else:
print(f"'{search_element}' was not found in the
list.")
This will return True if the element is in the list and False if it is not.
This will iterate over the elements of the list and check if each element is equal to the
search element. If it is, it will print a message indicating that the element was found,
and then exit the loop using the break statement. If the element is not found, the loop
will complete and the else block will be executed, printing a message indicating that
the element was not found.
print(f"'{search_element}' was found in the list.") means
it is a way to format your string that is more readable and fast.used in later python compilers.
Fname = ["Elayna", "Jaya", "Hazel", "Anisa", "Seraya",
"Tilly", "Izzy", "Ana", "Abigail", "Natasha", "Ruby",
"Belinda", "Lexi"]
print('Enter a name:')
search_element = input()
for name in Fname:
if name == search_element:
print(f"'{search_element}' was found in the list.")
break
else:
print(f"'{search_element}' was not found in the list.")
Another Solution
You might be more comfortable with this solution
Fname = ["Elayna", "Jaya", "Hazel", "Anisa", "Seraya",
"Tilly", "Izzy", "Ana", "Abigail", "Natasha", "Ruby",
"Belinda", "Lexi"]
print('Enter a name:')
search_element = input()
for name in Fname:
if name == search_element:
print( search_element, "was found in the list.")
break
else:
print( search_element, "was not found in the list.")
This will create a list called "Fname" that contains the
names you provided as strings. You can access the elements of
the list by their index, for example Fname[0] would return
"Elayna". You can also use list operations such as len(Fname)
to get the length of the list, or "Elayna" in Fname to check
if a specific element is in the list.
We will instead use loops to search, sort, add, remove and
find elements in the list
CONCLUSION
I have shown you how to search and traverse an array. There
are TWO other featureS you need to learn namely.
● Inserting an element(s) IN AN ARRAY
● Deleting an element(s) FROM AN ARRAY
You will be required to create python codes using
1. The inbuilt functions
2. For loop
For EACH.
IT IS IMPORTANT TO NOTE THAT ARRAYS ALWAYS APPEAR IN YOUR
GCSE EXAMS
Questions
SEE ACTIVITY ON GOOGLE CLASSROOM

Weitere Àhnliche Inhalte

Ähnlich wie Arrays Introduction.pptx

Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structuresSenthil Murugan
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxAmanRai352102
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2Abbott
 
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesswathirajstar
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programmingTaseerRao
 
arrayppt.pptx
arrayppt.pptxarrayppt.pptx
arrayppt.pptxshivas379526
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptxssuser8e50d8
 
Week 11.pptx
Week 11.pptxWeek 11.pptx
Week 11.pptxCruiseCH
 
data structure programing language in c.ppt
data structure programing language in c.pptdata structure programing language in c.ppt
data structure programing language in c.pptLavkushGupta12
 
DATA STRUCTURE AND ALGORITJM POWERPOINT.ppt
DATA STRUCTURE AND ALGORITJM POWERPOINT.pptDATA STRUCTURE AND ALGORITJM POWERPOINT.ppt
DATA STRUCTURE AND ALGORITJM POWERPOINT.pptyarotos643
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09Terry Yoast
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1Kumar
 

Ähnlich wie Arrays Introduction.pptx (20)

Data structures
Data structures Data structures
Data structures
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
 
Array ppt
Array pptArray ppt
Array ppt
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
Arrays
ArraysArrays
Arrays
 
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notes
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
arrayppt.pptx
arrayppt.pptxarrayppt.pptx
arrayppt.pptx
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
Arrays
ArraysArrays
Arrays
 
Week 11.pptx
Week 11.pptxWeek 11.pptx
Week 11.pptx
 
M v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notesM v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notes
 
data structure programing language in c.ppt
data structure programing language in c.pptdata structure programing language in c.ppt
data structure programing language in c.ppt
 
set.pptx
set.pptxset.pptx
set.pptx
 
DATA STRUCTURE AND ALGORITJM POWERPOINT.ppt
DATA STRUCTURE AND ALGORITJM POWERPOINT.pptDATA STRUCTURE AND ALGORITJM POWERPOINT.ppt
DATA STRUCTURE AND ALGORITJM POWERPOINT.ppt
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 

KĂŒrzlich hochgeladen

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍾 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍾 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍾 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍾 8923113531 🎰 Avail...gurkirankumar98700
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

KĂŒrzlich hochgeladen (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍾 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍾 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍾 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍾 8923113531 🎰 Avail...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Arrays Introduction.pptx

  • 2. What is an array? ● Definition: "An array is a collection of items stored at contiguous memory locations." It is a form of data structure ● Example: "An array of integers, an array of floating point numbers, an array of characters, etc."
  • 3. What is a data structure? A data structure is a way of organizing and storing data in a computer so that it can be accessed and modified efficiently. Arrays are a fundamental data structure that stores a sequence of elements, all of the same type, in contiguous memory locations. The elements in an array are accessed by their index, which is an integer value that corresponds to the position of the element in the array.
  • 4. Uses of Arrays Arrays are used in many types of applications, from low-level memory management to high-level data manipulation. 1. Storing large amounts of data: Arrays are a simple and efficient way to store large amounts of data in a single data structure. They can be used to store large amounts of data such as images, videos, or audio files. 2. Iteration and Traversal: Arrays are used to traverse through all of its elements, one by one, which is an essential operation in many algorithms. 3. Mathematical operations: Arrays can be used to perform mathematical operations on large sets of data, such as finding the average, median, or standard deviation of a set of numbers. 4. Dynamic Programming: Arrays can be used to store and retrieve information in dynamic programming problems, which are used to solve complex problems by breaking them down into smaller sub-problems.
  • 5. Uses of Arrays 1. Searching and Sorting: Arrays are used as the underlying data structure for many searching and sorting algorithms, such as binary search, bubble sort, and quick sort. 2. Representing tables: Arrays can be used to represent tables of data, such as a spreadsheet. Two-dimensional arrays are particularly useful for this, as they can be used to store the rows and columns of a table. 3. Implementing other data structures: Arrays can be used as the underlying data structure for other data structures, such as linked lists, stacks, and queues. 4. Graphs: Arrays can be used to represent graphs, where each element in the array represents a node in the graph and the connections between them can be represented by arrays of pointers to other elements in the array
  • 6. Types of Arrays 1. one dimensional ( We will be doing this today) 2. Two dimensional ( future lessons)
  • 7. Two-dimensional arrays A two-dimensional array, also known as a matrix, is an array of arrays. It is a data structure that stores a collection of elements, where each element is itself an array. In Python, you can create a two-dimensional array using a list of lists. For example: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  • 8. Why use arrays? Advantages: ● Efficient to access elements using indexes ● Can store elements of the same data type ● Can be used to store large amounts of data
  • 9. Declaring and initializing arrays ● Syntax for declaring an array: var_name[size] ● Syntax for initializing an array: var_name[] = [val1, val2, ..., valn]
  • 10. Accessing elements of an array ● Syntax: "array_name[index]" ● Example: "arr[0] = 1, arr[1] = 2, arr[2] = 3, etc."
  • 11. Modifying elements of an array ● Syntax: "array_name[index] = value" ● Example: "arr[2] = 10"
  • 12. Common operations on arrays ● Traversing an array ● Inserting an element ● Deleting an element ● Searching an element
  • 13. Create an array using python To create an array in Python, you can use the array module from the Python Standard Library:
  • 14. Alternatively, you can use a list to create an array: Lists in Python are very similar to arrays, but they can store elements of different data types and can be resized dynamically.
  • 15. REMEMBER WHEN WE ARE DOING ACTIVITIES, WE WILL LIMIT THE AMOUNT OF LIBRARY FUNCTIONS WE USE TO ANSWER QUESTIONS. THIS IS MANDATORY AS IT WILL HELP YOU IMMENSELY
  • 16. Example Array of students First name in L5CS We write the pseudocode just like how we write in python lists Fname = ["Elayna", "Jaya", "Hazel", "Anisa", "Seraya", "Tilly", "Izzy", "Ana", "Abigail", "Natasha", "Ruby", "Belinda", "Lexi"]
  • 17. Create Array continued But what if we wanted to input the data using a loop. arr = [] for i in range(13): value = input("Enter an element: ") arr.append(value) print(arr) Notice we created an empty array first
  • 18. This code creates an empty array called arr, then uses a for loop to iterate 13 times. On each iteration, it prompts the user to enter an element using the input() function, and appends the value to the arr using the append() method. After the loop is finished, it prints the resulting array.
  • 19. You can also use this for loop for specific data types, for instance a loop for integers: arr = [] for i in range(13): value = int(input("Enter an integer: ")) arr.append(value) print(arr) Note that input() function always returns a string, so if you want to store integers or floating-point numbers, you'll need to convert the input to the appropriate data type using int() or float() functions before appending them to the array.
  • 20. Traversing an array Traversing an array means iterating through all of its elements, one by one. The process of traversing an array is also known as "iterating" or "looping through" an array. There are a few ways to traverse an array in Python, the most common of which is using a for loop. Here is an example of how you can use a for loop to traverse an array: arr = [1, 2, 3, 4, 5] for element in arr: print(element) In this example, the for loop iterates over the arr array, and assigns each element in turn to the variable element. The code inside the loop, print(element), is then executed for each element in the array.
  • 21. arr = [1, 2, 3, 4, 5] i = 0 while i < len(arr): print(arr[i]) i += 1 Another way to traverse an array is with the while loop: In this example, we initialize a variable i to 0, and use it as the index for the array. And inside the while loop we check if i is less than the length of the array, if so we print the element at index i and then increase the value of i by 1. Traversing an array allows you to perform operations on all the elements of the array, such as printing them, summing them, counting the number of occurrences of a specific element, or even modifying the array by applying some operations. It's important to mention that the order of traversing an array is important, depending on the algorithm or data structure you are implementing, you might need to traverse the array in a specific order like ascending or descending.
  • 22. Search To access the elements of the list "Fname" in Python, you can use the following syntax:
  • 23. You can also use a loop to iterate over the elements of the list. For example: This will print each name in the list on a new line. You can also use the len() function to get the length of the list, and use this value as the stopping condition in a loop. For example: for i in range(len(Fname)): print(Fname[i]) This will also print each name in the list on a new line.
  • 24. x = ['p','y','t','h','o','n'] print(x.index('o')) Python has a method to search for an element in an array, known as index(). We can find an index using: Fname = ["Elayna", "Jaya", "Hazel", "Anisa", "Seraya", "Tilly", "Izzy", "Ana", "Abigail", "Natasha", "Ruby", "Belinda", "Lexi"] print (Fname[2]) The above will print the third person in the array.
  • 25. Use the for loop in python to find an element in the List To use a for loop to find an element in a list in Python, you can use the following code: # Search for the element "Ruby" in the list search_element = "Ruby" for name in Fname: if name == search_element: print(f"'{search_element}' was found in the list.") break else: print(f"'{search_element}' was not found in the list.")
  • 26. You can also use the in operator to check if an element is in the list. For example: if search_element in Fname: print(f"'{search_element}' was found in the list.") else: print(f"'{search_element}' was not found in the list.") This will return True if the element is in the list and False if it is not.
  • 27. This will iterate over the elements of the list and check if each element is equal to the search element. If it is, it will print a message indicating that the element was found, and then exit the loop using the break statement. If the element is not found, the loop will complete and the else block will be executed, printing a message indicating that the element was not found. print(f"'{search_element}' was found in the list.") means it is a way to format your string that is more readable and fast.used in later python compilers.
  • 28. Fname = ["Elayna", "Jaya", "Hazel", "Anisa", "Seraya", "Tilly", "Izzy", "Ana", "Abigail", "Natasha", "Ruby", "Belinda", "Lexi"] print('Enter a name:') search_element = input() for name in Fname: if name == search_element: print(f"'{search_element}' was found in the list.") break else: print(f"'{search_element}' was not found in the list.") Another Solution
  • 29. You might be more comfortable with this solution Fname = ["Elayna", "Jaya", "Hazel", "Anisa", "Seraya", "Tilly", "Izzy", "Ana", "Abigail", "Natasha", "Ruby", "Belinda", "Lexi"] print('Enter a name:') search_element = input() for name in Fname: if name == search_element: print( search_element, "was found in the list.") break else: print( search_element, "was not found in the list.")
  • 30. This will create a list called "Fname" that contains the names you provided as strings. You can access the elements of the list by their index, for example Fname[0] would return "Elayna". You can also use list operations such as len(Fname) to get the length of the list, or "Elayna" in Fname to check if a specific element is in the list. We will instead use loops to search, sort, add, remove and find elements in the list
  • 31. CONCLUSION I have shown you how to search and traverse an array. There are TWO other featureS you need to learn namely. ● Inserting an element(s) IN AN ARRAY ● Deleting an element(s) FROM AN ARRAY You will be required to create python codes using 1. The inbuilt functions 2. For loop For EACH. IT IS IMPORTANT TO NOTE THAT ARRAYS ALWAYS APPEAR IN YOUR GCSE EXAMS
  • 32. Questions SEE ACTIVITY ON GOOGLE CLASSROOM

Hinweis der Redaktion

  1. Fname = ["Elayna", "Jaya", "Hazel", "Anisa", "Seraya", "Tilly", "Izzy", "Ana", "Abigail", "Natasha", "Ruby", "Belinda", "Lexi"] for i in range(0, len(Fname)): print(Fname[i])
  2. F"'{search_element} is just another way of outputting the print statement using string literals As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster!