×

Difference between Expression and Statement in Python

What is an expression in Python?

Expression is a combination of operands and operators. Expression helps us to produce some other values.

In the python programming language,  expressions produce some other value and result after being interpreted with the help of a python interpreter.

Suppose we have a=a+20. In this expression 20 is a value that will be added to the variable a, and after the addition, we will get the result and it will be assigned to the variable a.

Example:

a = 25          # a statement
a = a + 20      # an expression
print(a)

Output:

45

In Python, an expression can contain identifiers, operands, and operators.

Identifiers

In Python, an identifier is a  name that is used to define and identify any class, variable, or function.

Operands

Operands are known as the object of the operator in Python. On the other hand, the operator is a type of symbol that performs the arithmetic or logical operation on the operands.

Types of expressions in Python

In Python programming language, we have different types of expressions:

1. Constant expressions

A constant expression is a type of expression that contains only values. In Python, the constant expression operator is always constant. The constant can not be changed after the initialization of the value.

Example:

x = 120 + 15
# in this program 120 and 15 are constants but x is a variable.
print("The value of x is: ", x)

Output:

The value of x is: 135

2. Arithmetic expressions

Arithmetic expressions are used to perform the mathematical operations with numeric values in Python.

There are various operators in the arithmetic expressions that are used in Python:

OperatorsSyntaxWorking
+a + bAddition of a and b
-a - bSubtraction of a and b
*a * bMultiplication of a and b  
/a / bDivision of  a and b
//a // bThe quotient of when  a divided by  b
%a %bRemainder when a is divided by b
**a **bExponent a to the power of b

Example:

x = 10
y = 5
addition = x + y
subtraction = x - y
product = x * y
division = x / y
power = x**y


print("The addition of x and y is: ", addition)
print("The subtraction between x and y is: ", subtraction)
print("The multiplication of x and y is: ", product)
print("The division of x and y is: ", division)
print("x to the power y is: ", power)

Output:

The addition of x and y is: 15
The subtraction between x and y is: 5
The multiplication of x and y is: 50
The division of x and y is: 2.0
x to the power y is: 100000

3. Integral expressions

Integral expressions are used to produce the integer result after the implementation of all the automatic and exciplit type conversions.

In other words, it is used for computation and type conversion that always produces an integer value as a result in Python.

Example:

x = 10      # an integer value
y = 5.0     # a floating point value


# we need to convert the floating-point number into an integer or vice versa for addition.
result = x + int(y)


print("The addition of x and y is: ", result)

Output:

The addition of x and y is: 15

4. Floating expressions

Floating expressions are used to produce the float value result after the implementation of all the automatic and exciplit type conversions.

In other words, it is used for computation and type conversion that always produces a float value as a result.

Example:

x = 20      # an integer value
y = 5.0     # a floating point value
# we need to convert the integer number into a floating-point number or vice versa for addition
result = float(x) + y
print("The sum of x and y is: ", result)

Output:

The sum of x and y is: 25.0

5. Relational expressions

In Python, a relational expression is a combination of more than two arithmetic expressions joined using the relational operation. It will tell if the overall expression result is either true or false.

In Python, we have four types of relational expressions:

>, >=, <=, <

Example:

10 + 15>20
a = 25
b = 14
c = 48
d = 45


# The expression checks if the sum of (a and b) is the same as the difference of (c and d).
result = (a + b) == (c - d)
print("Type:", type(result))
print("The result of the expression is: ", result)

Output:

Type: <class 'bool'>
The result of the expression is: False

6. Logical expressions

A logical expression is used to perform logical computation in the Python programming language. In other words, logical expression is used to perform the logical computation and provide the overall result in the form of true and false.

There are three types of logical expressions in Python:

OperatorSyntaxWorking
Anda and bThis expression returns true if both a and b are true, else it will return false.
Ora or bThis expression returns true if any one condition is true.
NotNot aThis expression returns true if condition a is false.

Example:

from operator import and_
x = (100 == 90)
y = (71 > 35)


and_result = x and y
or_result = x or y
not_x = not x


print("The result of x and y is: ", and_result)
print("The result of x or y is: ", or_result)
print("The not of x is: ", not_x)

Output:

The result of x and y is: False
The result of x or y is: True
The not of x is: True

7. Bitwise expressions

Bitwise expression is an expression in which the operation or computation is performed at the bit level. In other words, the expressions who work at the bit level are known as bitwise expression.

The bitwise expression also contains the bitwise operator. Let's understand by an example.

Example:

//program to show bitwise operation in python


x = 25
left_shift = x << 1
right_shift = x >> 1


print("One right shift of x results: ", right_shift)
print("One left shift of x results: ", left_shift)

