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