4 – Operators and Functions

OPERATORS In Python

Operators supported by Python Language are listed below.
1. Arithmetic Operators
2. Assignment Operators
3. Comparision Operators
4. Bitwise Operators
5. Logical Operators
6. Membership Operators
7. Identity Operators

ARITHMETIC OPERATORS

Arithmetic operators are Addition(+), Subtraction(-), Multiplication(*), Division(/), Modulus(%) and Power(**).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>>#Declare variables.
>>> a=20
>>> b=10
>>>#Addition
>>> a+b
30
>>>#Subtraction
>>> a-b
10
>>>#Multiplication
>>> a*b
200
>>>#Division
>>> a/b
2
>>>#Power
>>> a=2
>>> b=4
>>> a**b
16
>>>#Modulus
>>> a%b
2
>>>

COMPARISION OPERATORS

Comparision or Relational Operators are are Equal to(==), Not equal to(!=, <>), Lesser than(<), Greater than(>).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> a=10
>>> b=10
>>>#Equal to
>>> a==b
True
>>>#Not Equal to
>>> a!=b
False
>>>#Greater than
>>> a>b
False
>>>#Lesser than
>>> a<b
False
>>>#Lesser than or Equal to
>>> a<=b
True
#Greater than or Equal to 
>>> a>=b
True
>>>

 ASSIGNMENT OPERATORS

Assignment operators are =, +=, -=,  /=, *=, %=, **=.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
>>> a=10
>>> a+=10
>>> a
20
 
>>> a-=10
>>> a
10
 
>>> a*=10
>>> a
100
 
>>> a/=10
>>> a
10
 
>>> a%=2
>>> a
0
 
>>> a=2
>>> a**=2
>>> a
4
>>>

BITWISE OPERATORS

Bitwise operators are Bitwise OR(|), Bitwise AND(&), Bitwise XOR(^), Binary One’s Complement(~), Binary Left Shift(<<), Binary Right Shift(>>).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
>>>#Bitwise AND
>>> a=24
>>> bin(a)
'0b11000'
 
>>> b=53
>>> bin(b)
'0b110101'
 
>>> a&b
16
>>> bin(a&b)
'0b10000'
>>> #11000&110101=10000
 
>>>#Bitwise OR
>>> a|b
61
>>> bin(a|b)
'0b111101'
>>> #11000&110101=111101
 
>>>#Bitwise XOR
>>> a^b
45
>>> bin(a^b)
'0b101101'
>>> #11000&110101=101101
 
>>>#One's Complement
>>> ~a
-25
>>> bin(~a)
'-0b11001'
>>> #~11000=-11001
#Left Shift
>>> a<<2
96
>>> bin(a<<2)
'0b1100000'
>>> #11000<<2=1100000
 
>>>#Right Shift
>>> a>>2
6
>>> bin(a>>2)
'0b110'
>>> #11000>>2=110

LOGICAL OPERATORS

Logical operators Logical AND, Logical OR, Logical NOT.

1
2
3
4
5
6
7
8
9
10
11
>>> a=10
>>> b=0
>>> (a and b)
0
>>> (a or b)
10
>>> not a
False
>>> not b
True
>>>

MEMBERSHIP OPERATORS

Membership operators are in, not in.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>#in
>>> a=[1,2,3,4,5]
>>> 6 in a
False
>>> 4 in a
True
>>> for i in a:
	print i
 
1
2
3
4
5
 
>>>#not in
>>> 6 not in a
True
>>> 3 not in a
False
>>>

IDENTITY OPERATORS

Identity Operators are is, is not.

1
2
3
4
5
6
7
>>> a=5
>>> b=2
>>> a is b
False
>>> a is not b
True
>>>

FUNCTIONS

Functions are the group of statements to perform a specific taskThe keyword def is used to define a function in python.

1
2
3
4
5
6
7
8
9
10
11
#Function Definition
def addition(a,b):
    c=a+b
    return c;
 
#Get input from user
x=input("Enter var 1 ")
y=input("Enter var 2 ")
 
#Function Call
print addition(x,y)

OUTPUT

>>> 
Enter var 1 10
Enter var 2 20
30
>>>
THANK YOU

 

SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.