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