When multiple statements grouped into a single statement What is it known as?

In this tutorial, you will learn Python statements. Also, you will learn simple statements and compound statements.

Table of contents

  • Multi-Line Statements
  • Python Compound Statements
  • Simple Statements
    • Expression statements
    • The pass statement
    • The del statement
    • The return statement
    • The import statement
    • The continue and break statement

What is a statement in Python?

A statement is an instruction that a Python interpreter can execute. So, in simple words, we can say anything written in Python is a statement.

Python statement ends with the token NEWLINE character. It means each line in a Python script is a statement.

For example,

Hello
20
7 is an assignment statement. where
Hello
20
8 is a variable name and 10 is its value. There are other kinds of statements such as
Hello
20
9 statement,
# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50
0 statement,
# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50
1 statement, etc., we will learn them in the following lessons.

There are mainly four types of statements in Python, print statements, Assignment statements, Conditional statements, Looping statements.

The print and assignment statements are commonly used. The result of a print statement is a value. Assignment statements don’t produce a result it just assigns a value to the operand on its left side.

A Python script usually contains a sequence of statements. If there is more than one statement, the result appears only one time when all statements execute.

Example

# statement 1
print('Hello')

# statement 2
x = 20

# statement 3
print(x)

Output

Hello
20

As you can see, we have used three statements in our program. Also, we have added the comments in our code. In Python, we use the hash (

# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50
2) symbol to start writing a comment. In Python, comments describe what code is doing so other people can understand it.

We can add multiple statements on a single line separated by semicolons, as follows:

# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50

Multi-Line Statements

Python statement ends with the token NEWLINE character. But we can extend the statement over multiple lines using line continuation character (

# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50
3). This is known as an explicit continuation.

Example

addition = 10 + 20 + \
           30 + 40 + \
           50 + 60 + 70
print(addition)
# Output: 280

Implicit continuation:

We can use parentheses

# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50
4 to write a multi-line statement. We can add a line continuation statement inside it. Whatever we add inside a parentheses
# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50
4 will treat as a single statement even it is placed on multiple lines.

Example:

addition = (10 + 20 +
            30 + 40 +
            50 + 60 + 70)
print(addition)
# Output: 280

As you see, we have removed the the line continuation character (

# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50
3) if we are using the parentheses
# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50
4.

We can use square brackets

# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50
8 to create a list. Then, if required, we can place each list item on a single line for better readability.

Same as square brackets, we can use the curly

# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50
9 to create a dictionary with every key-value pair on a new line for better readability.

Example:

# list of strings
names = ['Emma',
         'Kelly',
         'Jessa']
print(names)

# dictionary name as a key and mark as a value
# string:int
students = {'Emma': 70,
            'Kelly': 65,
            'Jessa': 75}
print(students)

Output:

['Emma', 'Kelly', 'Jessa']
{'Emma': 70, 'Kelly': 65, 'Jessa': 75}

Python Compound Statements

Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way.

The compound statement includes the conditional and loop statement.

  • Hello
    20
    9 statement: It is a control flow statement that will execute statements under it if the condition is true. Also kown as a conditional statement.
  • # two statements in a single
    l = 10; b = 5
    
    # statement 3
    print('Area of rectangle:', l * b)
    
    # Output Area of rectangle: 50
    1 statements: The while loop statement repeatedly executes a code block while a particular condition is true. Also known as a looping statement.
  • # two statements in a single
    l = 10; b = 5
    
    # statement 3
    print('Area of rectangle:', l * b)
    
    # Output Area of rectangle: 50
    0 statement: Using for loop statement, we can iterate any sequence or iterable variable. The sequence can be string, list, dictionary, set, or tuple. Also known as a looping statement.
  • addition = 10 + 20 + \
               30 + 40 + \
               50 + 60 + 70
    print(addition)
    # Output: 280
    
    3 statement: specifies exception handlers.
  • addition = 10 + 20 + \
               30 + 40 + \
               50 + 60 + 70
    print(addition)
    # Output: 280
    
    4 statement: Used to cleanup code for a group of statements, while the with statement allows the execution of initialization and finalization code around a block of code.

