^^Arithmetic. Operators. Python.

Assegnazione

  1. x = y = z = 0  # A value can be assigned to several variables simultaneously.
  2. a , b = 5 , 7    # multiple assignment
  3. Mixed integer and floating point, convert to float

In-place operators. Infix notation.

%  percentage sign for integer remainder

**  Double star for exponentiation
      Attenzione:  "^"  e' Bitwise XOR, non esponenziazione come in Basic

/    divisione con virgola, cioe' quoziente

//   floor division, il risultato ha tipo intero

Operazione e assegnazione. Augmented assignment.

+=   y += 10   y = y + 10  
-= y -= 10 y = y - 10  
*= y *= 10 y = y * 10  
/= y /= 10 y = y / 10  
%= y %= 10 y = y % 10 replace the value with the old value modulo 10

For mutable objects like lists, arrays, or DataFrames, these augmented assignment operations are actually subtly different than their more verbose counterparts: they modify the contents of the original object rather than creating a new object to store the result.

Comparison (operators)

=    single  is assignment,

==   double is comparison

a == b   a != b   a uguale b,  non uguale
a < b a > b a minore b,  maggiore
a <= b a >= b a minore o uguale,  maggiore o uguale

 

1 < 3 < 5 unlike most programming languages, Python allows inequalities to be chained together, just as in mathematics.

Boolean logic.  Operator    and  or  not

a XOR b    equi    a != b

Identity Operators: "is" and "is not"

check for object identity.

Object identity is different than equality !

a = [1, 2, 3]
b = [1, 2, 3]

c = b

a == b

 True

a is b

 False

c is b

 True


Membership operators:  "in"  "not in"

check for membership within compound objects.

2 in [1, 2, 3]

 True

2 not in [1, 2, 3]

 False

Python  VS   lower-level languages such as C

Python easy to use, es membership operators

carleton.ca/paulogarcia/how-to-confront-programming-languages

 

Conditional espression. Ternary operator.

  1. reference/conditional-expressions (sometimes called a “ternary operator”) have the lowest priority of all Python operations.

    The expression   x if C else y 
    first evaluates the condition, C rather than x. If C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned

  2. Storia
    1. What’s New in Python 2.5 PEP 308: Conditional Expressions
    2. PEP 308 - Conditional Expressions. PEP written by Guido van Rossum and Raymond D. Hettinger; implemented by Thomas Wouters.