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