Download as pdf
Download as pdf
You are on page 1of 5
5.8.1 Arithmetic Operators Python supports arithmetic operators that are used to perform the four basic arithmetic operations as well as modular division, floor division and exponentiation. Table 5.3 Arithmetic Operators in Python ‘Operation Deseription Example (Try in Lab) ‘Addition ‘Adds the two numeric values on tither side of the operator ‘This operator can also be used to concatenate two strings on either side of the operator al ‘Hellorndia’ uml pum uml + num2 strl = "Hello" str2 = "India stel + str2 Subtraction Subtracts the operand on the right from the operand on the left >>> a1 umd puma numl_-“num2 Multiplication Multiplies the two values on both side of the operator Repeats the item on left of the operator if first operand is a string and second operand is an integer value 33> 30 ‘Indiarndia! pont num2 = puml + str = ‘India’ strl * 2 Division Divides the operand on the left by the operand on the right and returns the quotient 0.5 Bunt = 8 puma = 4 num2 / num % Modulus Divides the operand on the left by the operand on the right and returns the remainder >>>, uml = 13 pum2 = 5 uml % num2 V7 Floor Division Divides the operand on the left by the operand on the right and returns the quotient by removing the decimal part. It is sometimes also called integer division. aun puma uml numa Exponent Performs exponential (power) calculation on operands. That is, raise the operand on the left to the power of the operand on the right el mum pum puml ** num 5.8.2 Relational Operators Relational operator compares the values of the operands onits either side and determines the relationship among 202021 them. Assume the Python variables numi 10, num2 0, num3 = 10, stri = "Good", str2 Afternoon" for the following examples: Table 5.4 Relational operators in Python Operator [Operation Description ‘Example (Try in Lab) Equals to [If the values of two operands are|>>> numl == nun? ‘equal, then the condition is True, | False otherwise it is False >> strl == str2 False y Not equal to [If values of two operands are not] >>> num != muna equal, then condition is True, otherwise it is False > Greater than | If the value of the left-side operand |>>> num > numa is greater than the value of the right- | 7rue side operand, then condition is True, | >>> str1 > st<2 otherwise it is False ue < Less than If the value of the left-side operand | >>> numl < nun3 ig less than the value of the right-| False side operand, then condition is True,|>>> str2 < stri othenwise it is False rue > Greater than | If the value of the left-side operand is |>>> num1 >= nun2 or equal to | greater than or equal to the value of | True ‘the right-side operand, then condition} >>> num2 >= num? is True, otherwise it is False False >>> stri < Less than or [Ifthe value of the left operand is less [>>> unl <= numz equal to | than or equal to the value of the right | False operand, then is True otherwise it is}>>> num2 <= num3 False ue >>> strl False 5.8.3 Assignment Operators Assignment operator assigns or changes the value of the variable on its left. Table 5.5 Assignment operators in Python Operator Description Example (Try in Lab) = [Assigns value irom right-side operand to left-|>>> numl = 2 side operand >>> num2 = num 2 >>> country = "Indiat >>> country ‘india’ 202021 It adds the value of right-side operand to the left-side operand and assigns the result to the left-side operand += yis same asx=x+y unl = 10 pum2 = 2 uml += num2 uml un strl = ‘Hello! etr2 = "India! strl += 5 strl ‘Helloindia' “= __ | Ieaubiracts the value of right-side operand from | >>> numi the left-side operand and assigns the result to >>> num2 left-side operand >>> num Note: x-= y is same as x= x-y >>> num 8 Tt multiplies the value of right-side operand) [>>> sunt with the value of left-side operand and assigns | >>> num2 the result to left-side operand >>> num Note: x*=y is same as x= x*y >>> num 6 >a DoS a3 ‘indiatndiatndia! 7= __ | divides the value of left-side operand by the [>>> nunl value of right-side operand and assigns the | >>> num2 result to left-side operand 35> numl /= num2 Note: x /= y is same as x= x /y >>> num 2.0 It performs modulus operation using two|>>> num = 7 operands and assigns the result to left-side|>>> num2 = 3 operand >>> num 4+ num2 Note: x %= y is same as x= x%y >>> num Tt performs floor division using two operands [>>> nunl = 7 and assigns the result to left-side operand >>> num2 = 3 Note:x //=y is same as x=x//y >>> numl //= num2 >>> numl 2 = [It performs exponential (power) calculation on|>>> suml = 2 operators and assigns value to the left-side|>>> num2 = operand >>> num **= num2 Note: x **= y is same as x= x"*y >>> num) 202021 5.8.4 Logical Operators There are three logical operators supported by Python. These operators (and, or, not) are to be written in lower case only. The logical operator evaluates to either ‘rue or False based on the logical operands on either side. Every value is logically either True or False. By default, all values are True except None, False, 0 (cero), empty collections", (),[], sand few other special values. So if we say numl = 10, num2 = -20, then both num and num2 are logically True. ‘Table 5.6 Logical operators in Python. Operator | Operation Description ‘Example (Try in Lab) and [Logical AND [If both the operands are|>>> Teue and True true, then condition | rrue becomes True >>> numl = 10 >>> num2 = -20 >>> bool (numt~and num2) True >>> Truevand False False >>> nun3/= 0 >>> bool (num and num3) False >>> False and False False or [Logical OR _|Ifany of the two operands [>>> True or True are True, then condition | True becomes True >>> True or False True >>> bool (num or num3) True >>> False or False False not [Logical NOT [Used to reverse the logical [>>> numi = 10 state of its operand >>> bool (num) True >>> not numt >>> bool (num) False 5.8.5 Identity Operators Identity operators are used to determine whether the value of a variable is of a certain type or not. Identity operators can also be used to determine whether two 202021 variables are referring to the same object or not. There are two identity operators. Table 5.7 Identity operators in Python Deseription Example (Try in Lab) Evaluates True if the variables on either side of the operator point towards the same memory location and False otherwise. varl is var2 results to True if id(vari) is equal to id{var2) 35> numl = 5 >>> type (numt) True >>> num >>> id (num) 1433920576 >>> id (num2) 1433920576 >>> numl is num2 True is int uml is not Evaluates to Faise if the variables on either side of the operator point to the same ‘memory location and True otherwise. var1 is not var? results to True if id(vari) is not equal to id(var2) >>> uml is False mun 5.8.6 Membership Operators Membership operators are used to check if a value is a member of the given sequence or not. Table 5.8 Membership operators in Python Description ‘Example (Try in Lab) in Retums True if the variable/value is foundin the|>>> a = (1,2,3 ‘specified sequence and False otherwise >>> 2 ina ‘True >>> tt ina False notin Returns True if the variable/value is not found in]>>> a = [1,23 the specified sequence and False otherwise >>> 10 not in a True >>> L not ina False 5.9 Expressions An expression is defined as a combination of constants, variables, evaluates to a value. and operators. An expression always ‘A value or a standalone variable is also considered as an expression but a standalone operator is not an expression. Some examples of valid expressions are given below. (4) 100 (ii) num (iii) num - 20.4 202021 (iv) 3.0 + 3.14 (v) 23/3 -5 * 7(24 -2) vi) "Global" + "Citizen"

You might also like