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