Python 3 Flashcards
When you do 7 / 2 what will you get?
3.5 – division is always float division.
When you do 6 / 2 what will you get?
3.0 – division is always float division.
What if you wanted 7 divided by 2 to give you 3?
You would do 7 // 2 - the // operator gives the floor.
What will print(“A”, “B”) give?
“A B”, with a space between the params
What if you wanted to do print(“A”, “B”) without a space in the output?
Use the optional “end” param: print(“A”, “B”, end=””)
How do you get command line input?
Use the input(prompt) command.
x = input(“name: “)
What is argv?
argv is the command line parameters. 0 is the script name.
What are the three file modes and the modifier?
r: Position at beginning
w: Erase whole file and position at 0
a: Position at end
+: Lets you do both reading and writing
How do you open a file?
f = open(“file.txt”, ‘r’)
Once you have a file open, how do you read it? Both full-file and single-line.
f.read() is the full file
f.readline() is a single line.
How would you do a variable-length argument list?
Use *arg.
def foo(num, *args):
what are *args and **kwargs?
*args makes a list called args of the remaining arguments.
**kwargs makes a dict of the named arguments.
What’s the syntax for defining a class, with its constructor?
class ClassName(ParentClass):
def __init__(self, param1, param2):
If you’re not using inheritance, what class should be the ParentClass?
object (lower-case)
What’s the first thing your constructor should always do?
Call the parent class constructor.
super(ClassName, self).__init__(param1, param2)
What is the thing I always forget about classes?
Need to use self.variable, and include (self, arg) in method calls.
How do you do multiple inheritance?
class ClassName(ParentClass1, ParentClass2)
Why don’t you want to use multiple inheritance?
Because you can instead use composition – create an instance of Class1 and Class2 within your class and use them as objects.
What are typehints and how do they work?
It’s when you declare your variable types and return types in your method signature.
def foo(bar: str, baz: int) -> float:
How do you specify a generic type in a typehint (for list, tuple, dict, etc.)
You have to import the collection type from typing, and then put the generic type in [].
from typing import List, Tuple
def foo(bar: List[Tuple[int]])
How do you specify the return type as a typehint?
Use ->
def foo(bar: int) -> str:
What is an inner function?
It’s where you define a function as an objct inside another function, usually used for recursion.
def factorial(num: int) -> int:
def inner_factorial(num: int) -> int:
if num == 1:
return 1
return num * inner_factorial(num - 1)
return inner_factorial(num)
What is the syntax for a lambda function?
lambda x : x < 5
What are the three main places you use a lambda function?
filter, map, functools.reduce
What’s the place I always want to use a lambda function, but shouldn’t?
In a list comprehension. If you do [lambda x : x + 5 for x in values] you will get a list of lambda functions.
List comprehension already has an implicit lambda function – you would just do [x + 5 for x in values]
What’s the syntax for filter?
filter(lambda x : x < 5, [4, 5, 6])
What’s the syntax for map?
map(lambda x : x + 5, [4, 5, 6])
What’s the syntax for reduce?
import functools
functools.reduce(lambda x, y: x + y, [4, 5, 6])
What’s the main thing I need to remember about the lambda function methods?
That you can combine them, e.g.
list(map(lambda x : x + 5,
filter(lambda x : x > 10, [9, 10, 11])
))
returns [16]
How does functools.reduce work?
It’s a rolling aggregate. At each step you take the new variable and do the operation to combine it with the rolling aggregate. if it’s lambda x, y then x is the rolling aggregate and y is the new value.
What’s the syntax for a named tuple?
from collections import namedtuple
Point = namedtuple(“Point”, [“x”, “y”])
p = Point(2, 4)
p.x, p.y
Is a named tuple mutable?
No, but if it has a mutable type as a property, that is mutable.