Assignment Operator
Python Assignment Operator is used to assign a value to a variable. The value could be the result of evaluating an expression, return value from a function, or another variable, etc.
In this tutorial, we will learn the syntax of Assignment Operator and how to use it, with examples.
Syntax
The syntax of Assignment Operator is
variable_name = value
where variable_name is any Python Variable, and value is a string literal, a number, constant, value returned from a function call, an expression, etc.
=
symbol is used for Assignment Operator.
Assign a Value to Variable
In the following program, we assign a value of 'hello world'
to the variable message
.
message = 'hello world'
Python Interpreter assigns the value given on the right hand side to the variable in the left hand side.
Assign an Expression to a Variable
We can assign an expression to the variable using assignment operator.
In the following example, we have taken two variables:a
, b
with values 10, 25 assigned to them respectively. In the third statement, are assigning the expression a + b
to the variable result
.
a = 10
b = 25
result = a + b
Example
In the following example, we assign a variable with different values one by one, and print them to console.
Python Program
#assign a value to variable
message = 'hello world'
print(message)
#assing an expression to a variable
a = 'hello '
b = 'user'
message = a + b
print(message)
#assing value returned by function to a variable
message = 'hello'.upper()
print(message)
Output
hello world
hello user
HELLO
Conclusion
In this Python Tutorial, we learned the syntax of Assignment Operator, and how to use it to assign a value to a variable.