Skip to content
Snippets Groups Projects
Commit 5a19bbf7 authored by LOUIS TYRRELL OLIPHANT's avatar LOUIS TYRRELL OLIPHANT
Browse files

louis lec 21 copying

parent d0ad2ed8
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
%% Cell type:markdown id:193622e7 tags:
**Q2:** [PythonTutor Link](https://pythontutor.com/visualize.html#code=x%20%3D%20%5B1,%202,%203%5D%0Ay%20%3D%20%5B1,%202,%203%5D%0Az%20%3D%20x%0Az.append%284%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id:131b9746 tags:
``` python
x = [1, 2, 3]
y = [1, 2, 3]
z = x
z.append(4)
```
%% Cell type:markdown id:f578b492 tags:
**Q3:** [PythonTutor Link](https://pythontutor.com/visualize.html#code=nums1%20%3D%20%5B1,2%5D%0Anums2%20%3D%20nums1%0Ax%20%3D%20nums2.pop%281%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id:6d6a052b tags:
``` python
nums1 = [1,2]
nums2 = nums1
x = nums2.pop(1)
```
%% Cell type:markdown id:0abc64c0 tags:
**Q4:** [PythonTutor Link](https://pythontutor.com/visualize.html#code=x%20%3D%20%5B1,%202%5D%0Ay%20%3D%20%5B3%5D%0Az%20%3D%20x%20%2B%20y%0Ay.append%284%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id:373369b9 tags:
``` python
x = [1, 2]
y = [3]
z = x + y
y.append(4)
```
%% Cell type:markdown id:a3eb791e tags:
**Q5:** [PythonTutor Link](https://pythontutor.com/visualize.html#code=people%20%3D%20%7B%22alice%22%3A30,%20%22bob%22%3A25%7D%0Ax%20%3D%20people%0Ay%20%3D%20people%5B%22bob%22%5D%0Ax%5B%22alice%22%5D%20%3D%2031%0Ay%20%3D%2026&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id:ab703ab8 tags:
``` python
people = {"alice":30, "bob":25}
x = people
y = people["bob"]
x["alice"] = 31
y = 26
```
%% Cell type:markdown id:6dd6f676 tags:
**Q6:** [PythonTutor Link](https://pythontutor.com/visualize.html#code=def%20f%28items%29%3A%0A%20%20%20%20return%20items.pop%280%29%0Anums%20%3D%20%5B1,2,3%5D%0Anums.append%28f%28nums%29%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id:81a7d6d6 tags:
``` python
def f(items):
return items.pop(0)
nums = [1,2,3]
nums.append(f(nums))
```
%% Cell type:markdown id:5cea38e0 tags:
**Q7 (As Written):** [PythonTutor Link](https://pythontutor.com/visualize.html#code=import%20copy%0Ax%20%3D%20%5B2,1%5D%0Ay%20%3D%20copy.copy%28y%29%0Ay.sort%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id:ed06d8b5 tags:
``` python
import copy
x = [2,1]
y = copy.copy(y)
y.sort()
```
%% Cell type:markdown id:1499a535 tags:
**Q7 (As Fixed):** [PythonTutor Link](https://pythontutor.com/visualize.html#code=import%20copy%0Ax%20%3D%20%5B2,1%5D%0Ay%20%3D%20copy.copy%28x%29%0Ay.sort%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id:25a014f1 tags:
``` python
import copy
x = [2,1]
y = copy.copy(x)
y.sort()
```
%% Cell type:markdown id:88899380 tags:
**Q8:** [PythonTutor Link](https://pythontutor.com/visualize.html#code=import%20copy%0Adef%20biggest%28items%29%3A%0A%20%20%20%20items%20%3D%20copy.copy%28items%29%0A%20%20%20%20items.sort%28%29%0A%20%20%20%20return%20items%5B-1%5D%0Anums%20%3D%20%5B3,9,6%5D%0Ax%20%3D%20biggest%28nums%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id:a03f0f27 tags:
``` python
import copy
def biggest(items):
items = copy.copy(items)
items.sort()
return items[-1]
nums = [3,9,6]
x = biggest(nums)
```
%% Cell type:markdown id:60f15a79 tags:
**Q9:** [PythonTutor Link](https://pythontutor.com/visualize.html#code=import%20copy%0Ateam1%20%3D%20%5B%0A%20%20%7B%22name%22%3A%22A%22,%20%22age%22%3A7%7D%0A%5D%0Ateam2%20%3D%20copy.copy%28team1%29%0Ateam2.append%28%0A%20%20%7B%22name%22%3A%22B%22,%20%22age%22%3A9%7D%0A%29%0Ateam2%5B0%5D%5B%22age%22%5D%20%3D%208%0Ax%20%3D%20team1%5B0%5D%5B%22age%22%5D&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id:ba913d5b tags:
``` python
import copy
team1 = [
{"name":"A", "age":7}
]
team2 = copy.copy(team1)
team2.append(
{"name":"B", "age":9}
)
team2[0]["age"] = 8
x = team1[0]["age"]
```
%% Cell type:markdown id:9e3d7dd3 tags:
**Q10:** [PythonTutor Link](https://pythontutor.com/visualize.html#code=import%20copy%0Ateam1%20%3D%20%5B%0A%20%20%7B%22name%22%3A%22A%22,%20%22age%22%3A7%7D%0A%5D%0Ateam2%20%3D%20copy.deepcopy%28team1%29%0Ateam2.append%28%0A%20%20%7B%22name%22%3A%22B%22,%20%22age%22%3A9%7D%0A%29%0Ateam2%5B0%5D%5B%22age%22%5D%20%3D%208%0Ax%20%3D%20team1%5B0%5D%5B%22age%22%5D&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id:1c49cbcb tags:
``` python
import copy
team1 = [
{"name":"A", "age":7}
]
team2 = copy.deepcopy(team1)
team2.append(
{"name":"B", "age":9}
)
team2[0]["age"] = 8
x = team1[0]["age"]
```
%% Cell type:markdown id:30953821 tags:
**Q11:** [PythonTutor Link](https://pythontutor.com/visualize.html#code=import%20copy%0Aorig%20%3D%20%5B1,%5B2,%5B3,4%5D%5D%5D%0Ax%20%3D%20orig%0Ay%20%3D%20copy.copy%28orig%29%0Az%20%3D%20copy.deepcopy%28orig%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id:e67ede97 tags:
``` python
import copy
orig = [1,[2,[3,4]]]
x = orig
y = copy.copy(orig)
z = copy.deepcopy(orig)
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment