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