Contents Operator Operator precedence and Associativity Mathematical Functions
operator An operator is a symbol that performs operation. Operator acts on variables or constants known as operands. If operator acts on single operand then it is called as unary and if acts on two operands then it is called as binary and when acts on three operands it is called as ternary operator. Operators are classified depending upon their nature as..
Classification of operators arithmetic assignment unary minus relational logical Boolean bitwise membership Identity
Arithmetic operator performs basic arithmetic operation like addition, subtraction, multiplication and division. Lets assume a=13 and b=5 operator Meaning Example Result + addition a+b 18 - subtraction a-b 8 * multiplication a*b 65 / division a/b 2.6 % Modulus(remainder) a%b 3 ** Exponent operator a**b 371293 // Integer division a//b 2 TEST1 Writes a python program to perform all arithmetic operations on two variable a=20 and b=6
Priority to arithmetic operators 1.parentheses 2.exponent 3.multiplication,division,modulus and floor division are at equal priority 4.Addition and subtraction 5.Assignment Evaluate the following arithmetic expression d=(1+2)*3**2//2+3
Assignment Operator stores the right side value to left side variable. Short hand assignments operators used to perform arithmetic operations and stores the result to left side variable. X=20, y=10 and z=5 Operator Example Meaning Result = z= x+y Assignment operator z=30 += z+=x (z= z+x ) Shorthand addition assignment operator z=25 -= z-=x (z=z-x) Shorthand subtraction Assignment operator z=-15 *= z*=x z=z*x Shorthand multiplication operator z=100 /= z/=x z=z/x Shorthand division operator Z=0.25 %= z%=x z= z%x Shorthand modulus operator Z=5 **= z**=y z=z**y Shorthand Exponent assignment operator Z=9765625 //= z//=y z=z//y Shorthand division assignment operator
Contd.. TEST2 Write python program to perform arithmetic operation on x=“20” and y=0xad using shorthand assignment operator Guess the output a=b=1 print( a,b ) a=1;b=2 print( a,b ) a,b =1,2 print( a,b ) Note: python does not support increment(++) and decrement operator
Unary Minus Operator The symbol – is used as unary minus operator. This operator is used before a variable, its value is negated. That means if the variable value is positive, it will be converted into negative and vice versa. Example: N=10 print(-N ) output: -10 Num=-10 Num=- Num print(Num ) output: 10
Relational Operators these operators are used for comparison of values. They returns Boolean value True or False . a=1 and b=2 Operator Example Meaning Result > a>b Greater than operator False >= a>=b Greater than or equal to operator False < a<b Less than operator True <= a<=b Less than or equal to True == a==b Equals to operator False != a!=b Not equals operator True
Contd.. Relational operators can be chained. It means a single expression can hold more than one relational operators Guess the output x=15 print(10<x<20) print(10>=x<20) print(1<2<3<4) print(1<2>3<4) print(4>2>=2>1)
Logical operator Logical operators are used to construct compound condition. A compound condition is a combination of more than one simple condition. Each of the simple condition is evaluated to True or False and then the decision is taken to know whether total condition is True or False. a=10,b=20,c=3,d=5 operator Example Meaning Result and a>b and c>d Logical and False or a>b or c<d Logical or True not not a>b Logical not True
Boolean Operators There are three Boolean operators which results True or False. x=True y=False operator Example meaning result and x and y If both are true then it returns true otherwise false False or x or y If either x or y is True then it returns True else False True not not x If x is True it returns False else True. False
Guess the output x=10; y=0 print(x and y) # if x and y both evaluated as true then y is returned true # if x is true and y is false then y is returned print(x or y) print(not x) print(not y) OUTPUT 10 False True
Bitwise operator operates on individual bits ( 0 and 1) of the operands. They can operates on binary number or integers . Complement operator(~) AND operator(&) OR operator(|) XOR operator(^) Left shift operator(<<) Right shift operator(>>)
Contd.. Complement of given number is returned by toggling the bits. Example: x=10 X=10=0000 1010 print(~ x,bin (~x)) print(~ b,bin (~b)) Output -11 ( 1111 0101)
AND Operator(&) x=10=0000 1010 y=11=0000 1011 x&y =0000 1010 (10) example x=10; b=15 print(x & b, bin(x &b)) x=0b1001;x=0b1001 print( x & b, bin( x & b) ) Output:
Bitwise OR Operator(|) x=10=0000 1010 y=11=0000 1011 X|y =0000 1011 (15) example x=10;b=15 print(x | b, bin(x | b)) x=0b1001;b=0b1001 print( x | b, bin( x | b) ) Output 15 0b1111 9 0b1001
Bitwise left shift operator(<<): shifts the bits of the number towards left specified number of positions . x=10 X<<2 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 ( first time shift) 0 0 1 0 1 0 0 0 ( second time shift) (40) Example x=10 print(x<<2, bin(x<<2)) x=0b1001 print(x<<1,bin(x<<1)) Output 40 0b101000 18 0b10010
Bitwise right shift operator(>>): shifts the bits of the number towards right specified number of positions. x=10 X>>2 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 ( first time shift) 0 0 0 0 0 0 1 0 ( second time shift) (2) Example x=10 print(x>>2, bin(x>>2)) x=0b1001 print(x>>1,bin(x>>1)) Output 2 0b10 4 0b100
Membership operators: to test a member is in sequence such as list, string, tuple and dictionary in operator: it returns True if an element is found in specified sequence otherwise returns False. Example: City=[‘ Belagum ’, ‘Bombay’, ’Mysore’ , ‘Lucknow’] for k in City: print(k) If ‘Mysore’ in City: print(“yes”) else: print(“no”) Output Belagum Bombay Mysore Lucknow yes
Contd.. not in operator: Works in reverse manner for in operator. This operator returns True if an element is not found in the sequence City=[" Belagum " , "Bombay" , "Mysore" , "Lucknow"] if "Mysore" not in City: print("yes") else: print("no") Output: no
Identity Operator The memory location of object can be seen using the id() function. This function returns an integer number called the identity number that internally represents the memory location of the object. a=25 b=25 print(id(a),id(b)) b=47 print(id(a),id(b)) Output 140731879209504 140731879209504 140731879209504 140731879210208
The is operator is operator compare whether two objects identity numbers are same or not. If same it returns True otherwise False. a=25 b=25 if a is b: print("Both have same identity") else: print("They do not have same identity") Output Both have same identity
is not operator If the identity numbers of two objects being compared are not same then it returns True otherwise False. a=25 b=35 if a is not b: print("They do not have same identity") else: print("Both have same identity") Output They do not have same identity
Operator precedence and associativity operator Name () Parenthesis ** Exponentiation -,~ Unary minus, bitwise complement *, /,//,% Multiplication, division, floor division, modulus +,- Addition, subtraction <<,>> Bitwise left shift, bitwise right shift & Bitwise AND ^ Bitwise XOR | Bitwise OR >,>=,<,<=,==,!= Relational operators
Contd.. Operator Meaning =, %=,/=,//=,-=,+=,*=,**= Assignment operators Is, is not Identity operators In, not in Membership operator not Logical not or Logical or and Logical Associativity: In an expression if we have more than one operators with same priority then we have to flow the associativity of a operator. Associativity operator is either from left to right or right to left. All operators have the associativity from left to right except assignment operator.
Expression evaluation Value=3/2*4+3+(10/4)**3-2 Ans: 22.625 ValueAns:12 Value = 23<45 and 67-89>-10 Ans: False Value=15<<3 + 65//6-7**2 or 8%2 Ans: ValueError : negative shift count Value=not 78/4+(5-2) Ans:False
Mathematical Functions Functions Description ceil(x) Raises x value to the next higher integer. If x is integer then same value is returned ceil(4.5) gives 5 floor(x) Decreases x value to the previous integer value. If x is integer then the same value is returned. floor(4.5) gives 4 degree(x) Converts angle value x from radians into degrees degree(3.142) gives 179.999 radians(x) Converts x value from degrees into radians radians(180) gives 3.142 sin(x) Gives sine value of x. Here x value is given in radians. sin(0.5) gives 0.4794 cos(x) Gives cosine value of x. Here x value is given in radians. cos(0.5) gives 0.87758 tan(x) Gives tangent value of x. Here x value is given in radians. tan(0.5) gives 0.5463 exp(x) returns exponentiation of x. This is same as e **x exp(0.5) returns 1.64872
Function Description fabs(x) Gives the absolute value of x fabs(-4.56) gives 4.56 factorial(x) Returns factorial value of x. Raises value error if x is not integer or is negative factorial(4) gives 24 fmod ( x,y ) Returns remainder of division of x and y. fmod () is preferred to calculate modulus for float values than % operator. It works well for integer values. fmod (14.5,3) gives 2.5 fsum (values) Returns accurate sum of floating point values fsum ([1.5,2.4,-3.3]) returns 0.6000 modf (x) Returns float and integral part of x modf (2.56) gives(0.56,2.0) Log10(x) Returns logarithm of x for base 10 Log10(5.2345) gives 0.71888 Log(x,[base]) Returns the natural logarithm of x of specified base log(5.5,2) gives 2.45943 Sqrt(x) Returns positive square root value of x Sqrt(49) gives 7
Function Description pow( x,y ) Raises x value to the power of y pow(5,3) returns 125.0 gcd ( x,y ) Gives greatest common divisor of x and y. Available from python 3.5 onwards gcd (25,30) gives 5 trunc (x) The real value of x is truncated to integer value and returned trunc (15.5676) gives 15 isinf (x) Returns True if x is a positive or negative infinity and False otherwise. num=float(‘inf’) #num is a float number that indicates infinity print( isinf (num)) gives True Isnan (x) Returns True if x is a NaN (not a number) and False otherwise num=float(‘ NaN ’) #converts NaN into a float representation print( isnan (num)) gives True
Constants in Math Module Constant Description pi The mathematical constant pi value 3.141592 e e= 2.718281 inf A floating point positive infinity. For negative infinity use –math.inf nan A floating point not a number ( NaN ) value To use these constants and math built functions we should import math module import math b= math.sqrt (10) or from math import sqrt b=sqrt(10)
Programs on mathematical functions Write a python program to calculate area of circle #python program to find area of a circle import math r=4.5 area= math.pi * math.pow (r,2) print("Area of a circle", math.floor (area)) Output Area of a circle 63