Describe similarities and differences between copying


Consider the following Python code segment, which uses built-in Python lists and NumPy lists to perform similar operations, albeit with differing results.

Using built-in Python lists:


>>> data = [1, 2, 3, 4]
>>> print data
[1, 2, 3, 4]
>>> otherData = data
>>> otherData[1] = -2
>>> print otherData
[1, -2, 3, 4]
>>> print data
[1, -2, 3, 4]
>>> 
>>> otherData = data[1:3]
>>> print otherData
[-2, 3]
>>> otherData[0] = 0
>>> print otherData
[0, 3]
>>> print data
[1, -2, 3, 4]
Using NumPy arrays:

>>> import numpy as np
>>> data = np.array([1, 2, 3, 4])
>>> print data
[1 2 3 4]
>>> otherData = data 
>>> otherData[1] = -2
>>> print otherData
[1 -2 3 4]
>>> print data
[1 -2 3 4]
>>> 
>>> otherData = data[1:3]
>>> print otherData
[-2 3]
>>> otherData[0] = 0
>>> print otherData
[0 3]
>>> print data
[1 0 3 4]
Describe similarities and differences between copying and assignment semantics of built-in Python lists and NumPy arrays. Explain why the code behaves differently for the two. 

Request for Solution File

Ask an Expert for Answer!!
Python Programming: Describe similarities and differences between copying
Reference No:- TGS082531

Expected delivery within 24 Hours