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