Wednesday, December 18, 2019

python operator cont....

Assignment operators 

We can use assignment operator to assign value to the variable.
Eg: x = 10

We can combine assignment operator with some other operator to form compound assignment operator.

Following is the list of all possible compound assignment operators in python.

+=
-=
*=
/=
%=
//=
**=
&=
|=
^=
>>=
<<=

Eg:

x=10
x+=20
print(x) —> 30 

Eg:

x=10
x&=5
Print(x) —> 0

Ternary operator or conditional operator 

Syntax x = firstvalue if condition else secondValue

If condition is True then firstValue will be considered else secondValue will be considered.

Eg: 1 

> a,b=10,20
> x=30 If a<b else 40
> print(x) #30 

Eg2:  read two numbers from the keyboard and print minimum value.

> a=int(input(“Enter First Number:”))
> b=int(input(“Enter Second Number:”))
> min=a if a<b else b
> print(“Minimum Value:”,min)

Output 

Enter First Number :10
Enter Second Number:30
Minimum Value:10

Note: Nesting of Ternary Operator is Possible

Q: Program for Minimum of 3 numbers 

> a=int(input(“Enter First Number:”))
> b=int(input(“Enter Second Number:”))
> c=int(input(“Enter Third Number:”))
> min=a if a<b and a<c else b if b<c else c 
> print(“Minimum Value:”,min)

Q: Program for Maximum of 3 numbers 

> a=int(input(“Enter First Number:”))
> b=int(input(“Enter Second Number:”))
> c=int(input(“Enter Third Number:”))
> min=a if a>b and a>c else b if b>c else c 
> print(“Maximum Value:”,max)

Eg: 

> a=int(input(“Enter First Number:”))
> b=int(input(“Enter Second Number:”))
> print(“Both numbers are equal” if a==b else “First Number is less than second Number” if a<b else  “First Number Greater than Second Number”)

Output 

D; \python_classes>py test.py
Enter First Number:10
Enter Second Number:10
Both numbers are equal 

D; \python_classes>py test.py
Enter First Number:10
Enter Second Number:10
First Number is Less than Second Number 

D; \python_classes>py test.py
Enter First Number:10
Enter Second Number:10
First Number is Greater than Second Number 

Special operator 

Python defines the following 2 special operators 

  1. Identity operators 
  2. Membership operators

Identity operators : 

We can use identity operators for address comparison 
There are 2 identify operators are available :  1) is   2) is not 

> r1 is r2, returns True if both r1 and r2 pointing to the same object.
> r1 is not r2 returns True if both r1 and r2 are not pointing to the same object.

Eg:

> a=10
> b=10
> print(a is b) True
> x=True
> y=True
> print(x is y) True 

Eg: 

> a=“demo”
> b=“demo”
> print(id(a))
> print(id(b))
 > print(a is b)

Eg:

> list1=[“one”,”two”,”three”]
> list2=[“one”,”two”,”three”]
> print(id(list1))
> print(id(list2))
> print(list1 is list2) False
> print(list1 is not list2) True
> print(list1 == list2) True 

Note: we can use is operator for address comparison where as == operator for content comparison.

Membership operator 

We can use membership operators to check whether the given object present in the given collection ( it may be String, List, Set, Tuple OR Dict )

In —> Returns True if the given object present in the specified collection.
not in —> Returns True if the given object not present in the specified location.

Eg:

> x=“hello learning Python is very easy”
> print(‘h’ in x) True
> print(‘d’ in x) False 
> print(‘d’ not in x)  True
> print(‘Python’ in x) True 

Operator precedence 

If multiple operator present then which operator will be evaluated first is decided by operator precedence 

Eg:

> print(3+10*2) —> 23 
> print((3+10)*2) —> 26 

The following list describes operator precedence in Python 

  1. () —> parenthesis
  2. ** —> exponential operator 
  3. ~,-  —> Bitwise complement operator, unary minus operator 
  4. *, /, %, // —> mulitplication, division, modulo, floor division 
  5. +, - —> addition , substraction
  6. << , >> —> left and right shift 
  7. & —> Bitwise And 
  8. ^ - Bitwise X-OR
  9. | —> Bitwise OR
  10. >,>=,<,<=,==,!= —> relational or comparison operators 
  11. =, +=,-=,*= —> assignment operator 
  12. is, is not —> identity operator 
  13. in, not-in —> membership operator 
  14. not —> logical not 
  15. and —> logical and 
  16. or —> logical or

Mathematical functions ( math module ):

A module is collection of functions, variables and classes etc 
Math is a module that contains several functions to perform mathematical operations 
If we want to use any module in python, first we have to import that module
Import math 
Once we import a module then we can call any function of that module.

import math 
print(math.sqrt(16))
print(m.pi)

Output :

4.0
3.14444444

We can create alias name by using as keyword  Import math as m  
Once we create alias name, by using that we can access functions and variables of that module.

We can import a particular member of a module explicitly as follows 

Import math as m
print(m.sqrt(16))
print(m.pi)

We can import a particular member of a module explicitly as follows 

from math import sqrt 
from math import sqrt.pl

If we import a member explicitly then it is not required to use module name while accessing.

from math import sqrt.pi
print(sqrt(16))
print(pi)
print NameError: name (math.pi) ‘math’ is not defined.

Important functions of math module 

  1. celi(x)
  2. floor(x)
  3. pow(x,y)
  4. factorial(x)
  5. trunc(x)
  6. gcd(x,y)
  7. sin(x)
  8. cos(x)
  9. tan(x)

Important variables of math module 

pi3.14
e —> 2.71
inf —> infinity 
nan —> not a number 

Q: write a python program to find area of circle  pi*r**2 

from math import pi
r = 16
print(“Area of Circle is :”,pi*r**2)

Output : Area of Circle: 804.2477193 


No comments:

Post a Comment