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