# Python operators and expressions

In programming, Operators are special symbols (+, -, /) that are used for computations. The values that an operator acts on are called operands, operands could be variables or direct values. An expression is a combination of operator and operands.

## Objectives

• Python Arithmetic operators and expressions

• Python Assignment operators and expressions

• Python Comparison operators and expressions

• Python Logical operators and expressions

• Python Identity operators and expressions

• Python Membership operators and expressions

## Python Arithmetic operators and expressions

Used for mathematical calculation

Name Operator Syntax Example Output

+

a + b

``````a = 10
b = 11
result = a + b
print(result)``````

21

Subtraction

-

a - b

``````a = 12
b = 11
result = a - b
print(result)``````

1

Multiplication

*

a * b

``````a = 12
b = 11
result = a * b
print(result)``````

132

Division

/

a / b

``````a = 12
b = 11
result = a / b
print(result)``````

1.0909090909090908

Modulus

%

a % b

``````a = 12
b = 11
result = a % b
print(result)``````

1

Exponentiation

**

a ** b

``````a = 2
b = 3
result = a ** b
print(result)  # same as 2*2*2``````

8

Floor division

//

a // b

``````a = 9
b = 2
result = a // b
print(result)``````

4

## Python Assignment operators and expressions

Assignment operator basically responsible for assign value into variable. For example

``````a = 100
print(a)``````
Here
• We assigned 100 into the variable

• Then print will print 100 because, earlier we assigned 100 there

Operator Short form Actual form Description

+=

a += 3

a = a + 3

Add plus 3 with value of a and again assigned to a

-=

a -= 3

a = a - 3

Subtract 3 with value of a and again assigned to a

*=

a *= 3

a = a * 3

Multiply 3 with value of a and again assigned to a

/=

a /= 3

a = a / 3

Divide 3 with value of a and again assigned to a

%=

a %= 3

a = a % 3

Modulus 3 with value of a and again assigned to a

//=

a //= 3

a = a // 3

Divides 3 with value of a and again assigned floor value to a

**=

a **= 3

a = a ** 3

Exponentiation 3 with value of a and again assigned to a

## Python Comparison operators and expressions

Responsible for make comparison of 2 operand, means variable or value

Name Operator Example Output

Equal

==

``````a = 12
b = 11
result = a == b
print(result)``````

False

Not equal

!=

``````a = 12
b = 11
result = a != b
print(result)``````

True

Greater than

>

``````a = 12
b = 11
result = a > b
print(result)``````

True

Less than

<

``````a = 12
b = 11
result = a < b
print(result)``````

False

Greater than or equal to

>=

``````a = 12
b = 11
result = a >= b
print(result)``````

True

Less than or equal to

< =

``````a = 12
b = 11
result = a <= b
print(result)``````

False

## Python Logical operators and expressions

Logical operators are used to combine conditional statements

Name Operator Description Example Output

And

and

Returns True if both statements are true

``````a = 12
result = a < 5 and a < 10
print(result)``````

False

Or

or

Returns True if one of the statements is true

``````a = 12
result = a < 5 or a < 10
print(result)``````

False

Not

not

Reverse the result, returns False if the result is true

``````a = 12
result = not(a < 5 or a < 10)
print(result)``````

True

## Python Identity operators and expressions

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location

Name Operator Description Example Output

Is

is

Returns True if both variables are the same object

``````a = 12
b = 13
result = a is b
print(result)``````

False

Is Not

is not

Returns True if both variables are not the same object

``````a = 12
b = 13
result = a is not b
print(result)``````

True

## Python Membership operators and expressions

Name Operator Description Example Output

In

in

Returns True if a sequence with the specified value is present in the object

``````dictionary = {"name": "Touhid", "sex": "Male"}
result = "name" in dictionary
print(result)``````

True

Not in

not in

Returns True if a sequence with the specified value is not present in the object

``````dictionary = {"name": "Touhid", "sex": "Male"}
result = "name" not in dictionary
print(result)``````

False