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