Simple and compound statements of python in easy way

Statements in python

There are two major types of statements in python : simple and compound statements

Simple statement in python

As the name suggests, simple statement are comprised in a one single line.

These types of statements will never affect flow of program.

There are many simple statements like assert, break, continue, pass ,del,import, return, etc

Expression Statement

This type of statements are formed using operators, variables or values which create one mathematical expression.


>>> i=5 
>>> v=i-2
>>> v
>>> z=i**2
>>> z

Assignment Statement

In this type of statement we assign value to a variable.

This statement has one equal (=) sign.

We cannot set keyword as a variable name.


>>> i=5 
>>> a=10
>>> #keyword cannot be used in asignment
>>> for=5
SyntaxError: invalid syntax
>>> z='itvoyagers'

assert Statement

This statements are use to check condition if the condition returns ‘true’ then it will simply go on next line but if the condition statements returns false then it will stop the program execution and displays the error.


x = [1,2,3,4]


#if condition returns True, then nothing happens:
assert x == [1,2,3,4]

#if condition returns False, AssertionError is raised:
assert x == [1,2,3,4,5]


Output : 

Traceback (most recent call last):
File "", line 7, in <module>
assert x == [1,2,3,4,5]

del Statement

It is used to delete a name or variable.

>>> p=80
>>> q=p
>>> del p
>>> p
Traceback (most recent call last):
File "<pyshell#16>", line 1, in
NameError: name 'p' is not defined
>>> q
>>> z=80
>>> id(q) #checks location of 80
>>> id(z) #checks location of 80

EXPLANATION : This proves that here del statement deletes on variable name and not the value. If we assign same value to another variable it creates new reference to the value present in same location.

Augmented Statement

In augmented statement all arithmetic operators like +,-,*,/,//,%,** are suffixed with equal to symbol (=).

This operators states which operation is to be perform on both the values and it stores the result value in variable which is on left side of expression.

In augmented statement each step preserves the output and next step uses previous output for next operation.

This means continuously updated values are fed to next step for further execution.

Example :

>>> m=10
>>> m
>>> m+=5
>>> m
>>> m-=2
>>> m
>>> m*=2
>>> m
>>> m/=2
>>> m

import Statement

As the name says, “import” statement allows us to add another file (module) completely or to add part (class or method) from a file in our program.

Example :

>>>import math # it will include entire time module in our program with its methods and classes
>>> math.pi

Compound Statement in Python

These statements may affect flow of execution.

There are many compound statements like if, for, while, try, with, class etc.

if Statement

In this, if statement will check for certain condition and if the condition is true then it will execute block of code written in it.

Example :

if 0:
    print ("if condition is true i will be printed")
print("if condition is false i will be printed")

Output :

if condition is false i will be printed

for Statement

In python for loop is used to iterate over a sequence like list,string, tuple etc and other iterable objects.

Example :

print("Output of for loop using range()")
for m in list(range(10)):

Output :

Output of for loop using range()

while Statement

In python while loop is used to iterate over a sequence like list,string, tuple etc and other iterable objects.

Example :

while counter < 10:
    print("count is",counter)

Output :

count is 0
count is 1
count is 2
count is 3
count is 4
count is 5
count is 6
count is 7
count is 8
count is 9

try Statement

In python try statement is used to handle exceptions.

Example :

    val=int(input("enter a number"))
    print("value accepted")
except ValueError:
    print("integer number expected")

Output :

enter a number5
value accepted
>>> ================================ RESTART ================================
enter a number0
value accepted
>>> ================================ RESTART ================================
enter a number2.3
integer number expected

Other python related post

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon…….

Other advance python related post

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in ‘re’ module(Part I)- Match vs Search
Functions in ‘re’ module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
New posts coming soon…….
We are aiming to explain all concepts of Python in easiest terms as possible.

Leave a Comment