python and operator

 python And operator:

  • The python And operator is one of the logical operators.
  • In And operator, if both the operands are true then it returns true, otherwise false.
  • And operator is applicable for boolean type and non-boolean type.
  • For example: 1 And  " a"  then the result is "a".  
  •  It is represented by the  "&" symbol.
  •  It is written as  x &y
syntax:(a&b).

# write a python program using And operator.

  1. x=23
  2. y=44
  3. print(x>10 and y>10)
  4. print(x<40 and y<40)
  5. print(x&y)
  output:   true
                false
                 4

Types of operators in python

 Operators: 

  • Operators are nothing but the special symbols used in between the operands.
  • In python, we use the operators to perform some arithmetic, logic, and some more operations.
  • These operations are performed on the variables, values, arguments nothing but the operands.
  • We can perform operations on one or more operands.
  • In python, we have six operators.
  • Arithmetic( to add),comparison(to compare), logical( perform logical operations), bitwise(cal bitwise operations), assignment(assign values), special(check address and content ).

1.Arithmetic operator:

  • It is also called a math operator and it applicable for all int and float types values.
write a python program using the math operators/arithmetic operators.



2. comparison operator:

  • It compares and shows the relation of variables.
write a python code using comparison operators.




3. Logical operator: 

  •  This operator performs the logical operations on variables.
  • There are 3 logical operators.
write a python program using logical operators.



4. Bitwise operator:

  • It is used to perform bit-wise calculations.
  • We have 6 operators and applied to int and bool.
write a python code using bitwise operators.



5. Assignment operator:

  • It assigns the values to variables.
  • we have 12 operators in it.
write a python program using the assignment operators.



6.Special operator:

  • In, not in and is, is not are special operators.
  • Membership operators and identity operators are 2 types in it.
  • Membership operator is used to giving member is present in the list or not.
  • eg: list=[1,2,3]
  • pint(1 is a list )
  • output: true.
write a python program using the special operator.



python math operators

Math operators:

  • In python, we have various arithmetic operators.
  • These are called math operators because they perform different types of mathematical operations.
  • Here  we have seven types of arithmetic operators:
  1. +     addition
  2. -      subtraction.
  3. /       division.
  4. %     modulus.
  5. *       multiplication.
  6. //       floor division.
  7. **      exponential.
1.Addition(+): It is used to add two or more operands.
It is represented by the "+". symbol.
syntax:(a+b).

2.subtraction(-): It subtracts the value of the  one operand to other operands 
It is represented by the symbol "-".
syntax:(a-b).

3.Division(/): It divides one operand with another operand and gives the remainder. The result is always in float type only.
It is represented by the symbol "/'.
syntax:(a/b).

4.modulus(%): It is the remainder of the operands.
It is represented by the symbol "%".
syntax:(a%b).

5.Multiplication(*): It multiply the two or more operands.
It is represented by the symbol "*".
syntax:(a*b).

