Statement
What is a Statement in Python?
In Python, a statement
refers to an instruction which the Python interpreter can execute. As a programming language, Python is an unambiguous set of instructions for completing a task, and each of these instructions is known as a statement.
Various types of Python statements include:
- Assignment Statements
- Conditional Statements
- Loop Statements
- Import Statements
Assignment Statements
Assignment statements
are used to (re)bind names to values and also modify attributes or items of mutable objects. In simple terms, they are used to define a variable and associate a value with it.
1x = 10 # x is a name which is bound to the value 10
Conditional Statements
Conditional statements
, also known as selection statements, allow the execution of a statement sequence based on the evaluation of a condition. The if
, elif
and else
statements are the most common types of conditional statements in Python.
1if x > 0: # If the condition (x > 0) is true, then execute the indented statement
2 print("Positive number")
Loop Statements
Loop statements
are used to repeatedly execute a block of statements as long as the condition remains true. for
and while
are the most common loop statements.
1for i in range(5): # For each number i in range 0-4, print i
2 print(i)
Import Statements
The import statement
is used to include external modules into a Python script. When included, the functions and classes defined in the module can be used in the current script.
1import math # math module is included. Now we can use functions defined in this module.
Note
In Python, the end of a statement is marked by a newline character. However, we can make a statement extend over multiple lines with the line continuation character (\
), or by using parentheses, brackets, or braces.