On this page

Open In Colab

Numpy

import numpy as np

An array object represents a multidimensional, homogeneous array of fixed-size items

country = np.array(['USA', 'Japan', 'UK', 'Celestopoli', 'India', 'China'])
print(country)
['USA' 'Japan' 'UK' 'Celestopoli' 'India' 'China']

When you make numpy array using np.array than it would create a copy of the object array and would not reflect changes to the original array (with default parameters). On the other hand if you use np.asarray() than it would reflect changes to the original array.

x = np.array([2,3,1,0])
x
array([2, 3, 1, 0])
x.shape
(4,)
y = np.asarray([4,5,3,1])
y
array([4, 5, 3, 1])
type(x)
numpy.ndarray
y = np.array([[2,3,1,0],[1,1,1,1]])
y.shape
(2, 4)
np.zeros((2, 3))
array([[0., 0., 0.],
       [0., 0., 0.]])
k =np.ones((2, 3))
k
array([[1., 1., 1.],
       [1., 1., 1.]])
k.shape
(2, 3)
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(2, 10, dtype=float) # default strp=1
array([2., 3., 4., 5., 6., 7., 8., 9.])
np.linspace(1., 4.,num=10)
array([1.        , 1.33333333, 1.66666667, 2.        , 2.33333333,
       2.66666667, 3.        , 3.33333333, 3.66666667, 4.        ])
np.linspace(1., 4.,10)
array([1.        , 1.33333333, 1.66666667, 2.        , 2.33333333,
       2.66666667, 3.        , 3.33333333, 3.66666667, 4.        ])
x = np.array([3, 6, 9, 12])
x/3.0
x
array([ 3,  6,  9, 12])
print(x)
[ 3  6  9 12]
z = np.array([2,3,1,0])
np.sort(z)
array([0, 1, 2, 3])
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[9, 8, 7], [6, 5, 4]])
c = np.concatenate((a, b))
c
array([[1, 2, 3],
       [4, 5, 6],
       [9, 8, 7],
       [6, 5, 4]])
c.shape
(4, 3)
np.ones((2, 2), dtype=bool)
array([[ True,  True],
       [ True,  True]])
## Conversion to an array:

import numpy as np

# from list
my_list = [1, 2, 3, 4, 5, 6, 7, 8]

my_list = np.asarray(my_list)
#type(my_list)
my_list
array([1, 2, 3, 4, 5, 6, 7, 8])
# from tuple
my_tuple = ([8, 4, 6], [1, 2, 3])
type(my_tuple)
tuple
my_tuple = np.asarray(my_tuple)
my_tuple
array([[8, 4, 6],
       [1, 2, 3]])
#Array to list
a = np.array([1, 2, 3, 4, 5])

b = a.tolist()
b
[1, 2, 3, 4, 5]
#save array to drive
a = np.array([1, 2, 3, 4, 5])

np.savetxt("il_mio_array.csv", a)
# il filse csv deve essere sul drive
from google.colab import files
files.download("il_mio_array.csv")
#######################################################
a = np.array([1,2,3])
a
array([1, 2, 3])
b =np.append(a,[10,11,12,13])
b
array([ 1,  2,  3, 10, 11, 12, 13])
a = np.array([1,2,3])
b = np.array([1,2,3])
c = a+b
c
array([2, 4, 6])
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[400], [800]])

c = np.append(a, b)
c
array([  1,   2,   3,   4,   5,   6, 400, 800])
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[400], [800]])

c = np.append(a, b, axis = 1)
c
array([[  1,   2,   3, 400],
       [  4,   5,   6, 800]])
d = np.array([1, 2, 3])
e = np.insert(a, 1, 90) ## specify whick index add the value we want
e
array([ 1, 90,  2,  3,  4,  5,  6])
d = np.array([1, 2, 3])
e = np.insert(d, 0, 60) ## specify whick index add the value we want
e
array([60,  1,  2,  3])
len(d)
3
d = np.array([1, 2, 3])

e = np.insert(d, 3, 90) ## specify whick index add the value we want
e
array([ 1,  2,  3, 90])
#APPEND A ROW
a = np.array([[1, 2, 3], [4, 5, 6]])

b = np.append(a, [[50, 60, 70]], axis = 0)
b
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [50, 60, 70]])
## se volessi per colonna?
a = np.array([[1, 2, 3], [4, 5, 6]])

c = np.array([[50],[60]])
b = np.append(a, c, axis = 1)
b
array([[ 1,  2,  3, 50],
       [ 4,  5,  6, 60]])
### Delete an element
x = np.array([1, 2, 3])

x= np.delete(x, 1, axis = 0)
x
array([1, 3])
#delete an entire row (axis = 0)
x = np.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])

x = np.delete(x, 2, axis = 0)
x
array([[1, 2, 3],
       [4, 5, 6]])
x.shape
(2, 3)
#delete an entire column (axis = 1)
x = np.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])

x = np.delete(x, 2, axis = 1)
x
array([[ 1,  2],
       [ 4,  5],
       [10, 20]])
#find the index value

a = np.array([1, 2, 3, 4, 5])

print("il valore 5 si trova all'index: ", np.where(a == 5))
il valore 5 si trova all'index:  (array([4]),)
######### automatic addition

addizione = lambda x: x + 2

a = np.array([1, 2, 3, 4, 5, 6])

b = addizione(a)
b
array([3, 4, 5, 6, 7, 8])
moltiplicazione = lambda x: x**2

a = np.array([1, 2, 3, 4, 5, 6])

b = moltiplicazione(a)
b
array([ 1,  4,  9, 16, 25, 36])
età=18

if età <18:
    print('sei minorenne')
else:
    print('sei maggiorenne')
sei maggiorenne
età=18

if età <10:
    print('sei un bimbo')
elif età<=18:
    print('sei un regaz')
elif età<50:
    print('sei un boomer')
else:
    print('bella vecchio! sei anziano')
sei un regaz
età=50
patente=False

if età >=18 and patente==True:
    print('puoi guidare finalmente la tua auto da sogno')
elif età >=18 and patente==False:
    print('no patente, no party')
else:
    print('mi dispiace, ritenta quando sarai più grande!!')
no patente, no party

Funzioni

def somma_due_numeri(a,b): ## a,b sono argomenti
    #print('questa funzione somma due numeri:')
    risultato = a + b
    return risultato
# OUTPUT DI UNA FUNZIONE è IL VALORE DEL SUO RETURN
# TUTTE LE FUNZIONI HANNO UN RETURN
## input
x =input()
#x = input('inserire qualcosa   ')
type(x)
### PROGRAMMA
print('questo è il mio programma in python')

nome=input('inserire il tuo nome  ') 
print('buon pomeriggio  '+nome+'  spero tu stia imparando tantissimo!')
età = input('quanti anni hai?')
print('Bene!!! dunque hai '+ età + '  anni. Il porssimo anno ne avrai '+ str(int(età)+1))