Programming Basics- Operators

Programming Basics- Operators

Programming Basics - Operators

Introduction

  • Operators are special symbols in programming that carry out arithmetic or logical computation.
  • They help in manipulating data and variables to perform specific operations.

Arithmetic Operators

  • Arithmetic operators are used to conduct mathematical operations such as addition, subtraction, multiplication etc.
  • Examples include ’+’ (addition), ‘-‘ (subtraction), ‘*’ (multiplication), ‘/’ (division), and ’%’ (modulus) which gives the remainder of a division.
  • Notably ‘//’ (floor division) gives quotient and ‘**’ (exponent) raises to power, both in Python only.

Comparison Operators

  • Comparison operators, also known as relational operators, are used to compare two values and return a boolean value, either True or False.
  • These include ’==’ (equal to), ‘!=’ (not equal to), ‘>’ (greater than), ‘<’ (less than), ‘>=’ (greater than or equal to), ‘<=’ (less than or equal to).

Assignment Operators

  • Assignment operators are used to assign values to variables.
  • The most common type is ’=’ (equals), but there are compound assignment operators that combine arithmetic or bitwise operations with assignment, like *’+=’, ‘-=’, ‘=’, ‘/=’, ‘%=’, ‘//=’, ‘**=’, ‘&=’, ‘ =’, ‘^=’, ‘»=’, ‘«=’**.

Logical Operators

  • Logical operators are used to combine multiple conditions.
  • They include ‘and’, ‘or’, ‘not’ which represent logical AND, logical OR and logical NOT respectively.

Bitwise Operators

  • Bitwise operators act on operands as if they were strings of binary digits.
  • They include **‘&’ (bitwise AND), ‘ ’ (bitwise OR), ‘^’ (bitwise XOR), ‘~’ (bitwise NOT), ‘«’ (bitwise left shift), ‘»’ (bitwise right shift)**.

Membership Operators

  • Membership operators are used to test whether a value is a member of a sequence, such as strings, lists, or tuples.
  • These include ‘in’ and ‘not in’.

Identity Operators

  • Identity operators are used to compare the memory locations of two objects.
  • They encompass ‘is’ and ‘is not’ in Python.
  • ‘is’ operator evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
  • ‘is not’ operator evaluates to false if the variables on either side of the operator point to the same object and true otherwise.

Understanding operators and their application is vital in creating functional and efficient code to solve complex problems.