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