Seu SlideShare está sendo baixado. ×

# Arrays Introduction.pptx

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
numpy.pdf
Carregando em…3
×

1 de 32 Anúncio

# Arrays Introduction.pptx

Introduction to Lists in python

Introduction to Lists in python

Anúncio
Anúncio

Anúncio

### Arrays Introduction.pptx

1. 1. Arrays An Introduction
2. 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. 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. 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. 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. 6. Types of Arrays 1. one dimensional ( We will be doing this today) 2. Two dimensional ( future lessons)
7. 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. 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. 9. Declaring and initializing arrays ● Syntax for declaring an array: var_name[size] ● Syntax for initializing an array: var_name[] = [val1, val2, ..., valn]
10. 10. Accessing elements of an array ● Syntax: "array_name[index]" ● Example: "arr[0] = 1, arr[1] = 2, arr[2] = 3, etc."
11. 11. Modifying elements of an array ● Syntax: "array_name[index] = value" ● Example: "arr[2] = 10"
12. 12. Common operations on arrays ● Traversing an array ● Inserting an element ● Deleting an element ● Searching an element
13. 13. Create an array using python To create an array in Python, you can use the array module from the Python Standard Library:
14. 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. 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. 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. 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. 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. 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. 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. 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. 22. Search To access the elements of the list "Fname" in Python, you can use the following syntax:
23. 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. 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. 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. 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. 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. 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. 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. 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. 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. 32. Questions SEE ACTIVITY ON GOOGLE CLASSROOM

### Notas do Editor

• Fname = ["Elayna", "Jaya", "Hazel", "Anisa", "Seraya", "Tilly", "Izzy", "Ana", "Abigail", "Natasha", "Ruby", "Belinda", "Lexi"]
for i in range(0, len(Fname)):
print(Fname[i])
• 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!