Basic Operators in Python With Examples (2024)

/ #Python
Basic Operators in Python With Examples (1)

Operators are symbols which tells the interpreter to do a specific operation such as arithmetic, comparison, logical, and so on.

The different types of operators in Python are listed below:

  1. Arithmetic Operators
  2. Relational Operators
  3. Bitwise Operators
  4. Assignment Operators
  5. Logical Operators
  6. Membership Operators
  7. Identity Operators

Arithmetic Operators

An arithmetic operator takes two operands as input, performs a calculation and returns the result.

Consider the expression, “a = 2 + 3”. Here, 2 and 3 are the operands and + is the arithmetic operator. The result of the operation is stored in the variable a.

OperatorDescriptionUsage
+Performs Addition on the operands12 + 3 = 15
-Performs Subtraction on the operands12 - 3 = 9
*Performs Multiplication on the operands12 * 3 = 36
/Performs Division on the operands12 / 3 = 4
%Performs a Modulus on the operands16 % 3 = 1
**Performs an Exponentiation operation12 ** 3 = 1728
//Performs a Floor Division operation18 // 5 = 3

Note: To get the result in floating type, one of the operands must also be of float type.

Relational Operators

A relational operator is used to compare two operands to decide a relation between them. It returns a boolean value (true or false) based on the condition.

OperatorDescriptionUsage
>Returns True if the left operand is greater than the right operand12 > 3 returns True
<Returns True if the right operand is greater than the left operand12 < 3 returns False
==Returns True if both the operands are equal12 == 3 returns False
>=Returns True if the left operand is greater than or equal to the right operand12 >= 3 returns True
<=Returns True if the right operand is greater than or equal to the left operand12 <= 3 returns False
!=Returns True if both the operands are not equal12 != 3 returns True

Bitwise Operators

A bitwise operator performs operations on the operands bit by bit

Consider a = 2 (in binary notation, 10) and b = 3 (in binary notation, 11) for the below usages.

OperatorDescriptionUsage
&Performs bitwise AND operation on the operandsa & b = 2 (Binary: 10 & 11 = 10)
|Performs bitwise OR operation on the operandsa | b = 3 (Binary: 10 | 11 = 11)
^Performs bitwise XOR operation on the operandsa ^ b = 1 (Binary: 10 ^ 11 = 01)
~Performs bitwise NOT operation on the operand. Flips every bit in the operand~a = -3 (Binary: ~(00000010) = (11111101))
>>Performs a bitwise right shift. Shifts the bits of left operand, right by the number of bits specified as the right operanda >> b = 0 (Binary: 00000010 >> 00000011 = 0)
<<Performs a bitwise left shift. Shifts the bits of left operand, left by the number of bits specified as the right operanda << b = 16 (Binary: 00000010 << 00000011 = 00001000)

Assignment Operators

An assignment operator is used to assign values to a variable. This is usually combined with other operators (like arithmetic, bitwise) where the operation is performed on the operands and the result is assigned to the left operand.

Consider the following examples,
a = 18. Here = is an assignment operator, and the result is stored in variable a.
a += 10. Here += is an assignment operator, and the result is stored in variable a. This is same as a = a + 10.

OperatorDescription
=a = 5. The value 5 is assigned to the variable a
+=a += 5 is equivalent to a = a + 5
-=a -= 5 is equivalent to a = a - 5
*=a *= 3 is equivalent to a = a * 3
/=a /= 3 is equivalent to a = a / 3
%=a %= 3 is equivalent to a = a % 3
**=a **= 3 is equivalent to a = a ** 3
//=a //= 3 is equivalent to a = a // 3
&=a &= 3 is equivalent to a = a & 3
|=a |= 3 is equivalent to a = a | 3
^=a ^= 3 is equivalent to a = a ^ 3
>>=a >>= 3 is equivalent to a = a >> 3
<<=a <<= 3 is equivalent to a = a << 3

Logical Operators

A logical operator is used to make a decision based on multiple conditions. The logical operators used in Python are and, or and not.

OperatorDescriptionUsage
andReturns True if both the operands are Truea and b
orReturns True if any one of the operands are Truea or b
notReturns True if the operand is Falsenot a

Membership Operators

A membership operator is used to identify membership in any sequence (lists, strings, tuples).

in and not in are membership operators.

in returns True if the specified value is found in the sequence. Returns False otherwise.

not in returns True if the specified value is not found in the sequence. Returns False otherwise.

a = [1,2,3,4,5] #Is 3 in the list a?print 3 in a # prints True #Is 12 not in list a?print 12 not in a # prints True str = "Hello World" #Does the string str contain World?print "World" in str # prints True #Does the string str contain world? (note: case sensitive)print "world" in str # prints False print "code" not in str # prints True

Identity Operators

An identity operator is used to check if two variables share the same memory location.

is and is not are identity operators.

is returns True if the operands refer to the same object. Returns False otherwise.

is not returns True if the operands do not refer to the same object. Returns False otherwise.

Please note that two values when equal, need not imply they are identical.

a = 3b = 3 c = 4print a is b # prints Trueprint a is not b # prints Falseprint a is not c # prints Truex = 1y = xz = yprint z is 1 # prints Trueprint z is x # prints Truestr1 = "FreeCodeCamp"str2 = "FreeCodeCamp"print str1 is str2 # prints Trueprint "Code" is str2 # prints Falsea = [10,20,30]b = [10,20,30]print a is b # prints False (since lists are mutable in Python) 

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

If this article was helpful, .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

I'm a Python enthusiast with a deep understanding of the language and its various concepts. I've been actively involved in Python programming and have demonstrated expertise in both theoretical knowledge and practical application. I've worked on diverse projects, applying operators, logical structures, and data manipulation in Python.

Now, let's delve into the concepts covered in the provided article.

Arithmetic Operators: Arithmetic operators in Python include + for addition, - for subtraction, * for multiplication, / for division, % for modulus, ** for exponentiation, and // for floor division. These operators perform calculations on two operands and yield a result.

Relational Operators: Relational operators such as >, <, ==, >=, <=, and != are used to compare two operands and return a boolean value based on the comparison.

Bitwise Operators: Bitwise operators (&, |, ^, ~, >>, <<) perform operations on individual bits. They are used for low-level manipulation, working with binary representations of numbers.

Assignment Operators: Assignment operators (=, +=, -=, *=, /=, %=, **=, //=, &=, |=, ^=, >>=, <<=) are used to assign values to variables, often combined with other operators to perform operations and assign the result back to the variable.

Logical Operators: Logical operators (and, or, not) are used to make decisions based on multiple conditions. They return boolean values based on the logical evaluations.

Membership Operators: Membership operators (in and not in) are used to check whether a value is a member of a sequence (e.g., lists, strings, tuples).

Identity Operators: Identity operators (is and is not) are used to check if two variables refer to the same object in memory.

The article provides clear explanations and examples for each concept, making it a valuable resource for Python learners and programmers. If you have any questions or need further clarification on these concepts, feel free to ask.

Basic Operators in Python With Examples (2024)
Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6740

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.