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