Skip to main content
Chemistry LibreTexts

5.4: Assignment Operators

  • Page ID
    469579
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    The Assignment Operator (=)

    Assignment operators assign a value to a variable in python.  The equal sign is the simple assignment operator, and it assigns the value on the right hand side to a variable on the left hand side. The value on the right hand side can any data type (integer, float, boolean)

     

    Predict the output of the following code, and then run it.  Note, the type function indicates the data type of a variable. In Python you can change variable data types with the assignment operator

    a="b"
    print(a)
    print(type(a))
    print(3*" *! ")
    a=3
    print(a)
    print(type(a))
    Hello world!

    Shorthand Assignment Operators

    Shorthand assignment operators allow you to assign a variable and do an arithmetic operation at the same time

    Plus Equal (+=)

    Allows you to add a value to a variable and reassign the variable the new value. This can be very useful if a variable is incrementally increasing in value. Predict the output before running the code

    a=10
    print(a)
    a=a+5
    print(a)
    Hello world!

    Now try this

    a=10
    print(a)
    a+=5
    print(a)
            hello world
          

    the += operation also works on strings

    a="hydrogen"
    print(a)
    a+=" helium"
    print(a)
    
            hello world
          

     

    Minus Equal (-=)

    Allows you to remove a value from a number.  Note, you can mix integers with floating numbers, but can not do this for strings. When you do an operation with an integer and a float, the value turns to a float

    a=10
    print(a)
    a-=5
    print(a)
    print(type(a))
    a-=1.0
    print(a)
    print(type(a))
    a+=1
    print(a)
    print(type(a))
    Hello world!

    Note, the -= does not work for strings

    a="10"
    print(a)
    a-="5"
    print(a)
    Hello world!

    Multiple equal (*=)

    a=2
    print(a)
    a*=3
    print(a)
            hello world
          

    Divide Equal (/=)

    a=2
    print(a)
    print(type(a))
    a/=3
    print(a)
    print(type(a))
    Hello world!

    Modulus Equal (%=)

    a=25
    print(a)
    a%=2
    print(a)
    b=24
    print(b)
    b%=2
    print(b)
    Hello world!

    Floor Equal (//=)

    a=25
    print(a)
    a//=2
    print(a)
    b=24
    print(b)
    b//=2
    print(b)
    
            hello world
          

     


    This page titled 5.4: Assignment Operators is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?