# Basic Operators

Lesson Progress
0% Complete

Operators are the constructs that can manipulate the value of operands. For example: Given an expression 3+2 = 5; here 3 and 2 are operands and + is the operator. Python supports the following operators which we will look at in-depth:

• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators

## Arithmetic Operators

Python has 7 arithmetic operations that perform mathematical operations. They include addition(+), subtraction(-) , division( /), modulo/remainder (% ) and exponents (**) Here are some examples you should try out in your interactive prompt:

```print "5 + 2 = ",  5+2  # addition
print "5 - 2 = ", 5-2   # subtraction
print "5 * 2 = ", 5*2   # multiplication
print "5 / 2.0 = ", 5/2.0   # division
print "5 // 2 = ", 5//2  # floor division which truncates results
print "5 ** 2 = ", 5**2 # power of a numer /exponents
print "5 % 2 = ", 5%2  # modulus/remainder```

Note: Division (/) in Python2 returns a truncated decimal place and returns integer value hence need to add decimal point .0 to get the real division. This has been fixed in Python3.

## Assignment Operators

Comparison (Relational) Operators These operators compare the values on either side of them and decide the relation among them. They are also called Relational operators. Here are some examples you should try out:

```print "5 == 2 is  ", 5 == 2  # returns True if the two values are equal, false otherwise
print "5 !=  2 is ", 5 != 2  # returns True if the two value are not equal, false otherwise
print "5 > 2 is  ", 5 > 2    # returns True if left value is greater than right value, false otherwise
print "5 < 2 is  ", 5 < 2   # returns True if left value is less than right value, false otherwise
print "5<= 2 is  ", 5 <= 2  # returns True if left value is less than or equal to right value, false otherwise
print "5 >=  2 is ", 5 >=  2 # returns True if left value is greater than or equal to right value, false otherwise```

## Logical Operators

The following are the logical operators in Python. AND – returns true if both values are true   OR – returns True if either of the values are true NOT – Used to reverse the logical state of its operand  Example of logical operations

```# Assign variable a to True and variable b to false
a = True
b = True

print a and b # Returns False
print a or b  # Returns True
print not(a)  # Retuns False```

## Assignment Operators

Assignment operators are used to assign values to the variables.

Operator Description Example
= Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c
+= Add AND It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a
-= Subtract AND It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c – a
*= Multiply AND It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a
/= Divide AND It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a
%= Modulus AND It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a
**= Exponent AND Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a
//= Floor Division It performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a

## Membership Operators

in and not in are the membership operators; used to test whether a value or variable is in a sequence.

```in   True if value is found in the sequence

```a = "goat"