Python Practice Array
Python Practice Array
in aaray we need to specify the type . We need to use the type code
in array e need to use suare bracket
e.g - from array import *
vals = array('i',[5,6,7,8])
print(vals)
output-
array('i', [5, 6, 7, 8])
--------
from array import *
vals = array('i',[5,6,7,8])
for i in range(len(vals)):
print(vals[i])
output-
5
6
7
8
vals = array('i',[5,6,7,8])
for e in NewArray:
print(e)
output-
5
6
7
8
---------
print vaalue of array in ascending ordeer
vals = array('i',[5,6,7,8,1])
print(sorted(vals))
output-
15678
print factorial of given aarray
vals = array('i',[5,6,7,8,1])
new= array(vals.typecode, (factorial(a) for a in vals))
for e in new:
print(e)
output-
120
720
5040
40320
1
ouput
Number 5
120
-------------------
vals = array('i',[])
n = int(input("Enter the length of the array"))
for i in range(n):
x = int(input("Enter the Next value"))
vals.append(x)
print(vals)
k=0
for e in vals:
if e==w:
print(k)
break
K=k+1
print(vals.index(w))
output-
Enter the length of the array4
Enter the Next value45
Enter the Next value998
Enter the Next value56
Enter the Next value45
array('i', [45, 998, 56, 45])
Enter the value for search56
0
2
-------------------
Numpy package
arr = array([1,8,9,5])
print(arr)
ways of creating arrays using Numpy
When using numpy you dont need to specify the data type... also you can use dirent
data types in the sme array
e.g
arr = array([1,8,9,5.1])
print(arr)
output
[1. 8. 9. 5.1]
here there is only one decimal number... so all the numbers are displayed with a
point in the output
array()
linspace()
logsspace()
arange()
zeros()
ones()
print(arr)
output
[1. 8. 9. 5.1]
2. linspace()
e.g
print(arri)
If we dont specify the number of parts it will make same numer of parts..in
given example it will make 15 parts parts
e.g.
arri = linspace(0,15)
print(arri)
output
3. arange
e.g.
arry = arange(1,15,2).....dif of two in numbers
print(arry)
output
[ 1 3 5 7 9 11 13]