Operators in Python

python operators

operators in python are the special symbols that designate that some sort of computation should be performed. In python there are two types of operators are present in python.

  • Unary operators
  • Binary operators

Unary operators

  • Unary operators are those operators that required one operand to operate upon.
  • The following are some unary operators in python:
    • +        :-      unary plus
    • –         :-   unary minus
    • ~       :-      bitwise complement
    • not     :-    logical negation

Binary operators

  • binary operators are those operators that require two operands to operate upon.
  • Following are some binary operators:
    • Arithmetic operators
      • +             addition
      • –              subtraction
      • *             multiplication
      • /             division
      • %            remainder/modulus
      • **           exponent (raise to power)
      • //            floor division
    • Bitwise operators
      • &            bitwise AND
      • ^             bitwise exclusive OR (XOR)
      • |             bitwise OR
    • Shift operators
      • <<           shift left
      • >>           shift right
    • Identity operators
      • is            is the identity same ?
      • is not     is the identity not same ?
    • Relational operators
      • <             less than
      • >             grater than
      • <=           less then or equal
      • >=           greater then or equal
      • ==           equal to
      • !=           not equal to
    • Assignment operators
      • =             assignment
      • /=           assign quotient
      • +=           assign sum
      • *=           assign product
      • %=          assign reminder
      • -=           assign difference
      • **=        assign exponent
      • //=         assign floor division
    • Logical operators
      • and        logical AND
      • or           logical OR
    • Membership operators
      • in            whether variable in sequence
      • not in     whether variable not in sequence

Barebones of a program

  • In this topic we are going to talk about the basic structure of a python program – what is it contain.
  • As you can see that the above sample program contains various components like:
    1. Expressions
    2. Comments
    3. Blocks and indentation
    4. Statement
    5. Function
  • As you can see that the above sample program contains various components like:
    1. Expressions
    2. Comments
    3. Blocks and indentation
    4. Statement
    5. Function

Expression

  • An expression is any legal combination of symbols that represents a value.
  • It represents something, which python evaluates and which then produces a value.
  • For example: – a+10, v+18, v<56, etc.

Statement

  • A statement is a programming instruction that does something i.e., some action takes place.
  • While an expression is evaluated, a statement is executed i.e., some action takes place.
  • It is not necessary that a statement results in a value; it may or may not yield a value
  • For example: – a=45, d=s-94, etc.

Comments

  • These are the additional readable information to clarify the source code.
  • Comments in python begin with a # symbol and generally end with end of the physical line.
  • Comments can be written in inline mode.
  • It is a major python fundamentals to make the code readable for every one.
  • Basically, python have two types of comments
    • Single-line comment
    • Multiline comment

Single-line comment (normal comment)

  • The physical lines beginning with # are the full line comments.
  • For example : –  
# It is also a comment
# this is a single line comment in python
  • These comments are known as inline comments.
  • For example: –
if 5>g #it is called inline comment

Multiline comment (block comment)

  • If you want to enter a multiline comment or a block comment then you have to apply triple quoted.
  • For example: –     
‘’’this is a multiline comment and it is 
     it is useful for detailed additional information related to the
     program in question.
‘’’
  • This type of multiline comment is also known as docstring.
  • You can either use triple-apostrophe (‘’’) or triple quotes (“””) to write docstring.

Functions

  • A function is a code that has a name and it can be reused (executed again) by specifying its name in the program, where needed.
  • It is also a major python fundamentals to use a block of code again and again in a program.
  • In the above sample program, there is one function namely SeeYou ().
  • The statements indented below its def statement is part of the function.
  • To execute this you have to write the function in the main code as written below.
    • SeeYou ()            #function-call statement
  • Calling of a function becomes a statement e.g., print is a function but when you call print () to print something, then that function call becomes a statement.

Blocks and indentation

  • Sometimes a group of statements is part of another statement or function.
  • These type of group which contains one or more statements is called block/code-block/suite.
  • Many languages such as C, C++, Java, etc., use symbols like {} to show blocks but python does not use any symbol for it, rather it uses indentation
  • Suite in python refers to group of individual statements which make single code-block.
  • You cannot unnecessarily indent a statement; python will raise error for that.

Note :-

  • Learn Python Programing from basics – Click here
  • Add Two numbers in Python (Using 2 steps) – Click here
  • Multiplication Two numbers in Python (Using 2 steps)– Click here