Home » How - To / Tutorial » Programming » Basic Operators in Python Programming

Basic Operators in Python Programming

R-vs-Phyton-Which-one-is-better-2 Basic Operators in Python ProgrammingAn “operator” is defined as the element that can modify an operand’s value. In this example: 3 + 6 = 9, the operands are 3 and 6 while the + symbol is called the operator.

This Article will focus on the fundamental operators used in the Python programming language.

Types of Operator

Python currently supports these types of operators:

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

We will now discuss each operators:

Bitwise Operators

These operators execute bit by bit operation. If we will assume that x has a value of 60 and y has a value of 13, we will have these binary information:

  • X = 0011 1100
  • Y= 0000 1101
  • X and Y = 0000 1100
  • X | Y = 0011 1101
  • ~ X = 1100 0011
  • X * Y = 0011 0001

We will now use these binary data to show the Bitwise operations used in the Python programming language.

Number Operators Definition Examples
1 >> Binary Right Shift The value of the left operand is moved to the right based on the number of bits determined by operand on the right. X >> = 15 (which is 0000 1111)
2 << Binary Left Shift The value of the left operand is moved to the left based on the number identified by the operand on the right. X << 240 (which is 1111 0000)
3 & Binary AND This operator copies and pastes a bit to the end result if it is present in both operands. (X and Y) which is 0000 1100)
4 ^ Binary XOR This operator duplicates a bit if it is present in one operand but not on the other (X ^ Y) = 49 (which is 0011 0001)
5 ~ Complement of Binary Ones This operator is unary and it can flip bits. (~ X) = 61 (which is 1100 0011 in complement of 2’s form because of a marked binary digit)
6 | Binary OR This operator duplicates a bit if it is present in one of the operands. (X | Y) 61 (which is 0011 1101)

Identity Operators

These operators compare the locations of two elements. We will talk about two identity operators here:

Number Operators Definition Examples
1 Is not Its value becomes false if the operands on either side of the basic operator is pointing to a single element. Y is not X – “is not” came from 1 if id (X) is not equal to id (Y)
2 Is Its value becomes true if the operands on either side of the basic operator is pointing to a single element. X is Y – “is” came from 1 if id (X) is equal to id (Y)

Arithmetic Operators

For these operators, let’s assume that variable X has the value of 1 and variable Y has the value of 2. Then:

Number Operators Definition Examples
1 // or Floor Division It divides the operands where the resulting value is the quotient in which the numbers past the decimal point are deleted. 10 // 2 equals 5 and 10.0 // 2.0 = 5.0
2 * or Exponents It performs a power (exponential) computation on the operators. X ** Y = 2 to the power of 1
3 % or Modulus It divides the left operand by the value of the right operand and gives out the remainder. Y % X = 0
4 + or Addition This operator simply adds the values of the operands. X + Y = 3
5 - Or Subtraction This operator subtracts the value of the right operand from the left operand. X – Y = 1
6 * or Multiplication This operator multiples the values of the operands. X * Y = 2
7 / or Division It the divides the value of the left operand by the left operand. Y / X = 2

Logical Operators

These are the logical operators used in the Python programming language. We will assume that variable X is equal to 1 and variable Y is equal to 2.

Number Operators Definition Examples
1 Logical AND (and) The condition is only true if the two operands are true. (X and Y) is true.
2 Logical OR (or) The condition is only true if one of the operands is not equal to zero. (X and Y) is true.
3 Logical NOT (not) This operator is used to reverse an operand’s logical status. Not (X and Y) is false.

Membership Operators

These operators are used in the Python language to check for “membership” in a code sequence (e.g. lists, tuples, strings, etc.) Currently, Python only supports 2 membership operators. These are:

Number Operators Definition Examples
1 Is This is evaluated as true if the operands on either side of the operator are pointing to a single element. X is Y. “Is” is a result of (1 if id (X) is equal to id (Y))
2 Is not This is evaluated as false if the operands on either side of the operator are pointing to a single element. X is not Y. “Is not” is a result of (1 if id (X) is not equal to id (Y))

Relational or Comparison Operators

These operators are used to compare the values of the operands and determine the relation present among them. Let us assume that variable X is equal to 1 and that variable Y is equal to 2.

Number Operators Definition Examples
1 <= The condition is only true if the left operand’s value is less than or equivalent to the right operand’s value. (X <= Y) is true.
2 >= The condition is only true if the left operand’s value is great than or equal to the right operand’s value. (Y >= X) is true.
3 <> The condition is only true if the values of the two operands are unequal. (X <> Y) is true.
4 < The condition is only true if the value of the right operand is greater than the value of the left operand. (X > Y) is not true.
6 == The condition is only true if the values of the operands are equal. (X == Y) is not true.
7 != The condition is only true if the values of the operands are not equal. This one is similar to <> above.

Assignment Operators

We will just assume that the variable X is equal to 1, variable Y is equal to 2 and variable Z is unknown.

Number Operators Definition Examples
1 //= or Floor Division This one performs a floor division on the operators and assigns a value of the left hand operand. (Z //= X) is equal to (Z = Z // X)
2 **= or Exponent AND This one performs a power (exponential) computation on the operators and assigns the value of the left hand operand. (Z **= X) is equal to (Z = Z ** X)
3 %= or Modulus AND A modulus is taken from two operands and assigns the value of the left operand. (Z %= X) is equal to (Z = Z %=X)
4 += or Addition AND The value of the right hand operand is added to the left hand operand and sets the sum as the value of the left hand operand. (Z += X) is equal to (Z = Z + X)
5 -= or Subtraction AND The value of the right operand is deducted from the left operand and sets the difference as the left operand’s value. (Z -+ X) is equal to (Z = Z -X)
6 *= or Multiplication AND The value of the left operand is multiplied with the value of the right operand and sets the product as the left operand’s value. (Z *= X) is equal to (Z = Z * X)
7 /= or Division AND The value of the left hand operand is divided using the value of the right hand operand and assigns the result as the left operand’s new value. (Z /= X) is equal to (Z = Z / XZ /= X) is equal to (Z = Z / X)
8 = The value of the right operand is assigned to the left operand. Z = X + Y assigns the value of X + Y into Z

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website