Python Assignment Statements
Python Assignment Statements
Let’s learn about Python assignment statements in this tutorial with the help of some examples. In Python language, variables are created by the assignment statements.
Assignment Statement
An assignment statement assigns a value to a variable by using an assignment operator (=). Assignment operators link a name on the left-hand side of the operator with a value on the right-hand side.
Examples
Let’s explore some assignment statement examples.
length = 18
In this example, the integer 18 is assigned to the variable length
breadth = 2.75
In this example, the floating-point number 2.75 is assigned to the variable breadth
str= “World Cup”
In this example, the string “World Cup” is assigned to the string variable str. The assignment operator (=) should not be confused with an equal sign used in Mathematics.
Let’s take an example of the following assignment statement:
x = 9 # x holds 9
x=x+5
The second assignment statement does the following things:
- The interpreter evaluates the x + 5 expression first.
- Reads the current value of the variable x. The variable x holds the value 9.
- Now, it adds the value 5 to the current value of the x, i.e., 9+5=14
- The evaluated value 14 is assigned back to the memory location of x; it will overwrite the previous value of the variable x (i.e., 9) with the new value(i.e., 14).
To verify this, let’s print the value of the x using the print statement.
x = 9
x = x + 5
print(x)
—
Python Tutorials
Python Tutorial on this website can be found at:
https://www.testingdocs.com/python-tutorials/
More information on Python is available at the official website: