Python Statements: Essential Fundamentals You Can’t Afford to Miss in 2024

python statements

Python statements are broadly divided into two categories:

Simple Python Statements

  1. Expression statement
  2. Assignment statement
  3. assert statement
  4. pass statement
  5. del statement
  6. return statement
  7. yield statement
  8. raise statement
  9. break statement
  10. continue statement
  11. import statement
  12. global statement
  13. nonlocal statement

Compound Python Statements

  1. if statement
  2. while statement
  3. for statement
  4. try statement
  5. with statement
  6. Function definition
  7. Class definition
  8. Coroutine

Simple Python Statements

assert statement

  • assert python statements asserts or states the fact confidently in the program.
  • These are boolean expressions that are evaluated as True or False.
  • If evaluated as true then program moves to next step and if evaluated as false then execution stops and throws error.
  • We can define error message. If not defined it throws “Assertion error”.

Program: assert python statements

a=int(input("First Number: "))
b=int(input("Second Number: "))
assert b!=0, "Division by zero"
print("Division = ",a/b)

Output1:
First Number:15
Second Number:4
Division=3.75

Output2:
First Number: 15
Second Number:0
AssertionError: Division by zero

pass statement

  • This pass is a null statement and is executed.
  • When executed, doesn’t do anything.
  • Is used when it is syntactically required to show “do nothing”
  • Usually it is used in compound statements to indicate no action or in function of class definition, it indicates no definition.

del statement

  • del is a python keyword.
  • It is used to delete variable, user defined objects, lists items within list, dictionaries etc.

Example of del python statements:

a=5
a
print(a)

del a
a
NameError:name'a' is not defined.

return statement

  • return python statements is only used in function definition.
  • It is used to return control and/or value to calling function.

yield statement

  • It is used only in function definition.
  • We can use yield statement instead of return statement.
  • The difference is that, return statement terminates a function entirely whereas yield pauses the function saving all its states and later continues from there on successive function call.
  • Both yield and return statement return some value from a function.

break statement

  • It is used only in for or while loop.
  • It terminates the loop and transfer control out of the loop.
  • In exception handling, when break passes control out of a try with finally clause, the finally clause is executed before leaving the loop.

continue statement

  • It is used only in for or while loop.
  • It continue execution with the next cycle of the nearest closing loop.
  • In exception handling, when continue passes control out of a try with finally clause, the finally clause is executed before leaving the loop.

import statement

  • It is used to import the module and access the definitions inside imported module.
  • Definition are accessed as module definition.
  • Programmer can declare user defined modules.
  • These previously defined modules also can be imported using import statement.

Example of import python statements:

import math
print(math.pi)
3.14152653589793
  • It has two clauses, as and from.
  • If the module name is followed by as, the name following as is bound directly to the imported module.
  • The from form is used to import specific attributes and functions only.
  • If the statement is:
    >>>from abc import pqr
    Here pqr can be module, sub package or object
    For example:
    >>>from math import pi

future statement

  • This is a directive to the compiler to compile particular module using syntax that will be available in specified future release of python.
  • This helps to migrate to future version of python.
  • It must appear near the top of the module.
  • Only following is allowed before future statement:
    Module docstring
    Comment
    Blank lines
    Other future statement
  • All historical features are enabled by statement in current version.

global statement

  • This is a declaration statement which holds the value for the entire current code block.
  • The identifiers declared as global are interpreted as global.
  • It creates global variable and make changes in local context.
  • Note that:
    Variable created inside a function is by default local.
    Variable created outside a function is by default global.
    Use global keyword to read and write variable inside a function.

nonlocal statement

  • Nonlocal is the category of variables that are used in nested function whose local scope is not defined.
  • These variable are neither in local scope nor in global scope.
  • Variables listed in a nonlocal statement must not collide with per-existing binding in the local scope.

Compound Python Statements

  • Compound statement is a group of other statements.
  • It consists of one or multiple lines.
  • Traditional control flow statement if, while and for are compound statement.
  • Exception handling using try is also a compound statement.

if statement

  • It evaluates given test_condition.
  • If it is evaluates as True then executes if body.
  • If test_condition is evaluated as False then executes else body.
  • Action statement is blocked by indentation.
  • Among several blocks, only one block is executed.
  • Syntax:
    1) if test_condition:
    Statement(s)
    2) if test_condition:
    Statement(s)1
    else:
    Statement(s)2
    3) if test_condition
    Statement(s)1
    else if test_condition:
    Statement(s)2
    else:
    Statement(s)3

Example of if python statements:

n=1
print("positive" if n>=0 else "negative")

Output:
positive

n=-1
print("positive" if n>=0 else "negative")

Output:
negative

n=0
print("+ve") if n>0 else print("zero") if n==0 else print ("-ve")

Output:
zero

while statement

  • This is a iterative statement where a block of code executes repeatedly as along as the test condition is true.
    Syntax:
    while test_condition:
    Loop_body
    [else:
    statement(s)]
  • Python evaluates test condition and on that basis decides whether to iterate loop_body.
  • If test_condition is true then executes loop body otherwise transfer control to higher level of indent.
  • else specifies what to do when the loop has finished.
  • Note that value None and 0 are interpreted as False whereas any non-zero value is interpreted as True.
  • If loop_body contains break statement, it terminates the block and transfer statement to statement following that block.
  • A continue statement in loop_body skips the rest of statements in block and goes back to test the expression at the beginning.

for statement

  • Syntax:
    for iterator in expression:
    Statement(s)
    [else:

    Statement(s)]
  • The for loop allows to perform some action for each item in a list.
  • The difference between for and while loop is that, for loop iterates over a sequence whereas while loop iterates against a predefined condition.
  • else specifies what to do when the loop has finished.
  • The break statement breaks the loop whereas continue statement transfers control to the beginning of the loop to test_condition.

One thought on “Python Statements: Essential Fundamentals You Can’t Afford to Miss in 2024

  1. order neurontin on line no prescription Order neurontin cheap without doctor’s order neurontin online buy cheap buy generic cytotec USA neurontin by fedx buy neurontin at cheap rates no rx fedex shipping order neurontin USA no prescription online canadian pharmacy no prescription Learn About neurontin no rx required Stores buy neurontin co buy cheapest neurontin online buy with paypal

Leave a Reply

Your email address will not be published. Required fields are marked *