0% found this document useful (0 votes)
39 views35 pages

Arrays Grade08

Uploaded by

Linh Thuỳ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views35 pages

Arrays Grade08

Uploaded by

Linh Thuỳ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

ARRAY

LIST USING PYTHON

Anjan Mahanta
[email protected]
List | Working with multiple
records

2
Formatting the output

3
10.2 Arrays

● An array is a data structure containing several elements of the same data


type; these elements can be accessed using the same identifier name.
● The position of each element in an array is identified using the array’s index.
● The index of the first element in an array is the lower bound and the index
of the last element is the upper bound.
● The lower bound of an array is usually set as zero or one. Some
programming languages can automatically set the lower bound of an array.
● Arrays can be one-dimensional or multi-dimensional.
4
10.2.1 One Dimensional Arrays

● A 1D array can be referred to as a list.


● Here is an example of a list with nine
elements and
a lower bound of zero.

5
10.2.1 One Dimensional Arrays

6
Pseudocode

OUTPUT "Enter these 9 values in order 27, 19, 36, 42, 16, 89, 21, 16,
55"

FOR counter <-- 0 TO 8


OUTPUT "Enter next value "
INPUT myList[counter]
NEXT counter

7
Python

8
Dynamic Array

● Initialize a blank array


● Ask the user for the size of the array
● Enter the values
● Add the values to the list of array
● Display the array list with the inserted values

9
Dynamic Array

a=[]
size=int(input("Please enter the size of the array:
"))
for i in range(size):
value=int(input("Enter the values: "))
a.append(value)
print("The list of array: ", a)

10
Search for a value

#Create an array
a=[]
size=int(input("Please enter the size of the array: "))
for i in range(size):
value=int(input("Enter the values: "))
a.append(value)
print("The list of array: ", a)

#Search for a value


s=int(input("Enter the search value: "))
j=0
while j<len(a):
if a[j]==s:
print("Found at", j)
j=j+1 11
Linear Search | Static

list=["A","B","C","B"]
flag=0
find=input("Enter the search element: ")
for i in range(len(list)):
if find==list[i]:
print("Found at ",i)
flag=1
if flag!=1:
print("Not Found")
12
Linear Search | Dynamic

list=[]
n=int(input("Enter the length of array: "))
for i in range(n):
array_element=input("Enter the element: ")
list.append(array_element)
print(list)
flag=0
find=input("Enter the search element: ")
for i in range(len(list)):
if find==list[i]:
print("Found at ",i)
flag=1
if flag!=1:
print("Not Found")
13
Sorted Array
#Create an array
a=[]
size=int(input("Please enter the size of the array: "))
for i in range(size):
value=input("Enter the values: ")
a.append(value)
print("The list of array: ", a)
#Sort the array
a.sort()
print("The shorted list is ",a) 14
Delete from a List

subject_list=["maths", "science", "english", "computer", "french"]


print(subject_list)
dislike=input("Which of the subjects do you dislike?: ")
getrid=subject_list.index(dislike)
del subject_list[getrid]
print(subject_list)

15
Problem | Adding to the list | Part 1

16
Solution | Adding to the list | Part 1

17
Solution | Adding to the list | Part 1
name1=input("Enter the name of somebody you want to invite to your party:
")
name2=input("Enter another name: ")
name3=input("Enter a third name: ")
party_list=[name1,name2,name3]
another=input("Do you want to invite another? (Y/N): ")
while another=="Y":
newname=party_list.append(input("Enter another name: "))
another=input("Do you want to invite another? (Y/N): ")
print("You have ",len(party_list), "people coming to your party!")

18
Solution | Adding to the list | Part 1

19
Problem | Adding to the list | Part 2

20
Solution | Adding to the list | Part 2

21
Solution | Adding to the list | Part 2
name1=input("Enter the name of somebody you want to invite to your party: ")
name2=input("Enter another name: ")
name3=input("Ehter a third name: ")
party_list=[name1,name2,name3]
another=input("Do you want to invite another? (Y/N): ")
while another=="Y":
newname=party_list.append(input("Enter another name: "))
another=input("Do you want to invite another? (Y/N): ")
print("You have ",len(party_list), "people coming to your party!")
selection=input("Enter one of the names: ")
print(selection, "is in position ", party_list.index(selection), "on the list")
stillcome=input("Do you still want them to come (Y/N): ")
if stillcome=="N":
party_list.remove(selection)
print(party_list) 22
Solution | Adding to the list | Part 2

23
10.2.2 Two Dimensional Arrays

● A 2D array can be referred to


as a table, with rows and
columns.
● Here is an example of a table
with nine rows and three
columns (27 elements) and
lower bounds of zero.

24
10.2.2 Two Dimensional Arrays

25
Python

26
2 D Array | Challenge 1

27
2 D Array | Challenge 1

28
2 D Array | Challenge 2

29
2 D Array | Challenge 2

30
2 D Array | Challenge 3

31
2 D Array | Challenge 3

32
2 D Array | Challenge 4

33
2 D Array | Challenge 4

34
Dynamic 2 D Array

35

You might also like