Level 10 : What is the output?


def gen():
    try:
        yield 1
        yield 2
        yield 3
    except GeneratorExit:
        print("Generator closed")

g = gen()
print(next(g))
g.close()
print(next(g))

Author: SandhyaR | Tags: generator | 1859+ Python FAQs | Share
control-statement | recursion | enumerate() | function | datetime | dictionary-comprehensions | constructor | dataframe-and-series-objects | counter | file-handling | descriptors | complex | file-handling | identity-operators-(is,-is-not) | destructor | introduction-to-python | difference-between-threads-and-processes | integer | dictionary-methods | datetime.date,-datetime.time,-datetime.datetime | generators-in-python | directory-operations | comprehension | managing-directories-with-os-and-shutil | method | data-structures-in-python | objects | data-structures | membership-operators-(in,-not-in) | loops | data-type | conditional-statement | list-indexing | handling-missing-values-(fillna,-dropna) | methods | multiple-inheritance | list,-set,-and-dictionary-comprehensions | multiple-inheritance | method-overloading | magic-method | matplotlib | input-function | loop-control-statements | memory-management | nested-loop | command-line-parsing | list-slicing | defining-functions | namespace | default-arguments | exponentiation | choice | comment | polymorphism | operator-or | copy | feature | data-structure | data-type-conversions | head-and-tail | virtual-environment | writing:-write(),-writelines() | type-conversion-and-casting | library | array | working-with-file-paths | bitwise-operator | asyncio-module | using-os-and-pathlib-modules | escape-operator | collection | dicts | tuple-methods | oops | type-conversion | formatting-with-strftime | pseudocode | regex | implementation | module | python-history | binary | mathematical-function | list-mutation | int | sys | syntax | operations | operator-overloading | zfill-function | round-function | list-comprehension | print | virtual-environment | decorator | join | multithreading | name-mangling | literal-characters-and-special-characters | bitwise | list-method | string | random-function | conditional-statements | string-slicing | async | binary-operation | set | string-iteration | assert | while-and-for-loop | working-with-temporary-files-using-tempfile | regular-expression | context-manager | arithmetic-operation | benefit | error-handling | gil | filter-function | static-method | lambdas | global-variable | reading:-read(),-readline(),-readlines() | syntax-error | encoding | variable | format | string-method | statement | exception | input | exception-handling | syntax-error | import-method | working-with-dates-and-times | rollback | string-methods | string-slicing | conversion | tuples | version | expression | pickling--unpickling | global-keyword | swapping | set-operation | global | string-formatting | inheritance | heap-memory | keyword | while-loop | list-join | immutable-data-type | package-management | factorial-function | math-copysign | min-max-function | expand-tab | lambda-functions | init | lists | anchors-(^,-$) | filtering-and-indexing | __future__ | precedence | input-function | class-and-instance-variable | single-inheritance | round | global-variable | strings | class | matrix-list-comprehension | immutability | docstring | python | pass | id-function | namedtuple | range | fabs-function | using-yield-in-functions | list-methods-and-slicing | installation | python-syntax-and-structure | while-loops | comparison | data-type | math | matrix | min-function | precedence-and-associativity | version-check | floats | shallow-copy | pep | frozenset | reading-and-writing-large-files-efficiently | thread | python-module | built-in | return-statement | parameter | call | lambda-function | using-map()-for-transformations | class-and-inheritance | functional-programming | multiprocessing | odd-number | application | typeerror | exception-handling | invalid-list | dictionaries | iterators | default-mutable-argument | error-handling | update | recurssion | def | iterable | list-comprehension | zip-function | built-in-modules | data-structure | key-value-pairs | encapsulation | else-statement | __all__-attribute | assignment-operator | higher-order-function | map-function | with-statement | walking-through-directories-using-os.walk() | advanced-file-operations | python_version | collections | datatype | logical-operator | intering | tuple | lambda | mysql | software-design | using-join()-and-is_alive()-methods | match | functional-programming | multiple-except-blocks | max-min | bitwise-operator | zip | using-else-and-finally | string-concatenation | arithmetic-expression | formal-and-actual-parameter | error | slicing | center | using-the-threading-module | sum() | scientific-notation | logical-operator | python-django | regular-expressions | thread-synchronization | boolean-masking | booleans | asyncio | decimal | compute | control-flow | overflow | deep-copy | thread-safe-data-structures-(queue.queue) | counter-function | floating-point-precision | sum-function | inherit | tuple-unpacking | numeric | string-manipulation | __init_subclass__ | creating-custom-modules | unit-testing | boolean | function-and-scope | while-else-loop | dictionary | list-operation | built-in-function | identifier | mapping-function | generators | sorted | object-attribute | using-lists-as-stacks | goto-function | attribute | test-discovery | identity-operator | memory | list-vs-tuple | mutable | threading | debugging | sys-module | file_handling | opening-files-(open()) | generator | random | reading-and-writing-files | condition | operator | staticmethod | basic | data_structure | access-modifier | yield | function-argument | overriding-methods | time-series-with-pandas | postfix | sets | indentation | data-types | class-decorators | logical-operation | indexing:-loc-vs-iloc | type-conversion | append | built-in-function | numpy | for | logical-operators | mathematics | complex-number | assignment-operators | loop-control | identity-operator | file | oop | arbitrary-arguments-(*args,-**kwargs) | operators | classes | turtle | list-comprehensions | boolean-and-non-boolean | class-and-object | string-formatting | operand | loop | string-method | aggregating-data-with-functools.reduce() | touples | sorting-and-ranking | raise-statement | design-principle | else | raise | functions | conditional-statement | parsing-with-strptime | introduction-to-multiprocessing | control-flow | why-use-multiprocessing-over-threading?-(gil) | introduction-to-threads | python-repl | joining-and-merging-dataframes | operator-and | python-operators | using-assertions-(assertequal,-assertraises) | split | interpreter | sorting | print-statement | integers | list | slice | quantifiers-(*,-+,-?,-{}) | indexing | syntaxerror | iterator | filter | type_conversion | pandas | stack | creating-and-starting-threads | working-with-binary-files-(rb,-wb) ...