Python Language aggiunta


Esempio

a, b = 1, 2

# Using the "+" operator:
a + b                  # = 3

# Using the "in-place" "+=" operator to add and assign:
a += b                 # a = 3 (equivalent to a = a + b)

import operator        # contains 2 argument arithmetic functions for the examples

operator.add(a, b)     # = 5  since a is set to 3 right before this line

# The "+=" operator is equivalent to: 
a = operator.iadd(a, b)    # a = 5 since a is set to 3 right before this line

Combinazioni possibili (tipi predefiniti):

  • int e int (dà un int )
  • int e float (dà un float )
  • int e complex (dà un complex )
  • float e float (dà un float )
  • float e complex (dà un complex )
  • complex e complex (dà un complex )

Nota: l'operatore + è anche usato per concatenare stringhe, liste e tuple:

"first string " + "second string"    # = 'first string second string'

[1, 2, 3] + [4, 5, 6]                # = [1, 2, 3, 4, 5, 6]