Python Statements
Python Statements

Python Statements

A statement is an instruction that a Python interpreter can execute. Learn simple statements and compound statements.

Table of contents

  • Python Compound Statements
  • Simple Statements
    • Expression statements
    • The pass statement
    • The del statement
    • The return statement
    • The import statement
    • The continue and break statement
    • 1.0 What is a statement in Python

      statement is an instruction that a Python interpreter can execute. So, in simple words, we can say anything written in Python is a statement. Python statement ends with the token NEWLINE character. It means each line in a Python script is a statement.

      For example, a = 10 is an assignment statement. where a is a variable name and 10 is its value. There are other kinds of statements such as if statement, for statement, whilestatement, etc.

      There are mainly four types of statements in Python,

    • print statements,
    • Assignment statements, 
    • Conditional statements, 
    • Looping statements.
    • The print and assignment statements are commonly used. The result of a print statement is a value. Assignment statements don’t produce a result it just assigns a value to the operand on its left side. A Python script usually contains a sequence of statements. If there is more than one statement, the result appears only one time when all statements execute.

      2.0 Compound Statements

      Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way.

      The compound statement includes the conditional and loop statement.

    • if statement: It is a control flow statement that will execute statements under it if the condition is true. Also kown as a conditional statement.
    • while statements: The while loop statement repeatedly executes a code block while a particular condition is true. Also known as a looping statement.
    • for statement: Using for loop statement, we can iterate any sequence or iterable variable. The sequence can be string, list, dictionary, set, or tuple. Also known as a looping statement.
    • try statement: specifies exception handlers.
    • with statement: Used to cleanup code for a group of statements, while the with statement allows the execution of initialization and finalization code around a block of code.
    • 3.0 Simple Statements

      There are various types of simple statement in Python, Python has various simple statements for a specific purpose. Namely

    • Expression statements
    • The pass statement
    • The del statement
    • The return statement
    • The import statement
    • The continue and break statement
    • 1.0 Expression statements

      This is a simple statement that assign a value to a variable. But in a script, an expression all by itself doesn’t do anything! So we mostly assign an expression to a variable, which becomes a statement for an interpreter to execute.

      x = 5
      # right hand side of = is a expression statement
      
      # y = x + 10 is a complete statement
      y = x + 10

      2.0 The pass statement

      pass is a null operation. Nothing happens when it executes. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed.

      For example, you created a function for future releases, so you don’t want to write a code now. In such cases, we can use a pass statement.

      Example:

      # create a function
      def fun1(arg):
          pass  # a function that does nothing (yet)

      3.0 The del statement

      The Python del statement is used to delete objects/variables.

      Syntax:

      del target_list

      The target_list contains the variable to delete separated by a comma. Once the variable is deleted, we can’t access it.

      x = 10
      y = 30
      print(x, y)
      
      # delete x and y
      del x, y
      
      # try to access it
      print(x, y)
      10 30
      NameError: name 'x' is not defined

      4.0 The return statement

      We create a function in Python to perform a specific task. The function can return a value that is nothing but an output of function execution.

      Using a return statement, we can return a value from a function when called.

      Example:

      # Define a function
      # function accepts two numbers and return their sum
      
      def addition(num1, num2):
          return num1 + num2  # return the sum of two numbers
      
      # result is the return value
      result = addition(10, 20)
      print(result)
      
      
      # Output
      30

      5.0 The import statement

      The import statement is used to import modules. We can also import individual classes from a module. Python has a huge list of built-in modules which we can use in our code. For example, we can use the built-in module DateTime to work with date and time.

      import datetime
      
      # get current datetime
      now = datetime.datetime.now()
      print(now)
      
      # Output
      2021-08-30 18:30:33.103945

      6.0 The continue and break statement

    • break Statement: The break statement is used inside the loop to exit out of the loop.
    • continue Statement: The continue statement skip the current iteration and move to the next iteration.