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