Level 10 : What will be the output of the following Python code?


def foo():
    try:
        return 1
    finally:
        return 2
k = foo()
print(k)

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