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