Lists As Arrays
Lists As Arrays
A list in Python is just an ordered collection of items which can be of any type.
Lists as arrays
The arrays is a data structure in which the elements are of same data type.
A list in Python is just an ordered collection of items which can be of any type.
By comparison an array is an ordered collection of items of a single type.
The elements in the array are separated by comma and are enclosed within the
square bracket. For example
arr = [10,20,30,40,50]
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50
1. Creation of Arrays
We can create an array using the array name and list of elements. For example
arr = [10,20,30,40]
will create an array containing the elements 10,20,...,40. These elements can be
represented using for loop. Following program represents the array creation and
display of elements.
ArrayDemo.py
arr = [10,20,30,40]
for i in range(len(arr)):
print(arr[i])
Output
a = [i for i in range(10)]
2. Operations on Arrays
1. Appending a value
Using append() function we can add the element in the array at the end. For
example
ArrayDemo1.py
arr = [10,20,30,40]
for i in range(len(arr)):
print(arr[i])
arr.append(50)
for i in range(len(arr)):
print(arr[i])
Output
10
20
30
40
10
20
30
40
50
>>>
We can insert the value at any desired location using insert() function. The
syntax is insert(index,value)
For example
ArrayDemo2.py
arr = [10,20,30,40]
for i in range(len(arr)):
print(arr[i])
arr.insert(2,25)
for i in range(len(arr)):
print(arr[i])
Output
10
20
30
40
10
20
30
40
>>>
We can extend one array by joining another array to it. For that purpose the
extend() function is used. The syntax is
extend(new_array)
For example
ArrayDemo3.py
arr = [10,20,30,40]
for i in range(len(arr)):
print(arr[i])
new_arr = [50,60,70]
arr.extend(new_arr)
for i in range(len(arr)):
print(arr[i])
Output
10
20
30
40
50
60
70
>>>
Any desired element can be deleted from the array using remove() method. The
syntax is
remove(index_of_element)
For example
ArrayDemo4.py
arr = [10,20,30,40]
for i in range(len(arr)):
print(arr[i])
arr.remove(30)
for i in range(len(arr)):
print(arr[i])
Output
10
20
30
40
20
40
>>>
For removing the last element from the array then pop() function is used.
Syntax
pop()
For example
ArrayDemo5.py
arr = [10,20,30,40]
for i in range(len(arr)):
print(arr[i])
arr.pop()
for i in range(len(arr)):
print(arr[i])
Output
10
20
30
40
10
20
30
>>>
For example
ArrayDemo6.py
arr = [10,20,30,40]
for i in range(len(arr)):
print(arr[i])
arr.reverse()
for i in range(len(arr)):
print(arr[i])
Output
10
20
30
40
40
30
20
10
>>>
We can count the number of times the particular element appears in the array
using the count method.
For example
ArrayDemo7.py
arr = [10,20,30,40,50,20,30,20]
for i in range(len(arr)):
print(arr[i])
Output
10
20
30
40
50
20
30
20
Home | All Subjects | EEE Department | Problem Solving and Python Programming
<< Previous
Next >>
Following is a Python program that is used for obtaining the square root of a given
number
Illustrative Programs
AU : Dec.-19, Marks 8
1. quare Root
Following is a Python program that is used for obtaining the square root of a
given number
Python Program
num = float(input())
Output
2. GCD
The GCD is a largest integer that can exactly divide both numbers without a
remainder.
The easiest and fastest process consists in decomposing each one of the
numbers in products of prime factors, this is, and we successively divide each
one of the numbers by prime numbers till we reach a quotient that equals 1.
For example -
96 = 2 × 2 × 2 × 2 × 2 × 3
36 = 2 × 2 3 × 3
GCD = 2 × 2 × 3
= 12
a = int(input())
b = int(input())
rem = a%b
while rem! = 0:
a=b
b = rem
rem = a%b
Output
def gcd(a,b):
c=b
b = a%b
if b = = 0:
return c
else:
return gcd(cb)
print(gcd(96,36))
Output
12
>>>
Cano
3. Exponentiation
Python Program
def expo(base,degree):
result = 1
i=1
i+=1
print("Result is ",result)
expo(2,3)
Output
Result is 8
>>>
4. Sum of Numbers
For sum of numbers we have to store the numbers in an array. And by
traversing the elements of array, each number is added with each other. The
resultant sum is then printed.
For example
Python Program
n = int(input())
i=0
sum = 0
for i in range(0,n):
a[i] = int(input())
for i in range(0,n):
print(a[i])
for i in range(0,n):
Output
5. Linear Search
In linear search method, the key element is compared against every element of
the array. If the key element matches with the array element then we declare
element is found otherwise the element is declared as not found.
Python Program
n = int(input())
i=0
for i in range(0,n):
a[i] = int(input())
print("The elements in array are ...").
for i in range(0,n):
print(a[i])
key = int(input())
for i in range(0,n):
if a[i] = = key:
found = True
break
else:
found = False
if found = = True:
else:
Output
6. Binary Search
The binary search is an efficient searching technique.
Python Program
def binary_search(a,n,key):
low = 0
high = n
mid = int((low+high)/2)
if(key == a[mid]):
return mid
elif(key< a[mid]):
high = mid - 1
else:
low = mid + 1
return -1
for i in rnge(0,n):
a[i] = int(input())
k = int(input())
position = binary_search(a,n,k)
if(position ! = -1):
Output(Run1)
10
20
Enter the element:
30
40
50
40
>>>
Output(Run2)
10
20
30
40
50
>>>
The prerequisite for this searching technique is that the arry should be sorted.
Example :
Aa mentioned earlier the necessity of this method is that all the elements should
be sorted. So let us take an arry of sorted elements.
Step 2: Find the middle element of the array. Compare it with the key
i.e. if 42 < 99