Operators in Python (Books)

Operators in Python Chapter 4

 

Unary Operators:  If an operators acts on a single variable, it is called unary operators.


Binary Operators: If an operator acts on two variables, it is called binary operators.


Ternary Operators: If an operator acts on three variables, then it is called ternary operators.


We can classify the operators depending upon their nature, as shown 


1. Arithmetic operators:

These operators are used to perform basic arithmetic operations like addition,

subtraction,division etc.

 

There are seven arithmetic operators available in python.

These operators act on two operands, they are called binary operators also.

 

Let's assume,

a = 12 and b = 4   

and see the effect of various arithmetic operators.


+ (plus) ----> this is addition operators. Add two values.

a = 12 and b = 4 

a + b =  16

 

- (minus) ----> subtraction operators. Subtract one value from another.

a = 12 and b = 4

a - b = 8


* (multiplication)----> multiplication operator. Multiplies values on either side of the operator.

a = 12 and b = 4 

a * b = 48

 

/ (division)----> division operator. Divides left operand by the right operand.

a = 12 and b = 4  

a / b = 3.0 


% (modulus)----> modulus operator. Gives remainder of division.

a = 12 and b = 4  

a % b = 0 (zero remainder)


**(exponent)----> exponent operator. Calculates exponential power value.

a**b gives the value of a to the power of b.

a = 12 and b = 4  

a**b =  20736


// (integer division)----> integer division. This is also called floor division.

performs division and gives only integer quotient.

a = 12 and b = 4 

a // b = 3

 


2. Assignment Operators:

These operators are useful to store the right side value into a left side variable.

    They can also be used to perform simple arithmetic operations like addition,
     subtraction etc., and then store the result into a variable.  

 

Let's assume the value 

x = 20     y = 10    z = 5

= (equal) ----> Assignment operator. stores right side value into left side variable.

i.e. 

z = x + y
(x + y is stored into z)


+=,    z+=x ----->  Addition assignment operator. Adds right operand to the left operand and stores the result into left operand.

i.e. z = z + x

 

-=,     z -= x -----> Subtraction assignment operator. subtracts right operand from left operand and store the result into left operand.

i.e. z = z - x


*=,    z*=x ----> multiplication assignment operator. Multiplies right operand with left operand and stores the result into left operand.

i.e. z = z * x


/=,    z/=x----> division assignment operator. divides left operand with right operand and stores the result into left operand.

i.e. z = z / x


%=,    z%=x -----> Modulus assignment operator. Divides left operand with right operand and stores the remainder into left operand.

i.e.  z = z % x


**=,     z**=x-----> Exponentiation assignment operator. perform power value

and then stores the result into left operand.

i.e. z = z**x


//=,    z//=x----> Floor division assignment operator. perform floor division and then stores the result  into left operand.

i.e. z = z // x.

 

 

 

It is possible to assign the same value to two variables in the same statement as:

a=b=2
print(a,b) # it will display 2 2


 
 ## It is possible to assign the same value to two variables in 
    the same statement as:

a = b = 2
print(a,b)
 
    output: 2 2




Another example is where we can assign different values to two variables as:

 
    x = 9
y = 5
print(x,y)
 
    output: 9 5


 

The same can be done using the following statement:


 
 ## The same can be done using the following statement:

x , y = 9,5
print(x,y)
 
    output: 9 5  
     
 


   




3. Unary Minus Operators:

The unary minus operator is denoted by the symbols minus (-). When this operator is used before a variable, its value is negated.

That means if the variable value is positive, it will be converted into negative and vice versa.    

For example:

 

## Unary Minus Operators:

n = 10
print(-n)
     
    output: -10  
    


 
 ## Unary Minus Operators:
num = -10
num = -num
print(num)
 
    output: 10  


 

 

 

 

4. Relational Operators:

Relational operators are used to compare two quantities. We can understand whether two values are same or which one is bigger or which one is lesser etc. using this operator .

These operators will result in True or False depending on the values compared.

Let's assume

    a = 1 and b = 2

 Relational operators

 
    a = 1
b = 2

print(a >b)
# output: False

print(a>=b)
# output: False

print(a<b)
# output: True

print(a<=b)
# output: True

print(a==b)
# output: False

print(a!=b)
# output: True


 

Relational operators
This is a condition


    a = 1
    b = 2
 
if a > b:
print("yes")
else:
print("no")
     
    output: no


 

Relational operators


x = 15
print(10<x<20)
# # output: True

print(10>=x<20)
# # output: False

print(10<x>20)
# # output: False



If any comparison yields False, then we get false as the final result:

 
 ## Relational operators

print(1<2<3<4)
# # # output: True

print(1<2>3<4)
# # output: False

print(4>2>=2>1)
# # # output: True





5. Logical Operators: (and/ or/ not)

Logical operators are useful to construct compound conditions. A compound condition is a combination of more than one simple condition. Each of the simple condition is evaluated to True or False and then the decision is taken to know whether the total condition is True or False.


Let's take x = 1 and y = 2

and,    x and y :     and operator. If x false, it returns x, otherwise it returns y.

or,    x or y:     or operator. if x is false, it returns y. otherwise it returns x. 

not,    not x :    not operator. if x is false. it returns true. otherwise true.

 
     x = 300
y = 500

print(x and y)
# output: 500

print(x or y)
# output: 300

print(not x)
# # # output: False



and operator

x = 1
y = 2
z = 3

if x<y and y < z:
print("yes")
else:
print("No")
 
    output: yes



or operator


if x > y or y < z:
print("yes")
else:
print("No")
 
    output: yes  

 

 

 

 

6. Boolean Operators:

We know that there are two "bool" type literals. They are True or False.

Boolean operators act upon "bool" type literals and they provide "bool"

type output. It means result provided by boolean operators will be again either True or False.

There are three Boolean operators as mentioned.

 

Let's take 

x = True 

y = False

 

and-----> (x and y): Boolean and operator. If both are true then it return true.

otherwise False.

 

or-------> (not x) : Boolean or Operator. If either x or y true, then it returns true.

otherwise it False.

 

not -------> (not x): Boolean not operator. If x is true, it returns False. Otherwise True.

 

example:

 
 a = True
b = False

print(a and a)

print(a and b)

print(a or b)

print(not b)
 
 
    output: 
    True
    False
    True
    True



 

 

7. Bitwise Operators:

 

 

 

 

 

 

 

 

 

 

 

 

 

8. Membership Operators:

9. Identity Operators:

 

 





إرسال تعليق (0)
أحدث أقدم