Level 1 : What will be the output of the following Python code?


def f():
    global a
    print(a)
    a = "hello"
    print(a) 
a = "world" 
f()
print(a)

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