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