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