12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

issue, initialize mutable objects in separate statements instead, so that each creates adistinct empty object by running a distinct literal expression:>>> a = []>>> b = []>>> b.append(42)>>> a, b([], [42])Augmented AssignmentsBeginning with <strong>Python</strong> 2.0, the set of additional assignment statement formats listedin Table 11-2 became available. Known as augmented assignments, and borrowedfrom the C language, these formats are mostly just shorthand. They imply the combinationof a binary expression, and an assignment. For instance, the following twoformats are now roughly equivalent:X = X + YX += Y# Traditional form# Newer augmented formTable 11-2. Augmented assignment statementsX += Y X &= Y X -= Y X |= YX *= Y X ^= Y X /= Y X >>= YX %= Y X > x = 1>>> x = x + 1 # Traditional>>> x2>>> x += 1 # Augmented>>> x3When applied to a string, the augmented form performs concatenation instead.Thus, the second line here is equivalent to typing the longer S = S + "SPAM":>>> S = "spam">>> S += "SPAM" # Implied concatenation>>> S'spamSPAM'As shown in Table 11-2, there are analogous augmented assignment forms for every<strong>Python</strong> binary expression operator (i.e., each operator with values on the left andright side). For instance, X*= Y multiplies and assigns, X>>= Y shifts right and assigns,and so on. X//= Y (for floor division) was added in version 2.2.Assignment Statements | 223

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!