Operators in Python All the operators in Python are classified according to their nature and type and they are: • Arithmetic Operators • Relational Operators • Logical Operators • Assignment Operators • Bitwise Operators • Boolean Operators • Membership Operators • Identity Operators
Arithmetic Operators • These operators perform basic arithmetic operations like addition, subtraction, multiplication, division etc. and these operators are binary operators that means these operators acts on two operands. And there are 7 binary arithmetic operators available in Python. Operator Meaning Example Result + Addition 10 + 7 12 - Subtraction 10.0 - 1.5 8.5 * Multiplication 30 * 3 900 / Float Division 5 / 2 2.5 // Integer Division 5 // 2 2 ** Exponentiation 3 ** 2 9 % Remainder 10 % 3 1 Operator Priority Parenthesis (( ), [ ]) First Exponentiation (**) Second Multiplication (*), Division (/, //), Modulus (%) Third Addition (+), Subtraction (-) Fourth Assignment Fifth
Relational Operators • Relational operators are used for comparison and the output is either True or False depending on the values we compare. The following table shows the list of relational operators with example. Operator Meaning Example Result < Less than 5 < 7 True > Greater than 9 > 5 True <= Less than equal to 8 <= 8 True >= Greater than equal to 7 >= 9 False == Equal to 10 == 20 False != Not equal to 9 != 6 True
Logical Operators • Logical operators are used to form compound conditions which are a combination of more than one simple condition. Each of the simple conditions are evaluated first and based on the result compound condition is evaluated. The result of the expression is either True or False based on the result of simple conditions. Operator Meaning Example Result and Logical AND (5 > 7) and (3 < 5) False or Logical OR (7 == 7) or (5 != 5) True not Logical NOT not(3 <= 2) True
Assignment Operators • These operators are used to store a value into a variable and also useful to perform simple arithmetic operations. Assignment operators are of two types: simple assignment operator and augmented assignment operator. Simple assignment operators are combined with arithmetic operators to form augmented assignment operators. The following table shows a list of assignment operators and its use. Operator Meaning Example Result = Simple assignment a = 10 10 += Addition assignment a = 5 a += 8 13 -= Subtraction assignment b = 5 b -= 8 -3 *= Multiplication assignment a =10 a *= 8 80 /= Float Division assignment a = 10 a /= 8 1.25 //= Integer Division assignment b = 10 b //= 10 1 **= Exponentiation assignment a = 10 a %= 5 0 %= Remainder assignment b = 10 b ** = 8 100000000
Bitwise Operators • Bitwise Operators acts on individual bits of the operands. These operators directly act on binary numbers. If we want to use these operators on integers then first these numbers are converted into binary numbers and then bitwise operators act on those bits. The following table shows the list of bitwise operators available in Python. Operator Meaning Example Result & Bitwise AND a = 10 = 0000 1010 b = 11 = 0000 1011 a & b = 0000 1010 = 10 a & b = 10 | Bitwise OR a = 10 = 0000 1010 b = 11 = 0000 1011 a | b = 0000 1011 = 11 a | b = 11 ^ Bitwise XOR a = 10 = 0000 1010 b = 11 = 0000 1011 a ^ b = 0000 0001 = 1 a ^ b = 1 ~ Bitwise Complement a = 10 = 0000 1010 ~a = 1111 0101 = -11 ~a = -11 << Bitwise Left Shift a = 10 a << 2 = 40 a << 2 = 40 >> Bitwise Right Shift a = 10 a >> 2 = 2 a >> 2 = 2
Boolean Operators • There are three boolean operators that act on bool type literals and provide bool type output. The result of the boolean operators are either True or False. Operator Meaning Example Result and Boolean AND a = True, b = False a and b = True and False a and b = False or Boolean OR a = True, b = False a or b = True or False a or b = True not Boolean NOT a = True not a = not True not a = False
Membership Operators There are two membership operators in Python that are useful to test for membership in a sequence. • in: This operator returns True if an element is found in the specified sequence, otherwise it returns False. • not in: This operator returns True if any element is not found in the sequence, otherwise it returns True.
Identity Operators These operators are used to compare the memory locations of two objects. Therefore it is possible to verify whether the two objects are same or not. In Python id() function gives the memory location of an object. Example id(a) returns the identity number or memory location of object a. There are two identity operators available in Python. They are • is: This operator is used to compare the memory location of two objects. If they are same then it returns True, otherwise returns False. • is not: This operator returns True if the memory locations of two objects are not same.If they are same then it returns False.
Operator Precedence and Associativity • An expression may contain several operators and the order in which these operators are executed in sequence is called operator precedence. The following table summarizes the operators in descending order of their precedence. Operator Name Precedence ( ) Parenthesis 1st ** Exponentiation 2nd -, ~ Unary minus, bitwise complement 3rd *, /, //, % Multiplication, Division, Floor Division, Modulus 4th +, - Addition, Subtraction 5th <<, >> Bitwise left shift, bitwise right shift 6th & Bitwise AND 7th ^ Bitwise XOR 8th | Bitwise OR 9th >, >=, <, <=, = =, != Relational Operators 10th =, %=, /=, //=, -=, +=, *=, **= Assignment Operators 11th is, is not Identity Operators 12th in, not in Membership Operators 13th not Logical NOT 14th or Logical OR 15th and Logical AND 16th
Single Line and Multiline Comments • There are two types of comments used in Python: • Single Line Comments: These are created simply by starting a line with the hash character (#), and they are automatically terminated by the end of line. If a line using the hash character (#) is written after the Python statement, then it is known as inline comment. • Multiline Comments: When multiple lines are used as comment lines, then writing hash character (#) in the beginning of every line is a tedious task. So instead of writing # character in the beginning of every line, we can enclose multiple comment lines within ''' (triple single quotes) or """ (triple double quotes). Multi line comments are also known as block comments.
INPUT AND OUTPUT • The purpose of a computer is to process data and return results.The data given to the computer is called input. The results returned by the computer are called output. So, we can say that a computer takes input, processes that input and produces the output.

Operators in Python Arithmetic Operators

  • 1.
    Operators in Python Allthe operators in Python are classified according to their nature and type and they are: • Arithmetic Operators • Relational Operators • Logical Operators • Assignment Operators • Bitwise Operators • Boolean Operators • Membership Operators • Identity Operators
  • 2.
    Arithmetic Operators • Theseoperators perform basic arithmetic operations like addition, subtraction, multiplication, division etc. and these operators are binary operators that means these operators acts on two operands. And there are 7 binary arithmetic operators available in Python. Operator Meaning Example Result + Addition 10 + 7 12 - Subtraction 10.0 - 1.5 8.5 * Multiplication 30 * 3 900 / Float Division 5 / 2 2.5 // Integer Division 5 // 2 2 ** Exponentiation 3 ** 2 9 % Remainder 10 % 3 1 Operator Priority Parenthesis (( ), [ ]) First Exponentiation (**) Second Multiplication (*), Division (/, //), Modulus (%) Third Addition (+), Subtraction (-) Fourth Assignment Fifth
  • 3.
    Relational Operators • Relationaloperators are used for comparison and the output is either True or False depending on the values we compare. The following table shows the list of relational operators with example. Operator Meaning Example Result < Less than 5 < 7 True > Greater than 9 > 5 True <= Less than equal to 8 <= 8 True >= Greater than equal to 7 >= 9 False == Equal to 10 == 20 False != Not equal to 9 != 6 True
  • 4.
    Logical Operators • Logicaloperators are used to form compound conditions which are a combination of more than one simple condition. Each of the simple conditions are evaluated first and based on the result compound condition is evaluated. The result of the expression is either True or False based on the result of simple conditions. Operator Meaning Example Result and Logical AND (5 > 7) and (3 < 5) False or Logical OR (7 == 7) or (5 != 5) True not Logical NOT not(3 <= 2) True
  • 5.
    Assignment Operators • Theseoperators are used to store a value into a variable and also useful to perform simple arithmetic operations. Assignment operators are of two types: simple assignment operator and augmented assignment operator. Simple assignment operators are combined with arithmetic operators to form augmented assignment operators. The following table shows a list of assignment operators and its use. Operator Meaning Example Result = Simple assignment a = 10 10 += Addition assignment a = 5 a += 8 13 -= Subtraction assignment b = 5 b -= 8 -3 *= Multiplication assignment a =10 a *= 8 80 /= Float Division assignment a = 10 a /= 8 1.25 //= Integer Division assignment b = 10 b //= 10 1 **= Exponentiation assignment a = 10 a %= 5 0 %= Remainder assignment b = 10 b ** = 8 100000000
  • 6.
    Bitwise Operators • BitwiseOperators acts on individual bits of the operands. These operators directly act on binary numbers. If we want to use these operators on integers then first these numbers are converted into binary numbers and then bitwise operators act on those bits. The following table shows the list of bitwise operators available in Python. Operator Meaning Example Result & Bitwise AND a = 10 = 0000 1010 b = 11 = 0000 1011 a & b = 0000 1010 = 10 a & b = 10 | Bitwise OR a = 10 = 0000 1010 b = 11 = 0000 1011 a | b = 0000 1011 = 11 a | b = 11 ^ Bitwise XOR a = 10 = 0000 1010 b = 11 = 0000 1011 a ^ b = 0000 0001 = 1 a ^ b = 1 ~ Bitwise Complement a = 10 = 0000 1010 ~a = 1111 0101 = -11 ~a = -11 << Bitwise Left Shift a = 10 a << 2 = 40 a << 2 = 40 >> Bitwise Right Shift a = 10 a >> 2 = 2 a >> 2 = 2
  • 7.
    Boolean Operators • Thereare three boolean operators that act on bool type literals and provide bool type output. The result of the boolean operators are either True or False. Operator Meaning Example Result and Boolean AND a = True, b = False a and b = True and False a and b = False or Boolean OR a = True, b = False a or b = True or False a or b = True not Boolean NOT a = True not a = not True not a = False
  • 8.
    Membership Operators There aretwo membership operators in Python that are useful to test for membership in a sequence. • in: This operator returns True if an element is found in the specified sequence, otherwise it returns False. • not in: This operator returns True if any element is not found in the sequence, otherwise it returns True.
  • 9.
    Identity Operators These operatorsare used to compare the memory locations of two objects. Therefore it is possible to verify whether the two objects are same or not. In Python id() function gives the memory location of an object. Example id(a) returns the identity number or memory location of object a. There are two identity operators available in Python. They are • is: This operator is used to compare the memory location of two objects. If they are same then it returns True, otherwise returns False. • is not: This operator returns True if the memory locations of two objects are not same.If they are same then it returns False.
  • 10.
    Operator Precedence andAssociativity • An expression may contain several operators and the order in which these operators are executed in sequence is called operator precedence. The following table summarizes the operators in descending order of their precedence. Operator Name Precedence ( ) Parenthesis 1st ** Exponentiation 2nd -, ~ Unary minus, bitwise complement 3rd *, /, //, % Multiplication, Division, Floor Division, Modulus 4th +, - Addition, Subtraction 5th <<, >> Bitwise left shift, bitwise right shift 6th & Bitwise AND 7th ^ Bitwise XOR 8th | Bitwise OR 9th >, >=, <, <=, = =, != Relational Operators 10th =, %=, /=, //=, -=, +=, *=, **= Assignment Operators 11th is, is not Identity Operators 12th in, not in Membership Operators 13th not Logical NOT 14th or Logical OR 15th and Logical AND 16th
  • 11.
    Single Line andMultiline Comments • There are two types of comments used in Python: • Single Line Comments: These are created simply by starting a line with the hash character (#), and they are automatically terminated by the end of line. If a line using the hash character (#) is written after the Python statement, then it is known as inline comment. • Multiline Comments: When multiple lines are used as comment lines, then writing hash character (#) in the beginning of every line is a tedious task. So instead of writing # character in the beginning of every line, we can enclose multiple comment lines within ''' (triple single quotes) or """ (triple double quotes). Multi line comments are also known as block comments.
  • 12.
    INPUT AND OUTPUT •The purpose of a computer is to process data and return results.The data given to the computer is called input. The results returned by the computer are called output. So, we can say that a computer takes input, processes that input and produces the output.