Computer Science

Python Assignment Operator

The Python assignment operator, denoted by the equals sign (=), is used to assign a value to a variable. It is a fundamental concept in Python programming and is used to store and manipulate data. The assignment operator allows for the dynamic allocation of memory and is essential for performing operations and calculations in Python.

Written by Perlego with AI-assistance

5 Key excerpts on "Python Assignment Operator"

  • Book cover image for: Data Analysis with Python
    eBook - ePub

    Data Analysis with Python

    Introducing NumPy, Pandas, Matplotlib, and Essential Elements of Python Programming (English Edition)

    For assigning the value to a variable, we use assignment operators. The following is a list of assignment operators in Python:
    Operator name Operator symbol Description Example
    Assign = Assign the value of the right operand to the left operand a=b
    Addition and Assign += Add the value of the right operand to the left and assign the result to the left operand a+=10 (a = a+10)
    Subtract and Assign -= Subtract the value of right operand to the left and assign the result to the left operand a-=10 (a = a-20)
    Multiply and Assign *= Multiply the value of right operand to the left and assign the result to the left operand a*=10 (a = a*5)
    Divide and Assign /= Divide the value of right operand to the left and assign the result to left operand a/=2 (a = a/2)
    Floor Divide and Assign //= Floor divide the value of the right operand to the left and assign the result to the left operand a//=9 (a = a//9)
    Modulus and Assign %= Perform the modulus by the value of the right operand on the left and assign the result to the left operand a%=3 (a = a%3)
    Exponent and Assign **= Perform the exponent of operands and assign the result to the left operand a**=2 (a = a**2)
    Table 3.3:
  • Book cover image for: Introduction to Computing Using Python
    eBook - PDF

    Introduction to Computing Using Python

    An Application Development Focus

    • Ljubomir Perkovic(Author)
    • 2015(Publication Date)
    • Wiley
      (Publisher)
    The general format of an assignment statement is: = An expression we refer to as lies on the right-hand side of the = operator; it can be an algebraic, Boolean, or other kind of expression. On the left-hand side is a variable referred to as . The assignment statement assigns to the value that evaluates to. In the last example, x is assigned value 4. Section 2.1 Expressions, Variables, and Assignments 21 Once a value has been assigned to a variable, the variable can be used in a Python expression: >>> x 4 When Python evaluates an expression containing a variable, it will evaluate the variable to its assigned value and then perform the operations in the expression: >>> 4 * x 16 An expression involving variables may appear on the right side of an assignment statement: >>> counter = 4 * x In statement counter = 4 * x, x is first evaluated to 4, then the expression 4 * 4 is evaluated to 16, and then 16 gets assigned to variable counter: >>> counter 16 So far, we have defined two variable names: x with value 4 and counter with value 16. What about, say, the value of variable z that has not been assigned yet? Let’s see: >>> z Traceback (most recent call last): File "", line 1, in z NameError: name 'z' is not defined Not sure what we expected . . . but here we got our first (and, unfortunately, not the last) error message. It turns out that if a variable—z in this case—has not been assigned a value, it just does not exist. When Python tries to evaluate an unassigned name, an error will occur and a message (such as name 'z' is not defined) is printed out. We will learn more about errors (also called exceptions) in Chapter 4. Practice Problem 2.3 Write Python statements that correspond to the actions below and execute them: (a) Assign integer value 3 to variable a. (b) Assign 4 to variable b. (c) Assign to variable c the value of expression a * a + b * b.
  • Book cover image for: Computing Skills for Biologists
    For example, In [10]: 2 * 3 ** 3 Out[10]: 54 This is interpreted as 2 ⋅ 3 3 , rather than ( 2 ⋅ 3 ) 3 . However, relying on operator precedence makes the code less readable. You should use parentheses to make the calculation clearer: In [11]: 2 * (3 ** 3) Out[11]: 54 In [12]: (2 * 3) ** 3 Out[12]: 216 If this is your first time programming, you may not be familiar with the modulo operator, which returns the remainder of an integer division: Basic Programming ● 87 In [13]: 15 % 7 Out[13]: 1 that is, 15 = ( 7 ⋅ 2 ) + 1 (and as such 1 is the remainder of the integer division 15//7 ). 3.3.4 Variable Assignment When programming, typically you manipulate variables. One can think of a variable as a box that contains a value. To create a new variable, simply assign a value to it: In [1]: x = 5 # assign value 5 to variable x In [2]: x # display x Out[2]: 5 As you can see, Python uses the equals sign to mean “take whatever value is on the right of the equals sign, and assign it to the variable on the left of the equals sign.” Note that this means that we will need another symbol to mean “equality,” as the equals sign is used for variable assignment. In Python, you can test for equality with the double equals sign == (e.g., 3 == 2 + 1 yields True ). Whenever you create a variable, its name is stored in the memory. To see which variables you have created in the current session, type who : In [3]: who Out[3]: x Once you have defined a variable, you can use it to perform operations. Each time, Python will look up the value of the variable, and use it to produce the result: In [4]: x + 3 Out[4]: 8 In [5]: y = 8 In [6]: x + y Out[6]: 13 88 ● Chapter 3 Each variable contains data of a certain type. This data can be of a basic type such as integers, floats, strings, or Boolean, which we have already encountered, or the more complex data structures introduced below.
  • Book cover image for: Introduction to Computing Using Python
    eBook - PDF

    Introduction to Computing Using Python

    An Application Development Focus

    • Ljubomir Perkovic(Author)
    • 2012(Publication Date)
    • Wiley
      (Publisher)
    The general format of an assign- ment statement is: = An expression we refer to as lies on the right-hand side of the = operator; it can be an algebraic, Boolean, or other kind of expression. On the left-hand side is a variable referred to as . The assignment statement assigns to the value that evaluates to. In the last example, x is assigned value 4. Section 2.1 Expressions, Variables, and Assignments 21 Once a value has been assigned to a variable, the variable can be used in a Python expression: >>> x 4 When Python evaluates an expression containing a variable, it will evaluate the variable to its assigned value and then perform the operations in the expression: >>> 4 * x 16 An expression involving variables may appear on the right side of an assignment statement: >>> counter = 4 * x In statement counter = 4 * x, x is first evaluated to 4, then the expression 4 * 4 is evaluated to 16, and then 16 gets assigned to variable counter: >>> counter 16 So far, we have defined two variable names: x with value 4 and counter with value 16. What about, say, the value of variable z that has not been assigned yet? Let’s see: >>> z Traceback (most recent call last): File "", line 1, in z NameError: name 'z' is not defined Not sure what we expected . . . but here we got our first (an, unfortunately, not the last) error message. It turns out that if a variable—z in this case—has not been assigned a value, it just does not exist. When Python tries to evaluate an unassigned name, an error will occur and a message (such as name 'z' is not defined) is printed out. We will learn more about errors (also called exceptions) in Chapter 4. Practice Problem 2.3 Write Python statements that correspond to the below actions and execute them: (a) Assign integer value 3 to variable a. (b) Assign 4 to variable b. (c) Assign to variable c the value of expression a * a + b * b.
  • Book cover image for: An Introduction to Python Programming: A Practical Approach
    eBook - ePub

    An Introduction to Python Programming: A Practical Approach

    step-by-step approach to Python programming with machine learning fundamental and theoretical principles.

    • Dr. Krishna Kumar Mohbey; Dr. Brijesh Bakariya(Author)
    • 2021(Publication Date)
    • BPB Publications
      (Publisher)
    shortcut operator . There are various types of assignment and shortcut operators.
    • Assignment (=) operator : This operator assigns values from right side operands to left side operand z = x + y assigns x + y into z .
    • Add AND (+=) operator : This operator adds the right operand to the left operand and assigns the result to the left operand z += x is equivalent to z = z + x .
    • Subtract AND (-=) operator : This operator subtracts the right operand from the left operand and assigns the result to the left operand z -= x equals z = z – x .
    • Multiply AND (*=) operator : This operator multiplies the right operand with the left operand and assigns the result to the left operand z *= x is equivalent to z = z * x .
    • Divide AND (/=) operator : This operator divides the left operand with the right operand and assigns the result to the left operand z /= x is equivalent to z = z / x .
    • Modulus AND (%=) operator : This operator takes modulus using two operands and assigns the result to left operand z %= x is equivalent to z = z % x .
    • Exponent AND (**=) operator : This operator performs exponential (power) calculation on operators and assign value to the left operand z **= x is equivalent to z = z ** x
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.