What are the different parts of Python Program Structure?
Let us understand the basics of Python Program Structure. Logics can be simple to very complex and python can handle them all. Behind the scenes Python has to do a lot to make things possible and as a programmer you will have to make sure Python understands what you want to do.
Different Components of Python Program
Python Program Structure may consist of all or any combination of follwoing components:
- Statements
- Expressions
- Functions
- Block Structures, and
- Coments
Statements
Statement is a programming instruction in Python that does something. For example:
print("Hello") age=20 total = total +20
The above example shows three Python statements, written on separate lines. The statements are the most use component of Python Program structure.
Expressions
Expresson is any combination of variables, operators and literals that can be evaluated to represent some value. Below is the list of example to show some valid expressions in Python:
27 Val=18.36 b=a+10 Val2=(6+15)/5
In the example above, all the statements are valid examples of expressions.
Functions
Function is a block of code, that has a name and can be executed (reused) as many times without writing the same code again and again. To execute function you have to simply specify its name, where needed. Functions are used most often a Python Program structure and these can be library functions or user defined functions as well.
def welcome(): print("Welcome to Edukers") print("We provide Skill oriented Courses") print("Website: WWW.edukers.in") #Calling function welcome()
In the code above, welcome() is the function, that can be called as many times just by specifying its name, as shown above on line number 7.
Also observe the statements inside function (All three print() statements) are indented towards right (usually by 4 spaces). This makes all the statements inside function a code block (explained more in block structures, below) belonging to the function names “welcome()”.
Block Structures
First you need to nderstand – what is indentation?
Indentation refers to the spaces we give at the beginning of the line of code, in a Python program. In other languages it is done to enhance the readability, but in Python it is a way to represent block of code or containership.
Without indentation, Python Program structure is imcomplete. All the lines having same number of spaces before the beginning of the line of code, belong to same code block. These group of individual statements belonging to the same indent level (having same number of spaces at the beginning) make a single code-block also known as suit in Python.
In Python following are implemented as code blocks:
- Functions
- Conditional statements
- Looping Constructs
- Classes, etc.
Comments
Comments play an important role in a Python Program structure. Comments are a way to describe something in your program that you want Python to ignore it, or should not consider as part of program.
institute="Edukers" #This is a single line comment #This is also single line comment ''' This is a multi-line comment '''
There are two types of Comments:
- Single line Comments
- In Python Single line comments must begin with a pound (#) sign. Python interpreter ignores what follows pound (#) sign till the end of the line.
- Multi-line comments
- Multi Line comments must begin with and end with tripple-quotes