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