top of page

Structure of Python Program

A Python program has different components like – Statement, Expression, Comments, Function, Block and Indentation.

Introduction

A program is the collection of instructions, which tells the computer to do computing. A Python program has different components like – Statement, Expression, Comments, Function, Block and Indentation.


Structure of a Python Program

Let’s see structure of a simple Python Program


#Structure of Python program

#Comment begin with # sysbol

#Definition of function sayHello()


def sayHello(name):  #Function Header #inline comment

    print("Hello", name)    #Body of Function


#Start of main module


a = 20              #Statement

b = a + 10          #Expression

print(a)            #Statement

if a > 20 :         # : means block, if block start

    print("More than 20")   #indentated statement

else:

    print("Not More than 20")


sayHello("Students")    #calling of function sayHello( )


As you see, a Python program has contain various components-

  • Expression 

  • Statements 

  • Comments

  • Function 

  • Block and Indentation

Expressions 

An expression is any legal combination of symbols that represents something, and when Python evaluates its produces a value


Example -  25 # values only

a+25 # complex expression that evaluate

a > 20 # and produce a value.


Statements 

A statement is a programming instruction that does something i.e. some action takes place.

An expression is evaluated while a statement is executed. Statement may or may not yield a value .


Example- print(“Hello”)

a = 25

b = a / 10

if a > b :

print(“a is bigger than b”)


Comments 

Comments are the additional information, readable by programmer but ignored by Python Interpreter. In Python, comments are written in three ways –


a) Full Line Comments - 

Using of # symbol in the beginning of line, i.e Physical line,

# This is a comment

# Helps in describing the purpose of program or function or module.


b) Inline Comment – 

It starts in the middle of line i.e. Physical line.

a1 = 20 # a1 is the marks of physics , <- Inline Comments

a2 = 30 # a2 is the marks of chemistry


c) Multi–line Comment- 

Python allows you to write the Multi-line comment or Block Comment. We can use # symbol in the beginning of each physical line, or we can write these multiline comment inside the triple quote (either double quote or single quote).


Example -   Method - 1

# Comment line 1

# Comment line2

# Comment line3

Method -

“”” Comment line 1

Comment line2 # This is also called docstring comment.

Comment line 3 “””


Functions – 

A function is a code, that has a name and it can be reused by specifying its name in the program.


Example - sayHello(“Anjeev” ) #Function calling statement

def sayHello(name): # Function header

print(“Hello Mr. “, name) #Function body


Blocks and Indentation – 

Group of statements are written inside the function or statement is called block or code-block or suite.

Python uses indentation to create blocks of code. Statements written at the same indentation level are part of same block /suite.


Statements need or requiring suite / code-block have a colon (:) at their end.


Example –

if a > b :

      print(“a is bigger than b”) #block inside if

else :

      print(“b is bigger than a” #block inside else


def sayHello(name):

      print(“Hello Mr. “, name) #block inside function

      print(“How are You?”)

      print(“What about your exam!”)


Note: Unnecessarily indent a statement, raise error in Python.

Python Program Structure
bottom of page