×

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

Python: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

How to assign values to variables in Python and other languages?

Python makes it simple to construct variables. The value to be stored in the variable should then be written after a suitable name for the variable and the equality sign....

3 minutes read.

Enumerate() Function in Python

The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each...

3 minutes read.

Python Errors and exceptions

Exceptions and errors are the obstacles a programmer constantly faces while writing a program. Firstly, we need to understand what are errors and exceptions and the difference between these two...

6 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.

Python Built-in Functions

Python Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow: Methods Explaining abs() The abs() function returns...

6 minutes read.

Cube Root in Python

In general, the cube is a three-dimensional solid figure that has 6 square faces. It is also called a geometrical shape with six equal faces, eight vertices, and twelve edges....

3 minutes read.

Append key Value to Dictionary in Python

The Python dictionary is one of the built-in data types. Elements of dictionaries are key-value pairs. In Python, there are numerous ways to add dictionaries. Let's examine some of the...

9 minutes read.

Python Set issubset() method

Python Set issubset() method The set.issubset() method in Python returns True if all items in the set exists in the specified set, otherwise it returns False. Syntax set.issubset(set1) Parameter set- This parameter represents the set to check whether...

2 minutes read.

Python Decorator

Python Decorator: A Decorator is an interesting feature of Python that helps the user design patterns and insert a new functionality to an existing object without making any modifications in...

6 minutes read.

Python try catch exception

The try-except proclamation can deal with exceptions. Exceptions might happen when you run a program. Exceptions are blunders that occur during the execution of the program. Python won't educate you regarding...

3 minutes read.

Arithmetic Expressions in Python

What is Python Expression? Expressions are collections of operands and operators. Python expressions are translated by the Python interpreter into some value or outcome. In Python, an expression is made up...

13 minutes read.

Application to Search Installed Application using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python Bitwise Operators

When used with variables and objects in an expression, operators are tokens that carry out calculations. The variables and items to which the computation is applied are known as operands....

5 minutes read.

Python tuple() function

Python tuple() function The tuple() function in Python creates a tuple object. Syntax tuple([iterable]) Parameter Iterable: This parameter represents a sequence, collection or an iterator object. Return This function returns a tuple object. Example 1 # Python Program explaining #...

1 minute read.

Sys Module in Python

What are Modules? The Modules are the kind of files that contain Python statements and definitions. The module is known by the name of the file followed by the suffix “.py”....

5 minutes read.

Read JSON File in Python

JSON refers to the JavaScript Object Notation. It is a Data format used for representing structured data and it is used to transfer and store data. In JSON format, the...

5 minutes read.

Python NewLine

In python, a new line character denoted by "\n" is known as an escape character or escape sequence, and it will force the cursor to change its position to the next...

6 minutes read.

Python open() function

Python open() function The open() function in Python opens a file and returns a corresponding file object. If the file cannot be opened, an OSError is raised. Syntax open(file, mode='r', buffering=1, encoding=None, errors=None, newline=None, closefd=True, opener=None) Parameter file It represents the path and name of the file mode...

2 minutes read.