IniziaInizia gratis

Operating on lists and arrays

Operators, like +, work differently when applied to NumPy arrays and lists. When two NumPy arrays are added together with +, Python adds the two arrays together element-by-element and returns a new array of the same shape as the original arrays. This is similar to how MATLAB handles addition with matrices.

On the other hand, adding two lists together with + in Python concatenates them. While lists and arrays may appear to be interchangeable, if you aren't careful, these differences can lead to some unpleasant surprises.

In this exercise, you'll explore how the + operator works for NumPy arrays and lists.

Questo esercizio fa parte del corso

Python for MATLAB Users

Visualizza il corso

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# Create two lists, x and y, containing the 3 integers in each
x = ____
y = ____

# Add them together & save the result to z
Modifica ed esegui il codice