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