Python
Functions, Methods & Attributes
16 flashcards · answers and spaced-repetition review in the KnowCard app
What does this print?
```
def diff(a, b):
return a - b
print(diff(2, 10))
```
What is the difference between a function and a method in Python?
You want to sort a list. Why does sort(mylist) fail, and what is correct?
What does this print?
```
def greet():
return "hi"
print(greet)
```
Calling diff(a, b), how can you make the call order-independent and self-documenting?
What does number.real.is_integer() do, reading it left to right?
Why can you write [1, number.imag, number.real] and get [1, 6.0, 5.0]?
A method's signature is shown as method(p1, [p2, p3]). What do the square brackets mean?
True or false: max([3,6,2,1,9]) modifies the list in place like mylist.sort() does.
You can call max([3,6,2]) directly but must call [3,6,2].count(2). What's the rule?
What is the practical difference between writing f and f()?
An object has a method check. What does obj.check evaluate to, versus obj.check()?
What is an attribute of an object in Python?
What does this print?
```
number = 5 + 6j
print(number.real())
```
When you call mylist.sort(), how does the method know which list to sort?
Distinguish a parameter from an argument in a function call.
Start learning today
Free to start — download the app or use it in your browser.
