[go: up one dir, main page]

0% found this document useful (0 votes)
14 views14 pages

Operators 1

Uploaded by

ramaraju bathula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views14 pages

Operators 1

Uploaded by

ramaraju bathula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

OPERATORS :

 Operator is a symbol that performs certain operations.

 Python provides the following set of operators

1) Arithmetic Operators

2) Relational Operators OR Comparison Operators

3) Logical operators

4) Bitwise oeprators

5) Assignment operators

6) Special operators

1) Arithmetic Operators:
1) +  Addition

2) –  Subtraction

3) *  Multiplication

4) /  Division Operator

5) %  Modulo Operator

6) //  Floor Division Operator

7) **  Exponent Operator OR Power Operator

Eg: test.py

1) a=10

2) b=2

3) print('a+b=',a+b)

4) print('a-b=',a-b)

5) print('a*b=',a*b)

6) print('a/b=',a/b)

7) print('a//b=',a//b)

8) print('a%b=',a%b)

9) print('a**b=',a**b)
Output: Python test.py OR py test.py

a+b = 12

a-b= 8

a*b= 20

a/b= 5.0

a//b= 5

a%b= 0

a**b= 100.

Eg:

1) a = 10.5

2) b=2

3)

4) a+b= 12.5

5) a-b= 8.5

6) a*b= 21.0

7) a/b= 5.25

8) a//b= 5.0

9) a%b= 0.5

10) a**b= 110.25

Eg:

10/2 -> 5.0

10//2 -> 5

10.0/2 -> 5.0

10.0//2 -> 5.0

Note:

֍ / operator always performs floating point arithmetic. Hence it will always returns float value.

֍ But Floor division (//) can perform both floating point and integral arithmetic. If arguments are int
type then result is int type. If atleast one argument is float type then result is float type.
Note:

֍ We can use +,* operators for str type also.

֍ If we want to use + operator for str type then compulsory both arguments should be str type only
otherwise we will get error.

1) >>> "ram"+10

2) TypeError: must be str, not int

3) >>> "ram"+"10"

4) 'ram10'

֍ If we use * operator for str type then compulsory one argument should be int and other argument
should be str type.
֍ 2*"krishna"

"krishna"*2

2.5*"krishna" -> TypeError: can't multiply sequence by non-int of type 'float'

"krishna"*"krishna" -> TypeError: can't multiply sequence by non-int of type 'str'

֍ + -> String Concatenation Operator

֍ * -> String Multiplication Operator

Note:

For any number x, x/0 and x%0 always raises "ZeroDivisionError"

10/0

10.0/0

2) Relational Operators: >, >=, <=


1) a=10

2) b=20

3) print("a > b is ",a>b)

4) print("a >= b is ",a>=b)

5) print("a < b is ",a<=b)

6) print("a <= b is ",a<=b)

7)
8) a > b is False

9) a >= b is False

10) a < b is True

11) a <= b is True

We can apply relational operators for str types also.

Eg 2:

1) a="durga"

2) b="durga"

3) print("a > b is ",a>b)

4) print("a >= b is ",a>=b)

5) print("a < b is ",a<=b)

6) print("a <= b is ",a<=b)

7)

8) a > b is False

9) a >= b is True

10) a < b is False

11) a <= b is True.

Eg:

1) print(True>True) False

2) print(True>=True) True

3) print(10 >True) True

4) print(False > True) False

5)

6) print(10>'durga')

7) TypeError: '>' not supported between instances of 'int' and 'str'

Eg:

1) a=10
2) b=20

3) if(a>b):

4) print("a is greater than b")

5) else:

6) print("a is not greater than b")

Output: a is not greater than b

Note:

Chaining of relational operators is possible. In the chaining, if all comparisons returns True then only
result is True. If atleast one comparison returns False then the result is False

1) 10<20 -> True


2) 10<20<30 -> True
3) 10<20<30<40-> True
4) 10<20<30<40>50 -> False.

3) Equality Operators: ==, !=


We can apply these operators for any type even for incompatible types also.

1) >>> 10==20

2) False

3) >>> 10!= 20

4) True

5) >>> 10==True

6) False

7) >>> False==False

8) True

9) >>> "raju"=="raju"

10) True

11) >>> 10=="raju"

12) False

Note: Chaining concept is applicable for equality operators. If atleast one comparison returns False
then the result is False. Otherwise the result is True.
1) >>> 10==20==30==40

2) False

3) >>> 10==10==10==10

4) True

4) Logical Operators: and, or, not


We can apply for all types.

*For boolean Types Behaviour:

and -> If both arguments are True then only result is True

or -> If atleast one arugemnt is True then result is True

not ->Complement

True and False -> False

True or False -> True

not False ->True

*For non-boolean Types Behaviour:


0 means False

non-zero means True

empty string is always treated as False

x and y:

If x is evaluates to false return x otherwise return y

Eg: 10 and 20

0 and 20 If first argument is zero then result is zero otherwise result is y

x or y:

If x evaluates to True then result is x otherwise result is y

10 or 20 ->10

0 or 20 ->20.

not x:

If x is evalutates to False then result is True otherwise False


not 10 ->False

not 0 ->True

Eg:

1) "durga" and "durgasoft" ==>durgasoft

2) "" and "durga" ==>""

3) "durga" and "" ==>""

4) "" or "durga" ==>"durga"

5) "durga" or ""==>"durga"

6) not ""==>True

7) not "durga" ==>False.

5) Bitwise Operators:
֍ We can apply these operators bitwise.

֍ These operators are applicable only for int and boolean types.

֍ By mistake if we are trying to apply for any other type then we will get Error.

֍ &, |, ^, ~, <>

֍ print(4&5) ->Valid

֍ print(10.5 & 5.6)

->TypeError: unsupported operand type(s) for &: 'float' and 'float'

֍ print(True & True) ->Valid

֍ & -> If both bits are 1 then only result is 1 otherwise result is 0

֍ |-> If atleast one bit is 1 then result is 1 otherwise result is 0

֍ ^-> If bits are different then only result is 1 otherwise result is 0

֍ ~ -> bitwise complement operator

֍ 1 ->0 & 0 ->1

֍ << ->Bitwise Left Shift

֍ >> ->Bitwise Right Shift

֍ print(4&5) -> 4
֍ print(4|5) ->5

֍ print(4^5) ->1

You might also like