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