6.floor division(//): It divides the operands and get the result in the whole number only.
It is represented by the symbol "//'.
syntax:(a//b).

7.exponential(**): It is used to calculate the power of the values.
It is represented by the symbol "**'.
syntax:(a**b).


Example: write a python program for integers using math operators.

  1. x=3
  2. y=4
  3. print(x+y)
  4. print(x-y)
  5. print(x/y)
  6. print(x%y)
  7. print(x*y)
  8. print(x//y)
  9. print(x**y)
output:     7
               -1
               0.75
                3
               12
               0
               81




python division operator

  • The division operator in python is used to perform the division operations.
  • It divides the left-side values with the right-side value and gives the remainder.
  • In other languages, we have only one division operator but in python, we have 2 division operators.
  1. division.
  2. floor division
 1.Division: In division when we divide values we get the result in floating type.
It is denoted by the symbol "/".
 syntax:(x/y).

Example: write a python code for integers using a division operator.

  1. x=20
  2. y=4
  3. print(x/y)
output:5.0



2.floor division(//):  In the floor division when we divide the values we get the whole number only.
It is denoted by the symbol "//".
syntax:(x//y).

Example: write a python program for integer values using floor division.

  1. x=20
  2. y=4
  3. print(x//y)
output: 5















Python modulo operator


In Python, the modulo operator is represented by the percent sign (%). It is used to find the remainder of a division operation between two numbers. 
Here's an example of  modulo operator In Python

x = 7
y = 3
remainder = x % y
print(remainder)

This will print 1 as the remainder of 7 divided by 3 is 1.
modulo operator to check if a number is even or odd:


x = 7
if x % 2 == 0:
    print(x, "is even")
else:
    print(x, "is odd")
This will print "7 is odd" because the remainder of 7 divided by 2 is 1.

The modulo operator can also be used to wrap around a range of values. For example, you can use it to ensure that an index is always within the bounds of an array, even if the index is negative or greater than the size of the array:


index = -1
size = 5
correct_index = index % size
print(correct_index)

This will print 4 as the correct index, because -1 % 5 = -1, but we want the index to be between 0 and 4.

It's worth noting that, the modulo operation is defined as the remainder of the division of one number by another, i.e x % y = x - y * floor(x/y)
where floor is the floor division operator, that returns the quotient of the division and also discards any remainder.

  • Modulo operator:  The modulo is defined as "the remainder of the two arguments ".
  • It is denoted by the symbol   "%".
  • When we divide any number with 0 i.e;    x%0 the result is zero division error.
  • We can use int, float, and double values also.
  • Those int, float, double may be either positive or negative.
syntax:(x%y)
x is the first argument and y is the second argument.

#1.write a python program using the modulo operator.

  1. x=10.0
  2. y=2.0
  3. print(x%y)
output: 0.0


#2.write a python program for a value divides with 0 using the modulo operator.
  1. x=10.0
  2. y=0
  3. print(x%y)
output: zero division error


Python bitwise operators

  • Bitwise operator:  In python, the bitwise operator is used to performing the bitwise calculations on integers.
  • It can apply to boolean type also.
  • It will be converted into binary numbers and bitwise operations in integers first on that binary number.
  • As it performs the bit to bit operations the name is called a bitwise operator.
  • There are six types of bitwise operators in python they are:
  1. &   And.
  2. |      or.
  3. ^      xor.
  4. ~     bitwise not
  5. >>   right shift.
  6. <<   left shift.
1.And(&): If both bits are then it returns true, else false.
It is denoted by the "&" symbol.
syntax: (x&y).

Example: write a python program for integer values using And operator.

  1. x=4
  2. y=5
  3. print(x&y)
output: 4


2. or(|):  If any one of the bits is true then the result is true, else false.
           It is denoted by the symbol "|".
syntax:(x|y).

Example: write a python program for integer values using or. 

  1. x=10
  2. y=5
  3. print(x|y)
output: 15


3. xor(^): when both bits are different returns true, else false.
It is denoted by the "^" symbol.
syntax:(x^y)

Example: write a python code for integers using xor operator.

  1. x=1
  2. 2
  3. print(x^y)
output: 3


4. Bitwise not (~): It is the complement of the bit if the bit is 1 if the result is 0, if the bit is 0 then it result is 1.
It is denoted by the "~" symbol.
syntax:(~x)

Example: write a python code for bool type using bitwise not operator.

  1. x=true
  2. print(~true)
output: -2.



5.Right shift(>>): It shifts one bit right and fills the left side vacant cells with significance values.
It is denoted by ">>".
syntax:(x>>).

Example: Write a python program using the right shift operator.

  1. x=11
  2. y=2
  3. print(x>>y).
output: 2



6.Left shift(<<): It shifts one bit left and the right side vacant cells fill with 0.
It is denoted by the "<<" symbol.
syntax:(x<<)

Example: write a python code for int values using the left shift operator.

  1. x=11
  2. y=2
  3. print(x<<y)
output:44


Select Menu