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