0% found this document useful (0 votes)
27 views

Array (List in Python)

Here is a Python program to check the validity of enrollment numbers in a list: ```python # List of enrollment numbers enroll_nos = ["ENRNO-12345","ABCDE-67890","ENRNO-56789"] for enroll_no in enroll_nos: # Check if length is 11 characters if len(enroll_no) != 11: print(enroll_no+" is Invalid") continue # Split into first and second field first,second = enroll_no.split("-") # Check if first field is ENRNO and contains only letters if first != "ENRNO" or not first.isalpha(): print(enroll_no+" is Invalid

Uploaded by

chainz
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)
27 views

Array (List in Python)

Here is a Python program to check the validity of enrollment numbers in a list: ```python # List of enrollment numbers enroll_nos = ["ENRNO-12345","ABCDE-67890","ENRNO-56789"] for enroll_no in enroll_nos: # Check if length is 11 characters if len(enroll_no) != 11: print(enroll_no+" is Invalid") continue # Split into first and second field first,second = enroll_no.split("-") # Check if first field is ENRNO and contains only letters if first != "ENRNO" or not first.isalpha(): print(enroll_no+" is Invalid

Uploaded by

chainz
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/ 22

Arrays

Why variables are not


enough?
▪ The primary purpose of a computer system is to process data. The data can
be of any type.
▪ If a computer program needs to process one piece of data, then that data can
be stored in a variable and the resultant data can be stored in another
variable. In this case, a couple of variables are sufficient to write a program.
▪ But real-life programs need to handle tens of thousands of pieces of data. To
handle large amounts of data, it is impossible to store each piece of data in a
separate variable.
3

Data structure
▪ Because variables cannot be used for real-life programming, data is
grouped together or, more precisely, structured in a specific format. 
▪ The collection of data structured together is called a data structure.
▪ The different data structures in python are:
▪ Lists 
▪ Arrays
▪ Tuples 
4

Arrays
▪ An array is a data structure used to store a set of data elements in a certain
order.
Note: Array can be created in Python but it is less flexible as it is a
homogeneous collection of element which means an array may consist of
elements of only same data types
▪ Arrays can also be created using lists as it is a heterogeneous collection of
elements, which means a list may consist of elements of different data types. For
example,
myList = [‘six’, 7, 8, ‘nine’, ‘ten’]
▪ Similarly, a one-dimensional array is created by:
array1d=[10, 11, 12, 13, 14]
6

Lists
▪ A list is an ordered, sequential collection of zero or more elements.
▪ A list is a heterogeneous collection of elements, which means a list may
consist of elements of different data types.
▪ Lists are defined using elements in square brackets. For example,
myList=[1, 2, 3, 'four', 5]
7

Introduction
▪ A data structure is a logical model of a particular organisation of data. 
▪ An array is an ordered collection of elements of a similar data type. Two
types of arrays : one-dimensional array and two-dimensional array. 
▪ Lists: A list is an ordered, sequential collection of zero or more elements.
A list is a heterogeneous collection of elements, which means a list may
consist of elements of different data types.
▪ Tuples: Tuples are similar to lists, but tuples cannot be changed. It is like a
constant list.
Note: So we will be learning only list in Python for arrays concept.
Pseudocode for arrays will be later in Grade 9.
8

Lists
▪ Lists are defined using elements in square brackets. For example,
myList=[1, 2, 3, 'four', 5]
9

Lists
▪ For example,
fruits = ["apple", "banana", "watermelon", "kiwi", "grapes", "papaya"]
score = ["invalid", 10, 18, 20, 12, 15, "invalid", 19]
▪ A list can hold elements of different data types. Similar to arrays,
each element in a list is associated with a unique index. 
10

fruits

Lists List name


Data elements
Index
apple 0
fruits = ["apple", "banana", "watermelon",
"kiwi", "grapes", "papaya"] banana 1

watermelon 2

kiwi 3

grapes 4

papaya 5
11

List
▪ list1 is an empty list.
▪ None is a special value in Python
that holds a place when it has
nothing to say. In conditional
statements, it produces a False.
▪ list3 is a list with 10 elements.
12

Combining two lists


• Two lists can be combined using the + operator.
13

len() function
• The len() function returns the number of elements in a list.
• For example, finding the number of elements in the list fruits,
14

Example Program country tempList

▪ A list named country stores the country names.


▪ country=["Albania", "Algeria", "USA", "Andorra"] Albania 38

▪ For each country, the user is asked to enter the Algeria 44


temperature value. Temperature values are stored
in a separate list named the tempList. Afghanistan 38
▪ The elements are entered into the tempList using
the append() method within a for loop. Andorra 36
15

Example
country tempList country=["Albania", "Algeria", "Afghanistan", "Andorra"]
tempList=[]
numOfValues=len(country)
Albania 38 for element in range(numOfValues):
print("Enter the temperature for", country[element],":“, end=" ")
Algeria 44 temp=int(input())
tempList.append(temp)
Afghanistan 38

Andorra 36
16

Output country=["Albania", "Algeria", "Afghanistan", "Andorra"]


tempList=[]
numOfValues=len(country)
country tempList
for element in range(numOfValues):  
print("Enter the temperature for", country[element],":", end=" ")  
temp=int(input())  
Albania 38 tempList.append(temp)

Algeria 44

Afghanistan 38

Andorra 36
17

Printing elements
▪ This program prints the country name along with its
temperature value.

for element in range(numOfValues):


print(country[element], "\t" , tempList[element])
Python program with list functions
Note: Try max() and min() list functions
Output:
Task 1:
• create a list of names by assignment method
• print each element from a list in different lines. 
Task 2:
• create an empty list 
•  add integer elements to the list using for loop and append function 
• find the sum of the elements and print it with a proper message
Practical class Activity:
Write a program to accept a list of 3 enrolment numbers in the form of ENRNO-12345
and check each of the enrolment number in the list is valid or not.
The enrolment number is valid only when it satisfies the following conditions:
i. Must have 11 characters including the hyphen
ii. First field must be 'ENRNO' and only with letters(use is isalpha() string function)
iii. Second field must be 5 digit numbers and only with numbers 0-9(use isdigit()
string function)
Check and display each enrolment number from the list with a message 'Valid' if the
number is valid otherwise a message as "Invalid" if it is invalid.

Program:

You might also like