CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Python – Simple and Compound Statements

We write code blocks in Python and each code block contains sequence of statements. We classified these statements as simple and compound statements. Python program contains collection of these statements; assignments, expressions, computations, functions, loops etc.,. In this Article, I am going to explain; Simple and Compound statements in Python.

Simple Statements

The statements which are meant for simple operations and mostly written in a single logical line of code.

For example, assignment statements are simple statements.

x = 10

which means, we are assigning a value “10” to the variable “x”. This we call as simple statement.

The computation statements (expression statements) also we call simple statements; these statements will compute or calculate some expressions and return the results.

For example, x = (10 + 15) is an expression statement.

Other than Assignment and Expression statements; the statements below also we called as Simple Statements: These are the statements formed with Python keyword(s); some of them are break, continue, return and import.

break Statement – We use break statement, to bypass the execution of the statements which are defined after the break statement. The execution control will go to end of the Compound Statement. Usually we use this statement, within the Compound Statements.

continue statement – continue statement is used to skip the statements execution which are defined after this statement. The execution control will go to the beginning of the Compound Statement. These statements also usually use with the Compound Statements.

Have you noticed the difference between break & continue statements? Control execution will go to the beginning of the Compound Statement when we use continue; where as for break, the control execution will go to end of the Compound Statement.

return statement -We use return statements within the function to return from the function with or without a value.

import statement – To import code modules to current namespace, we use import statement. Usually we write these statements at the beginning of the Program code.

Lets’ look at compound statements.

Compound Statements

A compound statement is a statement comprise of group of statements. The compound statements are usually executes, when a condition satisfies or a code block is called directly or through a function call. Compound Statements are spread into multiple logical lines; but aligned them into a particular group.

Class definitions and Function definitions are Compound Statements. I have explained more about Classes & Functions in my previous Article “”; and I do not want to explain here it again.

Other Compound Statements we have are:

The conditional statement – The if statement

The statements which are grouped with in the Conditional Compound Statement (The if statement) are going to execute when the particular condition is satisfied.

Condition Loop Statements – The for statement AND The while statement

for statement is used to iterate through the elements of a sequence; where as the statements within the while statement are going to execute when the condition is satisfied.

Using while statement also we can iterate through the elements of a sequence; but we need to write additional code to do this; whereas for statement syntax by default supports this.

An Exception Handler – The try statement

The group of statements with-in try are block are going to execute when an exception occurred. I will explain more about this in a separate Article.

Putting all together the statements; the complete code looks like below:

Run the above code and observe the results.

These are the Simple and Compound Statements we mostly use to complete the Python program. We will discuss more about Python programming in my upcoming Articles.

Please share the feedback through comments.

/Shijit/

Python – Simple and Compound Statements

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top