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