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