Simple Statements in Python

 A statement is an instruction that the Python interpreter can execute. The statement in python is classified as  

 

•  Simple statement

            Single line statement

            Multi-line line statement

•   Compound statement

Simple statement

Simple statements are comprised of with-in a single logical line.

Multi-line statement

A statement is split into multiple lines using a backslash (\)  to indicate that a statement is continued on the next line.

Compound statement

Compound statements contain (groups of) other statements. For example if, while, for.

Expression Statement

It evaluates the expression list or a single expression.

Example :

x = y + z

An expression list is a tuple. The number of arguments is the length of a tuple. For example

x,y,z = 1,2,3

Here 1,2,3 is an expression list.

Assignment Statement

An assignment statement assigns a value to the variable.

Example :

x = y

x,y = 1,2

a = sqr(2)

​​​​​​​Augmented assignment statements

The example of the augmented assignment expression is x += 1. It is equivalent to  x = x + 1

Example :

x = 2

x *= 2

print(x)

​​​​​​​Output

4

 

The assert statement

An assert statement defines the condition. If the condition is false, assert halts the execution and raise an AssertionError.

Example:

def avgMarks(markslist):

        assert len(markslist) != 0, "List is Empty"

         return sum(markslist)/len(markslist)

marks = []

print("Average of marks:",avgMarks(marks))

​​​​​​​Output

File "", line 3, in avgMarks
    assert len(marks) != 0, "List is Empty"
AssertionError: List is Empty

The pass statement

A Pass is a null operation, when it is executed, nothing happens.

Example:

def ChangeIt():

    pass

The del statement

The statement del an object or individual element

Example:

numlist = [1,2,3,4]

del numlist[0]

print(numlist)

del numlist

print(numlist)

Output

[2,3,4]
NameError: name 'numlist' is not defined

The print statement

The print statement evaluates each expression and writes the result to file object usually standard output

Example:

x = 2

numbers = [1,2,3,4]

print(x)

print(numbers)

Output

2
[1,2,3,4]

The return statement

The return exists from the function return value or None.

Example:

def Odd(val):

    if( val % 2 == 1 ):

        return 1

    else:

        return 0

if(Odd(5) == 1):

    print("Odd")

else:

    print("Even")

Output

Odd

The yield statement

The yield statement is used in a generator function. Whenever you need to generate  values, yield keyword is used rather than return.  A function with yield statement is becoming a generator function

Example:

def GeneratorFunc():

    yield 1

    yield 2

    yield 3

    yield 4

   

for values in GeneratorFunc():

    print(values)

 

Output

1
2
3
4

 

The raise statement

The raise statement is used to throw an exception if an error a condition occurs.

Example:

def GeneratorFunc():

    yield 1

    yield 2

    yield 3

    yield 4

 for values in GeneratorFunc():

    print(values)

Output

1
2
3
4

 

The break statement

It exits from the enclosing loop and skips the optional else clause.

Example:

def breakex(val):

    val = 1

    for i in range(20):

        if(val == 5 ):

            break

        print(val)

        val+=1

breakex(5)

Output

1
2
3
4

 

The continue statement

When a continue statement is encountered, execution move to the next iteration of the loop.

Example

def continueex(string):

      for char in string:

        if( char in 'aeiou'):

            continue

        else:

            print(char, end= '')

 

continueex("testing")

Output

 tstng

​​​​​​​When continue passes control out of a try statement with a finally clause, that finally clause is executed before really starting the next loop cycle.

The import statement

The import statement use to import the modules

Example:

 import math

from random import random

The global statement

The global statement is used to declare the global identifier. The scope of the global identifier is entire code block.

Example:

def Increment():

    global x

    x+=1

    return x

    x = 10

print(Increment())

Output

11

 

The exec statement

This statement supports dynamic execution of Python code.

Example:

exec('print("executing the python code ")')

Output

executing the python code