Difference between List and Numpy Array

Numpy - Numerical Python

#pip install numpy
import numpy as np
x = [1,2,3,4.7,"python"]
x

OUTPUT:

[1, 2, 3, 4.7, 'python']‍

1. Less storage

2. More Speed

 

A List cannot handle mathematical operations.

Numpy array can handle mathematical operations.

x = [1,2,3,4]
y = [4,5,6,7]
x+y

OUTPUT:

[1, 2, 3, 4, 4, 5, 6, 7]
x = np.array([1,2,3,4])
y = np.array([5,6,7,8])
x+y

OUTPUT:

array([ 6,  8, 10, 12])

List is hetrogenous

Array is homogeneous

all the elements should always be the same

x= np.array([1,2,3,5.6,"python"])
x

OUTPUT:

array(['1', '2', '3', '5.6', 'python'], dtype='<U32')
Lesson Assignment
Challenge yourself with our lab assignment and put your skills to test.
# Python Program to find the area of triangle

a = 5
b = 6
c = 7

# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))

# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
Sign up to get access to our code lab and run this code.