Study Guides/Computer Science/Types of Operators in Python
Study Guide ยท Computer Science

Types of Operators in Python โ€” With Examples

In Python, an operator is a special symbol that performs an operation on one or more values, called operands. For example, in 5 + 3, the symbol '+' is an operator and 5 and 3 are operands. Python provides several types of operators for different kinds of operations โ€” arithmetic, relational (comparison), logical, assignment, bitwise, membership and identity operators. This guide explains each type of operator in Python with examples.

Question (Click to Flip)

What are the types of operators in Python?

Answer

Python has seven main types of operators: (1) arithmetic operators (+, -, *, /, %, **, //), (2) relational/comparison operators (==, !=, >, <, >=, <=), (3) logical operators (and, or, not), (4) assignment operators (=, +=, -=, etc.), (5) bitwise operators (&, |, ^, ~, <<, >>), (6) membership operators (in, not in), and (7) identity operators (is, is not).

Card 1 of 3 free previews

Key Facts

An operator is a symbol that performs an operation on operands.

Python has seven main types of operators.

Arithmetic operators: +, -, *, /, %, **, //.

Relational (comparison) operators give True or False: ==, !=, >, <, >=, <=.

Logical operators: and, or, not.

Membership operators: in, not in (check presence in a sequence).

Identity operators: is, is not (check if two variables refer to the same object).

Arithmetic and Relational Operators

  1. Arithmetic operators: used to perform mathematical calculations.
  • (addition), - (subtraction), * (multiplication), / (division), % (modulus/remainder), ** (exponent/power), // (floor division). Example: 7 % 2 = 1, 2 ** 3 = 8, 7 // 2 = 3.
  1. Relational (comparison) operators: used to compare two values; they give a result of True or False. == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to). Example: 5 > 3 gives True; 5 == 4 gives False.

Logical, Assignment and Bitwise Operators

  1. Logical operators: used to combine conditional statements. and (True if both are true), or (True if at least one is true), not (reverses the result). Example: (5 > 3) and (2 < 4) gives True.

  2. Assignment operators: used to assign values to variables. = (assign), += , -= , *= , /= , %= , **= , //= (compound assignment). Example: x = 5; x += 3 makes x = 8.

  3. Bitwise operators: work on the binary (bit) form of numbers. & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift). Example: 5 & 3 = 1.

Membership and Identity Operators

  1. Membership operators: check whether a value is present in a sequence (like a list, string or tuple). in (True if the value is found), not in (True if the value is not found). Example: 'a' in 'apple' gives True; 5 not in [1, 2, 3] gives True.

  2. Identity operators: check whether two variables refer to the same object in memory. is (True if both refer to the same object), is not (True if they do not). Example: if x = [1,2] and y = x, then x is y gives True.

So Python has seven main types of operators: arithmetic, relational, logical, assignment, bitwise, membership and identity.

Questions and Answers

What are the types of operators in Python?+

Python has seven main types of operators: (1) arithmetic operators (+, -, *, /, %, **, //), (2) relational/comparison operators (==, !=, >, <, >=, <=), (3) logical operators (and, or, not), (4) assignment operators (=, +=, -=, etc.), (5) bitwise operators (&, |, ^, ~, <<, >>), (6) membership operators (in, not in), and (7) identity operators (is, is not).

What are arithmetic operators in Python?+

Arithmetic operators perform mathematical calculations. They are + (addition), - (subtraction), * (multiplication), / (division), % (modulus/remainder), ** (exponent/power) and // (floor division). For example, 7 % 2 gives 1 and 2 ** 3 gives 8.

What is the difference between membership and identity operators?+

Membership operators (in, not in) check whether a value exists in a sequence such as a list or string โ€” for example, 'a' in 'apple' is True. Identity operators (is, is not) check whether two variables refer to the same object in memory โ€” for example, if y = x, then x is y is True.

What are logical operators in Python?+

Logical operators are used to combine conditional (True/False) statements. They are 'and' (True only if both conditions are true), 'or' (True if at least one condition is true), and 'not' (which reverses the result). For example, (5 > 3) and (2 < 4) gives True.

More in Computer Science

Study Smarter with Shinyu.ai

Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast โ€” free, no signup required.