OPERATORS AND EXPRESSIONS
LEARNING OBJECTIVES
At the end of this chapter students will be able to understand
Types of Operators:
Arithmetic Operators
Relational Operators
Assignment Operators
Logical Operators
Precedence of Operators
Python Expressions
Indentation
OPERATORS
Operators are used to perform some action on data. There are 6 different types of operators:
Arithmetic
Unary They require a single operand to perform an operation. They can precede or follow their operands. For eg. unary +, unary -, increment/decrement operator.
OPERATOR
DESCRIPTION
EXAMPLE
UNARY -
To make the value of the number negative
-5
UNARY +
To make the value of the number positive
+5
Binary They require two operands to perform an operation. For eg. +, -, *, /, % .
OPERATOR
DESCRIPTION
EXAMPLE
+
Add two operands
10+5=15
-
Subtracts two operands
10-5=5
*
Multiplies two operands
10*5=50
/
Divides two operands and gives quotient
10/5=5
10/3.0 = 3.3
10.0/3=3.3
10/3=3
%
Divides two operands and gives remainder
10%5=0
**
Exponentiation
2**4= 16
//
Integer division
5//2=2
5.0//2=2.0
Relational/Comparison Operator : These operators describe the relationship between two operands. For eg. <,>,<=,>=,==,!=
OPERATOR
DESCRIPTION
EXAMPLES
<
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
10<15, true
10>15, false
"rain"<"sun"
true
>
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
15>10, true
15<10, false
"rain">"sun"
false
<=
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
15<=15, true
13<=15, true
16<=15, false
"rain"<="sun"
true
>=
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
15>=15, true
16>=15, true
14>=15, false
"rain">="sun"
false
= =
Checks if the value of left operand is equal to the value of right operand, if yes then condition becomes true.
15= = 15, true
16 = = 15, false
"rain"=="sun"
false
<>
Checks if the value of left operand is not equal to the value of right operand, if yes then condition becomes true.
15 <> 15, false
14 <>15, true
"rain"<>"sun"
true
Assignment Operator
: Following assignment operators are supported by Python:
OPERATOR
DESCRIPTION
EXAMPLE
=
Assigns values from right side operands to left side operand
A = 10
+=
Adds right operand to the left operand and assign the result to left operand
A+=10 is equivalent to
A =A +10
- =
Subtracts right operand from the left operand and assign the result to left operand
A-=10 is equivalent to
A =A -10
*=
Multiply right operand by the left operand and assign the result to left operand
A*=10 is equivalent to
A =A *10
/=
Divides left operand by the right operand and assign the quotient to left operand
A/=10 is equivalent to
A =A /10
%=
Divides left operand by the right operand and assign the remainder to left operand
A%=10 is equivalent to
A =A %10
**=
Performs exponential operations on operands and assigns the result to left operand
A **=2 is equivalent to A=A**2
//=
Performs floor division on operands and assigns the result to left operand
A//=2 is equivalent to A=A//2
Logical Operators
: Logical operators are used to join relational expression.
OPERATOR
DESCRIPTION
EXAMPLE
not
NOT operator. It is a unary operator and it reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false.
>>> not(5>10) = not(false)
true
or
OR Operator. It returns true if any of the conditions joined by this operator are true. It is false when both the conditions are false.
>>> (10<5) or (15>20)
false
>>> (10>5) or (15>20)
true
>>> (10<5) or (15<20)
true
>>> (10<5) or (15< 20)
true
and
AND Operator. It returns true if both the conditions joined by this operator are true. It is false when any of the conditions are false.
>>> (20>5 ) and (15>10)
true
>>> (20<5 ) and (15>10)
false
>>>(20>5 ) and (15<10)
false
>>>(20<5 ) and (15<10)
false
NOTE: Please remember
Expression
Result
Expression
Result
not(false)
true
true and true
true
not(true)
false
false or true
true
false and false
false
false or false
false
false and true
false
true or false
true
true and false
false
true or true
true
PRECEDENCE OF OPERATORS
Python follows the same precedence rules for its mathematical operators that mathematics does. The acronym PEMDAS (Parentheses,Exponentiation, Multiplication/ Division, Addition/S ubtraction) is a useful way to remember the order of operations:
** (Exponentiation)
Highest
Lowest
-(unary minus),+(unary plus)
*(multiply), / (divide), %(modulus), //(floor division)
+(add),-(subtract)
<(less than),<=(less than or equal),>(greater than), >=(greater than or equal to)
==(equal),!=(not equal)
=(simple assignment) and other assignment operators (arithmetic assignment operator)
not (logical NOT), and (logical AND), or (logical OR)
NOTE : Multiplication and division are at the same precedence, and the left-to-right rule applies. Similarly, subtraction and addition are at the same precedence, and the left-to-right rule applies.
EXPRESSIONS
An expression is a combination of values, variables, operators and call to functions. When an expression is typed at the Python prompt, the interpreter evaluates it and displays the result, which is always a value:
EXAMPLE
v=u*t + 1/2*a*t*t
f=(c * 9/5) + 32
math MODULE
The math module provides access to the mathematical functions. Some of the most widely used functions present in this module are:
math.ceil(x)
Return the ceiling of x, the smallest integer greater than or equal to x.
math.floor(x)
Return the floor of x, the largest integer less than or equal to x.
math.exp(x)
Return e**x.
math.log10(x)
Return the base-10 logarithm of x.
math.pow(x, y)
Return x raised to the power y.
math.sqrt(x)
Return the square root of x.
Q Write the Python expression for the following:
(i) -x/y + (1+2a)4 - xy
(ii) a+ (2x-y)/3x
Answers
(i) -x/y + math.pow((1+2*a), 4) - x*y
(ii) a+ math.sqrt((2*x - y)/3*x)
Q Evaluate the following expressions:
(i) 85%2+70-3**3+15
= 85%2 + 70 - 27 +15
= 1 + 70 - 27 +15
= 59
(ii) 5//2*5+8-3**2
= 2*5+8-9
= 10+8-9
= 9
(iii) 5/2*5+8-3**2
= 2.5*5+8-9
= 12.5+8-9
= 11.5
(iv) not(5>10) or (10 == 10) and (not(9<15))
=not(false) or(true) and(not(true))
= true or true and false
=true and false
= true
Q Write a Python script to input values of a, b, c and calculate the roots of the quadratic equation.
# Program to calculate roots of the quadratic equation
import math
a=eval(input('enter value of a'))
b=eval(input('enter value of b'))
c=eval(input('enter value of c'))
D=b*b -4*a*c
root1= (-b + math.sqrt(D))/2*a
root2= (-b - math.sqrt(D))/2*a
print("Root1=", root1)
print("Root2=", root2)
OUTPUT
enter value of a1
enter value of b2
enter value of c1
Root1= -1.0
Root2= -1.0
INDENTATION
Indentation in Python is used to form block of statements also known as suite. In most other programming languages, indentation is used only to make the code look pretty. But in Python, it is required for indicating what block of code, a statement belongs to. Indentation is typically used in flow of control statements (like if, for etc.), function, class etc. Although we can give any number of spaces for indentation but all the statements in a block must have the same indent.
NOTE :
The amount of indentation matters in Python. A missing or extra space in a Python block could result in error or unpredictable results.
It is important to indent the statements within the same block of code at the same level.
Python IDLE is also designed to automatically indent code. For instance, pressing Return key after typing the ‘:’ in the flow of control statements like if, for etc. automatically indents the cursor on the next line.
Unnecessary indentation results in syntax error and incorrect indentation results in logical error.
Built-in functions for working with numbers
Some of the common built-in functions for working with numbers are:
FUNCTION
EXPLANATION
EXAMPLE
abs()
returns absolute value of a number
abs(12) will give 12
abs(-12) will give 12
max()
returns largest element
print(max(10, 20, 30)) will give 30
min()
returns smallest element
print(min(10, 20, 30)) will give 10
pow()
returns x to the power of y
pow(2,3) will give 8
NOTE : The help() function invokes the built-in Help System.
EXAMPLE
>>> help(max)
OUTPUT
Help on built-in function max in module built-in's:
max(...)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the largest argument.
Comments
Post a Comment