Sie sind auf Seite 1von 4

Arrays in Python

In array all the values should be of the same type

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

How to asign old array value to a new array

from array import *

vals = array('i',[5,6,7,8])

NewArray= array(vals.typecode, (a for a in vals))

for e in NewArray:
print(e)

output-
5
6
7
8

---------
print vaalue of array in ascending ordeer

from array import *

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

print factorial by providing the input


a= int(input("Number"))
print(math.factorial(a))

ouput
Number 5
120

-------------------

How to give input of array

from array import *


from math import *
import math

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)

w = int(input("Enter the value for search"))

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

used to print multi dimensionl array

from numpy import *

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

from numpy import *

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

we hav six ascending

there are six ways

array()
linspace()
logsspace()
arange()
zeros()
ones()

1st way - array()


arr = array([1,8,9,5.1])

print(arr)

output
[1. 8. 9. 5.1]

2. linspace()

e.g

arri = linspace(0,15,16) >>>>>>. here o is the starting number and 15 is the


end number and 16 is the number of parrts

print(arri)

[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.]

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

[ 0. 0.30612245 0.6122449 0.91836735 1.2244898 1.53061224


1.83673469 2.14285714 2.44897959 2.75510204 3.06122449 3.36734694
3.67346939 3.97959184 4.28571429 4.59183673 4.89795918 5.20408163
5.51020408 5.81632653 6.12244898 6.42857143 6.73469388 7.04081633
7.34693878 7.65306122 7.95918367 8.26530612 8.57142857 8.87755102
9.18367347 9.48979592 9.79591837 10.10204082 10.40816327 10.71428571
11.02040816 11.32653061 11.63265306 11.93877551 12.24489796 12.55102041
12.85714286 13.16326531 13.46938776 13.7755102 14.08163265 14.3877551
14.69387755 15. ]

3. arange

e.g.
arry = arange(1,15,2).....dif of two in numbers
print(arry)

output
[ 1 3 5 7 9 11 13]

Das könnte Ihnen auch gefallen