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