Section 09: Object Identity, Lists and Shallows, and Deep copies Flashcards

1
Q

String Methods:

Strip method:

s.strip.(chars)

A

Strip Method

s.strip(chars): returns copy string with leading and trailing characters in chars removed

  • if char not provided → whitespace default

For example, if we have a string s = " hello world ", s.strip() would return "hello world". If we pass additional arguments to the strip method, it will remove those characters instead of whitespace. For example, s.strip('dlo ') would return "hello wor".

s not modified ⇒ string immutable

```jsx
»> s = ‘ apple ‘
»> s.strip()
‘apple’
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

String Methods

Split Method

s.split(sep)

A

Split Method
s.split(sep): returns list of substrings in string using sep as delimiter

  • If sep not provided → whitespace default

```jsx
»> s = ‘bananas and appels’
»> s.split()
[‘bananas’, ‘and’, ‘apples’]
»> s
‘bananas and apples’
»> s.split(‘a’)
[‘b’, ‘n’, ‘n’, ‘s’, ‘nd’, ‘pples’]
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

String Methods

Join Method

s.join(x)

A

> s.join(x) takes input and iterable object x (eg. list) containing strings.
Returns new string with string s inserted between each string in x

```jsx
»> ‘.’.join([‘cat’, ‘dog’])
‘cot.dog’
»> ‘ ‘.join([‘cat’, ‘and’, ‘dog’])
‘cat and dog’
»> ‘ ‘.join(‘apple’)
‘a p p l e’
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the two ways to add items to a list?

A

```jsx
»> x = []
»> x = x + [‘first’]
OR
»> x = []
»> x.append(‘first’)

~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the ‘identification’ an object gets at its creation?

A
  • Each object assigned identifier at its creation (memory address)
  • Identity unique and constant for object during lifetime

Built in function id() function returns identity of object

```jsx
»> weather_str = ‘It's raining’
»> print(id(weather_str))
446656232
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can running your program in a module and shell change the output for booleans?

A

Python uses caching for all immutable objects of built-in types. Therefore, (sometimes) if you use instructions in a shell you’ll see False dispalyed, but if you run it in the module, all print statements will display True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Reference and Value

For copying references?

A

<aside>
💡 Python uses caching for all immutable objects of built-in types. Therefore, (sometimes) if you use instructions in a *shell* you’ll see `False` dispalyed, but if you run it in the module, all print statements will display `True`

</aside>

For: a = 14 and a = b, copying reference (assigning memory address) of object a in b

  • Verify uding the id() function which returns reference value

```python
»> a = 14
»> b = a
»> id(a)
44480656
»> id(b)
44480656
~~~

Two different IDs point to two different objects

```python
»> x = 500
»> y = 400
»> id(x) == id(y)
False

> > > x = 500
y = 500
id(x) == id(y)
False #Though they have the same value, different IDs
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can two different IDs point to two different object even if their value assignment is the same?

A

Two different IDs point to two different objects

```python
»> x = 500
»> y = 400
»> id(x) == id(y)
False

> > > x = 500
y = 500
id(x) == id(y)
False #Though they have the same value, different IDs
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the identity operators?

A

is and is not are boolean operators used to determine if the given operands have the same identity (refers to the same object)

Not the same as checking for equality (==) → refers to havinng the same data, but it does not mean that it is the same object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between a deep and shallow copy?

A

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Shallow vs Deep

What is a shallow copy?

A
  • When calling shadow copy, modifying one list will modify the other and visa versa
  • Reference the same list
  • Same reference ID

```python
»> list1 = [‘1’, ‘2’]
»> list2 = list1
»> list2[1] = ‘B’
»> print(list2)
[‘1’,’2’,’B’]
»> print(list1)
[‘1’,’2’,’B’]
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are list arguments?

A

Read section 10.12 (p.97)

When pass a list to a function, function get reference to a list.

  • If function modifies the list, caller sees change

```python
def delete_head(t):
del t[0]

> > > letters = [‘a’, ‘b’, ‘c’]
delete_head(letters)
letters [‘b’, ‘c’]
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Mutable vs. Immutable

A

Can be written as void

> Immutable: content object cannot be changed after object’s creation

Strings: immutable

  • Cant use the square brackets to modify character in the string

```python
»> s = ‘cats’
»> s[0] = ‘r’
**TypeError**
~~~

> Mutable: content of object can be changed

Lists: mutable

  • Can use square brackets to change

Example: function takes list of integers as input and increases the value of each element in list by its index:

```python
def increase_by_index(input_list):
for i in range(len(input_list)):
input_list[i] = input_list[i] + 1

return input_list

~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What modifies a list?

A

Which of the operators/methods we have seen modify the content of list object?

  1. Assigning value to an element using its index
  2. Using slice assignment to modify the elements of a list
  3. Using methods list append(), insert(), remove(), pop(), or sort()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does not modify a list?

A
  • Slicing!(It is useful tool to create new lists out of existing lists)
  • The + and * operators
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Shallow copy for list:

A

```python
def example(x):
y = []
for e in x:
new_e = []
for n in e:
new_e.append(n)
y.append(new_e)
y[0][0] = 5
~~~

> Shallow Copy: creating a new list and adding to it the references stored in the original list one by one

```python
shallow[0] = 5 #Not affect my_list
shallow[0][0] = 5 # affects my_list also
shallow[0].append(5) # affects my_list also
~~~

17
Q

Deep Copy for lists:

A

> Deep Copy: Creating a new list and adding it to a (deep) copy of the obejcts in the original list one by one

Nothing we can do through deep can affect my_list