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