0% found this document useful (0 votes)
2 views5 pages

Lesson 4 - Python - 1d Arrays

The document provides an overview of arrays in Python, explaining how to create, access, and manipulate them using examples. It includes code snippets for creating an array of student names, finding the length of the array, adding elements, and checking for existing names. Additionally, it presents practice questions to reinforce understanding of array operations.

Uploaded by

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

Lesson 4 - Python - 1d Arrays

The document provides an overview of arrays in Python, explaining how to create, access, and manipulate them using examples. It includes code snippets for creating an array of student names, finding the length of the array, adding elements, and checking for existing names. Additionally, it presents practice questions to reinforce understanding of array operations.

Uploaded by

nothing12334343
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arrays

Arrays stores multiple values of the same datatype. In python we use list as arrays under
single variable/identifier.

How to create an array in Python

Create an array for students with names as following:


 Muneeb
 Sarah
 Shanzay
 Rameen

Syntax:

StdName = [“Muneeb”,”Sarah”,”Shanzay”,”Rameen”]

An array was created for student names with StName as its identifier and the records are
saved as string datatype.

Code:

StdName=["Muneeb","Sarah","Shanzay","Rameen"]

How to access the individual elements in the array

StdName[o] StdName[1] StdName[2] StdName[3]


Taking Output of the Array and its individual elements
Code:

StdName=["Muneeb","Sarah","Shanzay","Rameen"]

print(StdName)

print(StdName[3])

Output:

Note: Array index starts from 0


Finding the length of the array
Array length can be printed or referred to by using the length function applied to the whole
array. The output of the function may or may not be assigned to a variable
Code with variable:
StdName=["Muneeb","Sarah","Shanzay","Rameen"]
length=len(StdName)
print(length)

Output with variable:

Code without variable:


StdName=["Muneeb","Sarah","Shanzay","Rameen"]
print(len(StdName))

Output without variable:

Taking an output of all elements, can also be done using a loop which will output the
elements as a separate element

Code:
StdName=["Muneeb","Sarah","Shanzay","Rameen"]

for x in range(len(StdName)):
print(StdName[x])

Output:
Practice Question:
Consider the following elements for the array ‘Students’ and find the index of “Haroon” in
the array.
1. Salman
2. Waqas
3. Hadia
4. Rida
5. Haroon
6. Shanzay
7. Rameen
8. Fahad
9. Ahsan
10. Itrat

Code:
Students=["Salman","Waqas","Hadia","Rida","Haroon","Shanzay","Rameen","Faha
d","Ahsan","Itrat"]

for x in range(len(Students)):
if Students[x]=="Haroon":
print("Haroon is present in the array at index:",x)

Output:

Adding an element to the array


Add one more name ‘Ayesha’ to the array Students

Code:
Students=["Salman","Waqas","Hadia","Rida","Haroon","Shanzay","Rameen","Faha
d","Ahsan","Itrat"]
print(Students)
[Link]("Ayesha")
print(Students)

Output:
Practice Question:
Take an input of a student name. if that name exists in the array then print “The name you
have entered is already present in Students” and if the name is not present then add it to the
list/array.

Code:
Students = ["Salman", "Waqas", "Hadia", "Rida", "Haroon", "Shanzay",
"Rameen", "Fahad", "Ahsan", "Itrat", "Ayesha"]

NewStudent = input("Please input the name of the new student: ")


flag = False
x = 0

while x < len(Students):


if NewStudent == Students[x]:
flag = True
break
x = x + 1

if flag:
print("The name '" + NewStudent + "' is already present in the Students
list.")
else:
[Link](NewStudent)
print("'" + NewStudent + "' has been added to the Students list.")

print("Updated Students list: " + str(Students))

Output:

Note:
The break statement is used to exit the while loop immediately once a specific condition is
met.

If you don’t use break, the loop will continue checking every remaining name in the list,
even after the match is found — which is unnecessary and inefficient.

For example, if NewStudent is "Rida" (which is already in the list), as soon as you find it at
index 3, you don’t need to check the rest (Haroon, Shanzay, etc.).
Practice Question:
Define array Numbers [10,60,20,30,50,90] and reverse the order of the array using for loop

Code:
Numbers=[10,60,20,30,50,90]
new=[0,0,0,0,0,0]

opposite_index=5

for x in range(6):
new[x]=Numbers[opposite_index]
opposite_index=opposite_index-1
print(new)

Output:

Practice Question:
Ask Numbers form the user and find the average of those numbers and when the user types in
0 then stop asking the number from the user and print the average

Code:
sum=0
count=0

flag=True

while flag==True:
number=float(input("Please enter your number: "))

if number==0:
flag=False
else:
count=count+1
sum=sum+number
average=sum/count
print(average)

Output:

You might also like