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