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