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