# Python Programming – Simple statement

In this Page, We are Providing Python Programming – Simple statement. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

## Python Programming – Simple statement

Simple statement

A simple statement is comprised within a single logical line. Several simple statements may occur on a single physical line separated by semicolons. The extended BNF notation describing syntax for simple statement is:

simple__stmt : := expression_stmt
| assert_stmt
| assignment_stmt
| augmented_assignment_stmt
| pass_stmt
| del_stmt
| print_stmt
| return_stmt
| yield_stmt
| raise_stmt
| break_stmt
| continue_stmt
| import_stmt
| global_stmt
| exec_stmt

This book will discuss some of the simple statements.

Expression statement

An expression in a programming language is a combination of values, constants, variables, operators, functions etc. that are interpreted according to the particular rules of precedence for a particular programming language, which computes and then returns another value. This process is called evaluation. An expression statement evaluates the expression list (which may be a single expression).

expression_stmt : := expression_list

The following are examples of expression statements and their evaluation.

>>> 4
4
>>> 4==4
True
>>> 2+6
8
>>> ' Hi ' * 3
' HiHiHi '

Assignment statement

Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects. The following example shows binding value 4 to object (or name) a.

>>> a=4

Pass statement

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

pass_stmt  : : =  " pass "

The following example defines a function f and does nothing.

>>> def f ( arg ) :
. . .      pass
. . .

Del statement

This statement deletes each target in target_list from left to right.

del_stmt : := " del " target_list

Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a global statement in the same code block. If the name is unbound, a NameError exception will be raised.

>>> a = [ -1 , 1 , 66  .25 , 333 , 333 , 1234 . 5 ]
>>> del a [ 0 ]
>>> a
[ 1 , 66 . 25 , 333 ;  333 , 1234 . 5 ]
>>> del a [ 2 : 4 ]
>>> a
[ 1 , 66 . 25 , 1234 . 5 ]
>>> del a [ : ]
>>> a
[ ]
>>> a = [ -1 , 1 , 66 . 25 , 333 , 333 , 1234 . 5 ]
>>> del a
>>> a

Traceback ( most recent call last ) :
File " <pyshell#24> " , line 1 , in <module>
a
NameError: name ' a ' is not defined

Print statement

The print statement evaluates each expression and then writes the resulting object to standard output (computer screen). If an object is not a string, it is first converted to a string using the rules for string conversion, the resulting or original string is then written. Space is written before each object is (converted and) written unless the output system believes it is positioned at the beginning of a line. A ‘ \n’ character is written at the end unless the print statement ends with a comma. If only the print keyword is there in the print statement, then only the ‘ \ n ‘ character is written. Following is a script “print_example.py”, having the following code:

a=20
print ' hi ' , 5 , a
print ' bye ' , 10 , a
print
print ' hi ' , 5 , a ,
print ' bye ' , 10 , a ,

Output after running the script is:

>>> runfile ( ' C : / Temp / print_example . py ' , wdir=r ' C : / Temp ' )
hi 5 20
bye 10 20

hi 5 20 bye 10 20

Return statement

The return statement leaves the current function call with the expression_list (or None) as the return value.

return_stmt : := " return " [ expression_list ]

The following script ” addition_example . py ” demonstrate the use of return statement.

def summation ( arg1 , arg2 ) :
return arg1+arg2

print ' Sum is : ' , summation ( 1 , 2 )

def summation ( arg1 , arg2 ) :        # The function return None
print ' Sum is: ' , arg1+arg2

summation ( 3 , 4 )
print summation ( 3 , 4 )

def summation ( arg1 , arg2 ) :        # The function return None
print 'Sum is: ',arg1+arg2
return

summation ( 5 , 6 )
print summation ( 5 ,6)

def summation(argl,arg2):               # The function return None
print ' Sum is: ' , arg1+arg2
return None

summation ( 7 , 8 )
print summation ( 7 , 8 )

def summation ( arg1 , arg2) :
def add ( arg1 , arg2) :
return arg1+arg2
return add (arg1 , arg2 )

print ' Sum is : ' , summation ( 9 , 10)

The output after running the script is:

>>> runfile ( ' C : / Temp / addition_example . py ' , . wdir=r ' C : / Temp ' )
Sum is: 3
Sum is: 7
Sum is: 7
None
Sum is: 11
Sum is: 11
None
Sura is: 15
Sum is: 15
None
Sum is: 19

Break statement

The break statement terminates the nearest enclosing loop, skipping the optional else clause if the loop has one.

break_stmt : : = " break "

If a for loop is terminated by break, the loop control target keeps its current value. The following script ” break_example . py ” demonstrates the use of the break statement.

a=5
for i in range ( 10 ) :
if i==a:
break
else:
print i

print i

The output after running the script is:

>>> runfile ( ' C : / Temp / addition_example . py ' , wdir= r ' C : / Temp ' )
0
1
2
3
3
5

Continue statement

The continue statement makes the current nearest enclosing loop skip one iteration and executes the remaining ones.

continue_stmt : : = " continue "

The following script ” continue_example . py ” demonstrate the use of the continue statement.

for i in range ( 10 ) :
if i>4 and i<8 :
continue
else:
print i

The output after running the script is:

>>> runfile ( ' C : / Temp / continue_example . py ' , wdir= r ' C : / Temp ' )
0
1
2
3
4
8
9

Import statement

Definitions (variables, function definitions, etc.) from one module can be imported into another module using an import statement. For more information, please refer to chapter 5.

Global statement

The global statement is a declaration that makes listed identifiers to be interpreted as global. This will become clearer by referring to section 3.2.5.

global_stmt : := " global " identifier ( " , " identifier) *