
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between 'and' Operators in Python
The symbols "=" and "==" look similar but have different meanings and usability in Python. The "=" symbol is the assignment operator, and the "==" symbol represents a comparison operator. In this article, we will understand the difference between these two and how to use them.
The "=" Operator
The "=" operator in Python is the assignment operator. It is used to assign a value to a variable. You put the variable on the left side and the value or expression on the right side. The value on the right is stored in the variable on the left. The expression and name of the variable are not interchangeable.
Examples of the "=" Operator
In this section, we will see some examples of using assignment operators in Python to assign values to variables or expressions.
Example 1
In the example below, age and name variables have been assigned values. These values are used by the print statement, printing the text within it while taking the values of variables.
age = 22 Name = "Ajay" print(f"{Name} is {age} years old")
Following is the output of the above program -
Ajay is 22 years old
Example 2
In the example below, a and b have been assigned values using the assignment operator. The sum variable has been assigned an expression "a+b", and it stores the result of the expression.
a = 5 b = 3 sum = a + b print(sum)
Following is the output of the above program -
8
The "==" Operator
The "==" operator in Python represents the comparison operator. This is used to check if two values are equal or not. It returns a boolean value. If the values are the same, it will return true, and if the values are not the same, it will return false.
Examples of == Operator
In this section, we will see some examples of using comparison operators in Python for comparing the values of variables or expressions.
Example 1
The following is a simple example of a comparison operator. The variable a is assigned the value 10. The first line checks if a is equal to 10 (which is True), and the second line checks if a is equal to 5 (which is False).
a = 10 print(a == 10) print(a == 5)
Output
True False
Example 2
The below example checks if the value of favorite_color is equal to "blue". If it is, it prints "Your favorite color is blue!" Otherwise, it prints "Your favorite color is not blue."
favorite_color = "blue" # Checking if the favorite color is blue if favorite_color == "blue": print("Your favorite color is blue!") else: print("Your favorite color is not blue.")
Output
Your favorite color is blue!