What is Statement in Python With Example?

In Python, a statement is a unit of code that performs an action or carries out a specific task. It is a complete instruction that the Python interpreter can execute. Statements can include assignments, control flow structures, function calls, and more. Here’s an example of different types of statements in Python:

1. Assignment Statement:

x = 10Code language: Python (python)

2. Conditional Statement (if-else):

if x > 0:
    print("Positive number")
else:
    print("Negative number")
Code language: Python (python)

3. Looping Statement (for loop):

for i in range(5):
    print(i)Code language: Python (python)

4. Function Call Statement:

result = calculate_sum(5, 7)Code language: Python (python)

5. Return Statement (inside a function):

def calculate_sum(a, b):
    return a + bCode language: Python (python)

6. Import Statement:

import mathCode language: Python (python)

7. Exception Handling Statement (try-except):

try:
    result = 10 / 0
except ZeroDivisionError:
    print("Error: Division by zero")Code language: Python (python)

These are just a few examples of statements in Python. Python programs are typically composed of multiple statements that work together to achieve a desired outcome.

What are the 4 types statements in Python?


In Python, there are four main types of statements:

1. Assignment Statements:

Assignment statements are used to assign a value to a variable. They have the following syntax:

variable = expressionCode language: Python (python)

For example:

x = 5Code language: Python (python)

2. Control Flow Statements:

Control flow statements control the flow of execution in a program. They include:

2.1 Conditional Statements (if, elif, else):

if condition:
    # code block executed if condition is true
elif condition:
    # code block executed if condition is true
else:
    # code block executed if none of the above conditions are true
Code language: Python (python)

2.2 Looping Statements (for, while):

  • For loop:
for variable in iterable:
    # code block executed for each item in the iterableCode language: Python (python)
  • While loop:
while condition:
    # code block executed repeatedly as long as the condition is trueCode language: Python (python)

2.3. Break and Continue statements:

  • Break statement:
breakCode language: Python (python)

It terminates the execution of the nearest enclosing loop or the switch statement.

  • Continue statement:
continueCode language: Python (python)

3. Function Definition Statements:

Function definition statements define new functions with a specific name and set of parameters. They have the following syntax:

def function_name(parameters):
    # code block defining the function
    return valueCode language: Python (python)

For example:

def calculate_sum(a, b):
    return a + bCode language: Python (python)

4. Import Statements:

Import statements are used to bring external modules or functions into your Python program. They have the following syntax:

import module_nameCode language: Python (python)

For example:

import mathCode language: Python (python)

These four types of statements are fundamental in Python and allow you to perform various operations, control program flow, define functions, and utilize external libraries.

Difference between statement and expression in Python with example?

In Python, there is a distinction between statements and expressions based on their purpose and the value they produce. Here’s a comparison between statements and expressions:

Statement:

  • A statement is a unit of code that performs an action or carries out a specific task.
  • Statements do not produce a value or result.
  • Statements are used for control flow, assignment, function definition, import, etc.
  • Statements are typically executed for their side effects.
  • Examples of statements:
x = 5
if x > 0:
    print("Positive number")
def calculate_sum(a, b):
    return a + bCode language: Python (python)

Expression:

  • An expression is a combination of values, variables, operators, and function calls that evaluates to a value.
  • Expressions produce a value as their result.
  • Expressions can be used wherever a value is expected, such as in assignments, function arguments, and within other expressions.
  • Examples of expressions:
5 + 3
x > 0
calculate_sum(5, 7)Code language: Python (python)

Here’s an example that demonstrates the difference between a statement and an expression:

x = 5 + 3  # This is a statement (assignment statement)
y = x * 2  # This is a statement (assignment statement)
z = y > 10  # This is an expression (comparison expression)

print(x)  # Output: 8
print(y)  # Output: 16
print(z)  # Output: True
Code language: Python (python)

In the example above, x = 5 + 3 and y = x * 2 are assignment statements that perform calculations and assign values to variables. On the other hand, z = y > 10 is an expression that evaluates the comparison y > 10 and produces a boolean value (True in this case). The resulting value of the expression is then assigned to the variable z.

Read More;

  • Dmytro Iliushko

    I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

    View all posts

Leave a Comment