Keywords Flashcards

1
Q

Name four scenarios where as keyword is used.

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s the syntax for creating an alias for exception with as keyword?

A
except <exception_name> as <alias_name>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Does assert throw an error when the following condition is true or false?

A

When the condition is false.

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

What’s the syntax for assert in python?

A
assert condition, message
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the purpose of the del keyword in python?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the syntax for try except blocks in python?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the syntax for the raise keyword in python?

A
if num == 0:
    raise ZeroDivisionError('cannot divide')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the purpose of the finally keyword?

A

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.

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

What are the two uses for the global keyword?

A
  • To create a global variable inside a function
  • To modify a global variable inside a function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What’s the use of the in and not in keywords?

A

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.

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

Can in and not in only be used with sequence data structures like lists or sets?

A

No, it can also be used with generators like range()

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

What’s the use of the is keyword in python?

A

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.

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

What’s the syntax for an anonymous function in python?

A
lambda a: a**2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s the difference between the nonlocal keyword and the global keyword?

A

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.

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

What’s the syntax for the with statement?

A
with expression as target_var:
    do_something(target_var)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Is the is keyword required in with statements?

A

No, the short version of the with statement is:

with expression:
    do_something(expression)
17
Q

Can you use only one context menager with one with statement?

A

No, you can use multiple context menagers with one with statement and separate them with commas:

with A() as a, B() as b:
    pass
18
Q

How does the yield keyword work, and what is a generator?

A

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.

19
Q

What’s the syntax for generator comprehension?

A
csv_gen = (row for row in open(file_name))
20
Q

What are the three ways to use a generator?

A

Create an object:
generator = gen()

for i in generator:
pass
Use as is:
for i in gen():
pass
Use a special function:
next(gen())

21
Q

Can the yield keyword be used in a expression?

A

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
22
Q

How to stop a generator?

A

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