Level 1 : What is the output of the following code?


def filter_even(nums):
    for num in nums:
        if num % 2 == 0:
            yield num

g = filter_even(range(10))
print(next(g), next(g), next(g))
print(list(g))

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