Keywords Flashcards
Name four scenarios where as
keyword is used.
- To create an alias for module name in import statement.
- To create an alias for sub-module name in import statement.
- To create an alias for resource in with resource statement.
- To create an alias for exception.
What’s the syntax for creating an alias for exception with as
keyword?
except <exception_name> as <alias_name>
Does assert
throw an error when the following condition is true or false?
When the condition is false.
What’s the syntax for assert
in python?
assert condition, message
What’s the purpose of the del
keyword in python?
del
is used to delete the reference to an object. Ex:
>>> a = b = 5 >>> del a >>> a Traceback (most recent call last): File "<string>", line 301, in runcode File "<interactive input>", line 1, in <module> NameError: name 'a' is not defined >>> b 5
What’s the syntax for try except blocks in python?
try: # Some code except <Exception>: # Run on <Exception> except: # It's possible to put more than one except block under one try # block, and you don't need to specify the <Exception> after the # except keyword
What’s the syntax for the raise
keyword in python?
if num == 0: raise ZeroDivisionError('cannot divide')
What’s the purpose of the finally
keyword?
finally
is used with try…except block to close up resources or file streams.
Using finally
ensures that the block of code inside it gets executed even if there is an unhandled exception.
What are the two uses for the global
keyword?
- To create a global variable inside a function
- To modify a global variable inside a function
What’s the use of the in
and not in
keywords?
in
is used to test if a sequence (list, tuple, string etc.) contains a value. It returns True
if the value is present, else it returns False
. not in
works equivalently.
Can in
and not in
only be used with sequence data structures like lists or sets?
No, it can also be used with generators like range()
What’s the use of the is
keyword in python?
is
is used in Python for testing object identity. While the == operator is used to test if two variables are equal or not, is
is used to test if the two variables refer to the same object.
What’s the syntax for an anonymous function in python?
lambda a: a**2
What’s the difference between the nonlocal
keyword and the global
keyword?
The difference is that the nonlocal
keyword is used only with variables outside the local scope, like for example variables declared in outer function, but not with global variables.
What’s the syntax for the with
statement?
with expression as target_var: do_something(target_var)
Is the is
keyword required in with
statements?
No, the short version of the with
statement is:
with expression: do_something(expression)
Can you use only one context menager with one with
statement?
No, you can use multiple context menagers with one with
statement and separate them with commas:
with A() as a, B() as b: pass
How does the yield
keyword work, and what is a generator?
yield
is used inside a function like a return
statement. But yield
returns a generator.
Introduced withPEP 255, generator functionsare a special kind of function that return a lazy iterator. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their contents in memory.
While using yield
instead of return
the state of the function is preserved and the next time the generator function (the function with yield
) is called the execution resumes from the next line after yield
statement.
What’s the syntax for generator comprehension?
csv_gen = (row for row in open(file_name))
What are the three ways to use a generator?
Create an object:
generator = gen()
for i in generator:
pass
Use as is:
for i in gen():
pass
Use a special function:
next(gen())
Can the yield
keyword be used in a expression?
Yes, the yield
keyword can be used in an expression and then after the execution stops, the .send()
method can be used to send the value to the generator:
>>> def double_inputs(): ... while True: ... x = yield # yield can also be used as an expression ... yield x * 2 ... >>> gen = double_inputs() >>> next(gen) # run up to the first yield >>> gen.send(10) # goes into 'x' variable
How to stop a generator?
There are two ways to stop a generator, using the .throw()
method and .close()
method. Both work in similar ways, but with the .throw()
method its possible to pass an error to the generator:
>>> def double_inputs(): ... while True: ... x = yield # yield can also be used as an expression ... yield x * 2 ... >>> gen = double_inputs() >>> next(gen) # run up to the first yield >>> gen.send(10) # goes into 'x' variable