Variables:
a) use_this
or
b) useThis
?
a
Do floats have limited precision ?
Yes
0.1 == 0.10000000000000001 —> True
What does the null object None represent?
The absence of a value
How can you create multi line strings?
Triple double quotes “““-”””
with \ in the str
What will the following return?
‘Hello!’[-1]
‘!’
Does this work?
my_tuple = (True, 2, 'three')
my_tuple[2] = 'third'
No, a Python tuple is an immutable, heterogeneous, ordered sequence of elements, but we can concat tuples with + operator
How do we concat dicts?
How do we update dicts?
With the | operator. Be careful because same keys will be overwritten with the latter one.
dict.update() (order matters again!)
Give the operators for union, intersection, difference and sym. difference for sets.
| union (alles)
& intersec (mitte)
- diff (nur links)
^ sym diff (alles ohne mitte)
what is the result?
list({'a': 1, 'b': 2})
tuple({'a': 1, 'b': 2})
['a', 'b']
('a', 'b')
^Cool^
letTheFuncRide()
shook_one_shook_two()
B
What is automatically invoked after a instantiating a class obj?
__innit__ method
How can you create subclasses?
And how can you override a superclass method ?
specify its superclass in the class statement
Override it in the subclass.
How do you invoke a method of a superclass?
with super()
Where does import statement load files from?
From the Python installation directory, from the system path, and from the working directory.
Whats a “regular” package?
A “regular” package contains an __init__.py file. This file is supposed to contain the initialization logic
What does the finally block do in a try…except block?
Its always executed as the last task before the try statement completes
What is a and what is b?
a = 1
b = [2, 3, 4, 5]
Cool Cool
wow
ALAAARM
criss cross
list
beep
set
buup
formatzZ
When are f-string evaluated?
At runtime
Choco
is a deque a linked or double linked list ?
double linked list.
It hast the attributes append(app-left) and pop(pop-left)
Which operations does a deque perform less efficiently than a regular list?
Indexing, slicing, concat, sort
In a for … in … loop does the __iter__ methode (iterables) invoke the iter function or the other way round ?
iter invokes __iter__ which returns an iterator obj
damn daniel
Whats an iterator?
An iterator is an object representing a stream of data. Iterators are required to implement two methods: __next__ and __iter__. The __iter__ method should return the iterator object itself to allow using an iterator with a for...in loop.
Why would we want a custom iterator?
Why would we want to create a custom iterator instead of just putting numbers from zero to nine in a list? This example is indeed trivial! However, consider parsing a 10 GB FASTA file. We probably don’t want to load all the records in memory at once, so we might use an iterator (iterators are lazy). In other situations we might actually need an infinite number of values, or the iteration might have complex logic.
Can you convert finite iterators into other datatypes?
yes you CAN! just like bob the baumeischter
Cordola grün
what does __repr__ do? Whats the diff to __str__?
Implementing the __str__ method allows us to tune casting an object to a string. Implementing the __repr__ method allows us to change the way an object is represented (used as fallback when str not available)
What are callables and name ALL you know!
a callable is something we can call, invoke, execute using parenthesis ();
functions are callables;
classes are callables;
instances of a class that defines a __call__ method are callables.
let us skip to the good part. AAAAAAAAAAAAAAAAAAAAAA
Name examples where decorators are especially useful?
authentication / authorization;
logging;
benchmarking;
validating input;
sanitizing input;
changing function’s output;
registering functions.
Whats the difference between class and instance attributes?
How do you mark “private” variables?
By convention put a _ in front of them:
_private = “deutschland”
How do you impelent class methods, static methods, getters & setters?
classmethod decorator
staticmethod decorator
property and {property_name}.setter decorator
Whats a context manager?
the magic is the return of the __enter__ method (in terms of with open context managers)
What is a generator?
can you use the principle of list comprehension for generators?
Name generators you know.
yyyye
enumerate, zip, map, filter
advice advice advist
Stupor
Whats a closure?
A closure isn’t just a nested (inner) function. The closure mechanism allows the inner function to keep access to its environment as it was when the closure was created. In other words:
1. The outer function defines the inner function and returns it.
2. The nested (inner) function can access all the variables of the outer function (that means the outer_param and the outer_var).
3. The inner function keeps access to the variable of the outer function even after the outer function has finished its execution.
Used in:
data hiding/protection (not bulletproof)
implementing function factories (creating functions at runtime)
decorators
Junior High
how do you specify type hints? Give 3 examples
with :
age: int = ‘too young’ (type hints are just hints)
height: float = 1.41
names: list[str] = [‘Tiffany’, ‘Action Jackson’, ‘Carmen’]
What methods are added by the dataclass decorator by default?
__init__, __repr__, __eq__ the rest can be added in the decorator
With the frozen argument we can make our class read-only sheesh
Last changed2 years ago