Level 9 : What will this code output?


import threading
x = 0

def inc():
    global x
    for _ in range(1000):
        x += 1

t1 = threading.Thread(target=inc)
t2 = threading.Thread(target=inc)
t1.start(); t2.start()
t1.join(); t2.join()

print(x)

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