Python Assigment Operators
Assignment operators are used in Python to assign
values to variables.
Always the value in Right is assigned to the value in Left.
This is a simple Assigment Operator - equal(=)
#The value 100 is assigned to variable "a".
a = 100
#The string value "Hello World" is assigned to variable "b".
b = "Hello World"
print(a)
print(b)
Press Run to execute.
In the above example you can identify that when printing variables as an output we do not use quotes.
In Python, when using the 'print' statement,
everything inside quotes(" ") are shown as it is.Therefore variables are not declared inside
quotes(" ")
When using variables we can use a comma( , ) to seperate it from the value inside quotes(" ").
fruit = "Apple"
print("My favourite fruit is",fruit)
Press Run to execute.
Here are few basic Assignment Operators.
Operator | Description | Example |
---|---|---|
= | Assigns values from right side operands to left side operand | c = a + b assigns value of a + b into c |
+= Add AND | It adds right operand to the left operand and assign the result to left operand | c += a is equivalent to c = c + a |
-= Subtract AND | It subtracts right operand from the left operand and assign the result to left operand | c -= a is equivalent to c = c - a |
*= Multiply AND | It multiplies right operand with the left operand and assign the result to left operand | c *= a is equivalent to c = c * a |
/= Divide AND | It divides left operand with the right operand and assign the result to left operand | c /= a is equivalent to c = c / a |
%= Modulus AND | It takes modulus using two operands and assign the result to left operand | c %= a is equivalent to c = c % a |
**= Exponent AND | Performs exponential (power) calculation on operators and assign value to the left operand | c **= a is equivalent to c = c ** a |
//= Floor Division | It performs floor division on operators and assign value to the left operand | c //= a is equivalent to c = c // a |