❮ Previous Next ❯

Python Operators

We know that an operator is a symbol which performs an operation. An operator acts upon which data items is known as operands.
python Operator
Python supports the following operators which are as follows:

Python Arithmetic Operators


Arithmetic operators are specifically used with numeric values to perform mathematical operations operations like addition, subtraction, multiplication, division.
python Operator1

# Examples of Arithmetic Operator
x = 5

y = 3

# Addition of numbers

add = x + y

# Subtraction of numbers

sub = x - y

# Multiplication of number

mul = x * y

# Division(float) of number

div1 = x / y

# Division(floor) of number

div2 = x // y

# Modulo of both number

mod = x % y


# print results
print(add)

print(sub)

print(mul)

print(div1)

print(div2)

print(mod)


Output

8
2
15
1.6666666666666667
1
2


Python Assignment Operators

Assignment operators are used to assign values to variables:


Example

>>> x=2

>>> x=x+2

>>> x

4

>>> x+=2

>>> x

6

>>> x*=3

>>> x

18

>>> a,b=10,20

>>> a

10

>>> b

20

>>> n=10

>>> n

10

>>> -n

-10

Python Comparison(Relational) Operators

Comparison(Relational) operators are used to compare two values: Here in this context the output value is a Boolean value(Either True or False).


python Operator2

#Example

>>> a=10

>>> b=20

>>> a<b

True

>>> a>b

False

>>> a==b

False

>>> a<=b

True

>>> a>=b

False

>>> a!=b

True

Python Logical Operators

python Operator3

#Example

>>> a<b and b<10

False

>>> a<4 or b<20

False

>>> a and b

20

>>> a or b

10

>>> not a

False

>>> a= not a

>>> a

False

Python Identity Operators

Identity operators are used to compare the objects.


python Operator4

# Examples

a1 = 10

b1 = 10

a2 = 'SilanTechnology'

b2 = 'SilanTechnology'

a3 = [10,20,30]

b3 = [10,20,30]

print(a1 is not b1)

print(a2 is b2)

# Output is False, since lists are mutable.

print(a3 is b3)

output:

False
True
False

Python Membership Operators

# Examples of Membership operator

p = 'SILAN Technology'

q = {10:'x',20:'y'}


print('S' in p)

print('java' not in p)

print('Tech' not in p)

print(3 in q)

print('y' in q)
Output: True

True

False

False

Python Bitwise Operators

python Operator5

# Examples
x = 5
y = 3

# Print bitwise AND operation
print(x & y)

# Print bitwise OR operation
print(x | y)

# print bitwise XOR operation
print(x ^ y)

# print bitwise right shift operation
print(x >> 2)

# print bitwise left shift operation
print(x << 2)

Output

1
7
6
1
20

❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved