Operators and Expressions

PYTHON OPERATORS

  • The operator can be defined as a symbol that is responsible for a particular operation between two operands.
    • Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands, and + is called operator.
  • Operators are the pillars of a program on which the logic is built in a specific programming language.
  • Operators are used to performing operations on variables and values.
  • Python provides a variety of operators, which are described as follows.
    • Arithmetic operators
    • Assignment operators
    • Comparison operators
    • Logical operators
    • Identity operators
    • Membership operators
    • Bitwise operators

ARITHMETIC OPERATORS

  • Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.


      Example

ASSIGNMENT OPERATORS
  • The assignment operator is used to assign the value of the right expression to the left operand.

Example: -
OUTPUT: - 

COMPARISON OPERATOR
  • Comparison operators are used to comparing the value of the two operands and return Boolean true or false accordingly.
Example: -

LOGICAL OPERATORS
  • The logical operators are used primarily in the expression evaluation to make a decision.
Example: -
IDENTITY OPERATORS
  • The identity operators are used to decide whether an element certain class or type.
Example: - 

MEMBERSHIP OPERATORS
  • Python membership operators are used to check the membership of value inside a Python data structure. If the value is present in the data structure, then the resulting value is true otherwise it returns false.
Example: -

BITWISE OPERATOR
  • The bitwise operators perform bit by bit operation on the values of the two operands.
Example: -

OPERATOR PRECEDENCE

The precedence of the operators is essential to find out since it enables us to know which operator should be evaluated first.

Example: -

EXPRESSIONS

An expression is a combination of operators and operands that is interpreted to produce some other value. In any programming language, an expression is evaluated as per the precedence of its operators. So that if there is more than one operator in an expression, their precedence decides which operation will be performed first. We have many different types of expressions in Python. Let’s discuss all types along with some exemplar codes:

  • Constant Expressions: These are the expressions that have constant values only.
  • Example: - 
Output: - 
  • Arithmetic Expressions: An arithmetic expression is a combination of numeric values, operators, and sometimes parenthesis. The result of this type of expression is also a numeric value. The operators used in these expressions are arithmetic operators like addition, subtraction, etc. Here are some arithmetic operators in Python:
  • Integral Expressions: These are the kind of expressions that produce only integer results after all computations and type conversions.
  • Floating Expressions: These are the kind of expressions that produce floating-point numbers as a result of all computations and type conversions.
  • Relational Expressions: In these types of expressions, arithmetic expressions are written on both sides of relational operator (> , < , >= , <=). Those arithmetic expressions are evaluated first, and then compared as per relational operator and produce a boolean output in the end. These expressions are also called Boolean expressions.
  • Logical Expressions: These are kinds of expressions that result in either True or False. It basically specifies one or more conditions. For example, (10 == 9) is a condition if 10 is equal to 9. As we know it is not correct, so it will return False. Studying logical expressions, we also come across some logical operators which can be seen in logical expressions most often.
  • Bitwise Expressions: These are the kind of expressions in which computations are performed at the bit level.
  • Combinational Expressions: We can also use different types of expressions in a single expression, and that will be termed combinational expressions.
OPERATOR ASSOCIATIVITY
  • If an expression contains two or more operators with the same precedence then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.
  • Example: Operator Associativity

NON-ASSOCIATIVE OPERATORS
  • Some operators like assignment operators and comparison operators do not have associativity in Python. There are separate rules for sequences of this kind of operator and cannot be expressed as associativity.
  • For example, x < y < z neither means (x < y) < z nor x < (y < z). x < y < z is equivalent to x < y and y < z, and is evaluated from left-to-right.
  • Furthermore, while chaining of assignments like x = y = z = 1 is perfectly valid, x = y = z+= 2 will result in error.


Comments

Popular posts from this blog

Modules

Control Structures

Classes and Objects