Level 9 : What happens when we send() values into a generator?


def accumulator():
    total = 0
    while True:
        value = yield total
        if value is None:
            break
        total += value

gen = accumulator()
print(next(gen))
print(gen.send(10))
print(gen.send(20))
print(gen.send(None))

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