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