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