Level 9 : What will be the output of the following code?


def alternating_signs(n):
    for i in range(n):
        yield (-1) ** i * i

gen = alternating_signs(5)
print(list(gen))

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