﻿ Python Programming – Basic Operators - Python: Learn the Basics FAST From Python Programming Experts (2015) ﻿

## Python: Learn the Basics FAST From Python Programming Experts (2015)

### Chapter 4: Python Programming – Basic Operators

An “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 chapter will focus on the fundamental operators used in the Python programming language.

Types of Operator

Python currently supports these types of operators:

Bitwise Operators

Identity Operators

Arithmetic Operators

Logical Operators

Membership Operators

Relational or Comparison Operators

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.

 Operators Definition Examples >> 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) << 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) & 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) ^ 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) ~ 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) | 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:

 Operators Definitions Examples 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) 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:

 Operators Definition Examples // 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 * or Exponents It performs a power (exponential) computation on the operators. X ** Y = 2 to the power of 1 % or Modulus It divides the left operand by the value of the right operand and gives out the remainder. Y % X = 0 + or Addition This operator simply adds the values of the operands. X + Y = 3 - Or Subtraction This operator subtracts the value of the right operand from the left operand. X – Y = 1 * or Multiplication This operator multiples the values of the operands. X * Y = 2 / 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.

 Operators Definition Examples Logical AND (and) The condition is only true if the two operands are true. (X and Y) is true. Logical OR (or) The condition is only true if one of the operands is not equal to zero. (X and Y) is true. 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:

 Operators Definition Examples 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)) 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.

 Operators Definition Examples <= 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. >= 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. <> The condition is only true if the values of the two operands are unequal. (X <> Y) is true. < The condition is only true if the value of the right operand is greater than the value of the left operand. (X < Y) is true. > The condition is only true if the value of the right operand is lesser than the value of the left operand. (X > Y) is not true. == The condition is only true if the values of the operands are equal. (X == Y) is not true. != 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.

 Operators Definition Examples //= 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) **= 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) %= 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) += 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) -= 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) *= 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) /= 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) = The value of the right operand is assigned to the left operand. Z = X + Y assigns the value of X + Y into Z
﻿

﻿