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