Simple Statements

Apart from the declaration and calculation statements, Python has various simple statements for a specific purpose. Let’s see them one by one.

If you are an absolute beginner, you can move to the other beginner tutorials and then come back to this section.

Expression statements

Expression statements are used to compute and write a value. An expression statement evaluates the expression list and calculates the value.

To understand this, you need to understand an expression is in Python.

An expression is a combination of values, variables, and operators. A single value all by itself is considered an expression. Following are all legal expressions (assuming that the variable

addition = 10 + 20 + \
           30 + 40 + \
           50 + 60 + 70
print(addition)
# Output: 280
5 has been assigned a value):

5
x
x + 20

If your type the expression in an interactive python shell, you will get the result.

So here

addition = 10 + 20 + \
           30 + 40 + \
           50 + 60 + 70
print(addition)
# Output: 280
6 is the expression statement which computes the final value if we assume variable x has been assigned a value (10). So final value of the expression will become 30.

But in a script, an expression all by itself doesn’t do anything! So we mostly assign an expression to a variable, which becomes a statement for an interpreter to execute.

Example:

x = 5
# right hand side of = is a expression statement

# x = x + 10 is a complete statement
x = x + 10

The addition = 10 + 20 + \ 30 + 40 + \ 50 + 60 + 70 print(addition) # Output: 280 7 statement

addition = 10 + 20 + \
           30 + 40 + \
           50 + 60 + 70
print(addition)
# Output: 280
7 is a null operation. Nothing happens when it executes. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed.

For example, you created a function for future releases, so you don’t want to write a code now. In such cases, we can use a

addition = 10 + 20 + \
           30 + 40 + \
           50 + 60 + 70
print(addition)
# Output: 280
7 statement.

Example:

# create a function
def fun1(arg):
    pass  # a function that does nothing (yet)

The addition = (10 + 20 + 30 + 40 + 50 + 60 + 70) print(addition) # Output: 2800 statement

The Python

addition = (10 + 20 +
            30 + 40 +
            50 + 60 + 70)
print(addition)
# Output: 280
0 statement is used to delete objects/variables.

Syntax:

Hello
20
0

The

addition = (10 + 20 +
            30 + 40 +
            50 + 60 + 70)
print(addition)
# Output: 280
2 contains the variable to delete separated by a comma. Once the variable is deleted, we can’t access it.

Example:

Hello
20
1

Output:

Hello
20
2

The addition = (10 + 20 + 30 + 40 + 50 + 60 + 70) print(addition) # Output: 2803 statement

We create a function in Python to perform a specific task. The function can return a value that is nothing but an output of function execution.

Using a

addition = (10 + 20 +
            30 + 40 +
            50 + 60 + 70)
print(addition)
# Output: 280
3 statement, we can return a value from a function when called.

Example:

Hello
20
3

Output:

Hello
20
4

The addition = (10 + 20 + 30 + 40 + 50 + 60 + 70) print(addition) # Output: 2805 statement

The import statement is used to import modules. We can also import individual classes from a module.

Python has a huge list of built-in modules which we can use in our code. For example, we can use the built-in module DateTime to work with date and time.

What is multi line statement?

In this type of multi-line statement, we will be using the line continuation character (\) to split a statement into multiple lines.

What is multiple statements found while compiling a single statement in Python?

Semicolons are used to separate numerous statements on a single line (;).

Can multiple Python statements be written on a single line?

These statements can very well be written in one line by putting semicolon in between. However, this practice is not allowed if there is a nested block of statements.

What does Python use to determine the grouping of statements?

Now you know why: indentation is used to define compound statements or blocks. In a Python program, contiguous statements that are indented to the same level are considered to be part of the same block. Here, all the statements at the matching indentation level (lines 2 to 5) are considered part of the same block.