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