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