Chapter 6 - Functions and stuff Flashcards

1
Q

What is the general form of a function?

A

def functionname(parameters):
“"”Docstring”””
#function body

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

keyword def

A

starts the header for a function, and header must end in : like all good things in python

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

what is a docstring?

A

a string in triple quotes under a function header, when called with keyword def

def sqrt(sidelen):
“”"”DOCSTRING””””
will return docstring

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

can you return multiple things from a function?

A

kind of, returns a single tuple but you can use that tuple to set equal to like a, b, c

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

formal vs actual parameters

A

formal = in header, actual is in function

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

what do functions with no return statement return?

A

special value None

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

what is a lambda function?

A

short one line functions w/o the need for the def keyword, created via lambda keyword

lambda argument(s): expression

ex.
times3 = lambda num: num*3
times3(4)

would return 12

confined to only one simple expression

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

can a function print and return?

A

yes, but bad form so avoid it

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

Write a function that will shuffle the elements in a tuple and return the result.

A

from random import shuffle
def tup_shuffle(tup):
“"”shuffles elements of a tuple and returns result”””
tuplist = list(tup)
shuffle(tuplist)
return tuple(tuplist)

tup_shuffle((1, 4, 3))

rmr, shuffle function shuffles IN PLACE! and ONLY ON MUTABLES!

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

write a one-line lambda function to add “, eh?” to a string

A

pluscanada = lambda userin: userin + “, eh?”

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

what will this print:
list1 = [1, 2, 3, 4, 5]
list2 = list1
list1[0] = 45
print(list1, list2)

A

[45, 2, 3, 4, 5] [45, 2, 3, 4, 5]

when MUTABLE type, location remains unchanged, and so list2 still points at the same place list1 points at post-modification.

no new list is created, thus they stay linked to the same mem. location

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

whats up with slicing and memory location?

A

for MUTABLE TYPES, slicing, even if it slices through everything and returns what looks like the exact same result, refers to a different memory location.

IMMUTABLE types like strings and tuples dont give af, will remain the same

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

when does memory location remain the same under a change in place like +, -, *, append, etc.?

A

when it is a mutable type! immutable types are immutable, and thus the variables are assigned a completely new value, rather than being mutated.

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

how would you swap two variables without a temporary variable?

A

a, b = b, a

simultaneous assignment is simultaneous, happens all in one instant and thus the switch will complete successfully

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

what is the ONLY way to modify an argument passed in/referred to in a function within the function?

A

by altering the argument through append() or smth, cannot simply reassign new (thus, immutable things like integers cannot be modified at all)

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

how would you prevent a mutable argument from being altered in a function?

A

either assign to a new value w/i function which you return or use x[:] where x is the parameter, thus the og object is not altered.

17
Q

true or false:
passing an argument to a function parameter results in the function parameter referring to the same location as the argument

A

true, its basically assigning one variable to another

18
Q

= function always creates new memory

A
19
Q

deep copy vs shallow copy

A

deep copy - everything copied to new object

shallow copy - only reference is copied to new object, less a COPY and more like a website url

deep copy example:
a = 5
b = a
a = 3
print(a,b)

shallow: - independent of each other??? ig???

Set the myList
myList = [‘1’, ‘2’, ‘3’, ‘4’]

Create a new variable, myNewList, and assign myList to it.
myNewList = myList
myNewList[0] = ‘0’

Print the old list
print(“myList: “, myList)

Check that these two variables, myList and myNewList are indeed exactly the same!
print(myNewList is myList)

20
Q

result?
d = 500
d /= 100
print(d)

A

5.0 (float)

21
Q

do += or -= or *= or /= create new memory locations?

A

yes

22
Q

returns?
“hello”*=4

A

hellohellohellohello

23
Q

make an identity matrix (1 along diagonal left to right) in one line

A

def makeidentity(n):
“"”returns n x n identity matrix”””
return [[1 if j == i else 0 for i in range(n)]
for j in range(n)]