Output:

One right shift of x results: 12
One left shift of x results: 50

8. Combination expressions

A combination expression in python is a type of expression that can contain single or multiple expressions that give the result in the form of an integer or Boolean. It all depends on the expression that is involved.

Example:

x = 21
y = 35


result = x + (y << 2)


print("Result obtained : ", result)

Output:

Result obtained : 161

What is a statement in Python?

In Python, the statement is an instruction that a python interpreter can execute. In other words, the statement can be anything written in Python language. In the Python program, statement always end with the token NEWLINE character. It shows that each line in a Python program is a statement that can be executed with the help of a python interpreter.

Example:

# statement 1
print('Hello world')


# statement 2
x = 200


# statement 3
print(x)

Output:

Hello world
200

We can also add multiple statements in a single line with the help of semicolons in Python. Let’s take an example to understand:

Example:

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


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


# Output Area of rectangle: 500

Output:

Output Area of rectangle: 500

Types of statements in Python:

There are different types of statements in Python as follows:

  • Multi line statements
  • Expression statements
  • Continue statements and Break statements

Multi-line statements

As a name says,, multiline statements are used over multiple lines and we can use multi-line statements using line continuation characters.

Example:

# program to show the use of multi-line statements in python
sum = 10 + 20 + \
30 + 40 + \
50 + 60 + 70
print(sum)

Output:

280

Expression statements

In Python, expression statements are used to compute and write a value. In other words, an expression statement is used to evaluate the expression list and calculates the value.

Or we can say that an expression statement is a combination of values, variables, and operators.

Example:

x =15
# right hand side of = is a expression statement
# x = x + 10 is a complete statement
x = x + 10
print(x)

Output:

25

The Continue and Break statements

  • Continue statement: It is used to skip the current iteration and move to the next iteration.
  • Break statement: It is used inside the loop so, we can exit the loop using a break statement.

Difference between Statement and Expression

StatementsExpressions
It is used to create a variable or for displaying a variable.It is used to produce the value after being interpreted by the python interpreter.
A statement is not evaluated for the results in Python.Anexpression is evaluated for the results in Python.
The execution of statement can change the state of a variable.It does not allow any change in the result.
A statement can be an expression.An expression is not a statement.
Example: a=22Example: a= 2+6

Related Topics

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.

XGBoost for Regression in Python

Regression problem results real values. Decision Trees and Linear Regression are regularly used regression algorithms and use some metrics involved in regression like mean squared error and root mean squared...

5 minutes read.

Loan calculator using Tkinter in Python

Tkinter: Tkinter, part of all common Python distributions, is the de facto method for creating Graphical User Interfaces (GUIs) in Python. The only framework included in the Python standard library is...

4 minutes read.

Working with files in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has an...

5 minutes read.

Python Key Error

What is an Error? Errors are nothing but problems in the program which occur in a program code, and this will stop the execution of the program. It is also called...

6 minutes read.

os.stat() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

Python vars() Function

Python vars() Function The vars() function in Python returns the __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute. Syntax vars([object]) Parameter object: This parameter represents any object with a __dict__attribute Return This function returns...

1 minute read.

Assertion error in python

In this article, we will discuss assertions in the python programming language. Assertion: Assertions are a way of telling a program to test a certain condition and trigger an error if the...

3 minutes read.

How to Define a Function in Python?

What is a Function? In programming, a piece of code that executes a specific task or a group of related operations is known as a function. What does Python Functions Do? If you...

6 minutes read.

Python Graph

Python Graph: In Computer Science and Mathematics, a Graph is a pictorial representation of a group of objects or elements where some elements are connected using the links. A graph...

5 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

Python Percentage Sign

In Python, the percentage sign significantly completes two things. They are: It goes about as a Modulo administrator. It helps in string organizing. Allow us to see every one of them plainly. Modulo operator: Like...

2 minutes read.

Syntax of Map function in Python

In this tutorial, we will understand what the map function in Python is meant. Further, we will see the syntax to be used for the same in python language. We...

3 minutes read.

Python round() function

Python round() function The round() function in Python returns a number rounded to ‘ndigits’ precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Syntax round(number[, ndigits]) Parameter Number: This parameter represents the...

1 minute read.

How to develop a game in python

Fun always makes a task interesting and easy to execute. Learning in the same theoretical way at some point reaches the boring spot. In this article, using some Python knowledge,...

13 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Python List count() method

Python List count() method The list.count() method returns the number of times x appears in the list. Syntax list.count(x) Parameter x: This parameter represents the value to search for and can contain any iterable (list, set, tuple, etc.) Return This method returns the...

1 minute read.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 minutes read.