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.
a = 25 # a statement a = a + 20 # an expression print(a)
In Python, an expression can contain identifiers, operands, and operators.
In Python, an identifier is a name that is used to define and identify any class, variable, or function.
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.
x = 120 + 15 # in this program 120 and 15 are constants but x is a variable. print("The value of x is: ", x)
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:
|+||a + b||Addition of a and b|
|-||a - b||Subtraction of a and b|
|*||a * b||Multiplication of a and b|
|/||a / b||Division of a and b|
|//||a // b||The quotient of when a divided by b|
|%||a %b||Remainder when a is divided by b|
|**||a **b||Exponent a to the power of b|
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)
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.
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)
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.
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)
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:
>, >=, <=, <
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)
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:
|And||a and b||This expression returns true if both a and b are true, else it will return false.|
|Or||a or b||This expression returns true if any one condition is true.|
|Not||Not a||This expression returns true if condition a is false.|
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)
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.
//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)
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.
x = 21 y = 35 result = x + (y << 2) print("Result obtained : ", result)
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.
# statement 1 print('Hello world') # statement 2 x = 200 # statement 3 print(x)
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:
# two statements in a single l = 100; b = 5 # statement 3 print('Area of rectangle:', l * b) # Output Area of rectangle: 500
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
As a name says,, multiline statements are used over multiple lines and we can use multi-line statements using line continuation characters.
# program to show the use of multi-line statements in python sum = 10 + 20 + \ 30 + 40 + \ 50 + 60 + 70 print(sum)
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.
x =15 # right hand side of = is a expression statement # x = x + 10 is a complete statement x = x + 10 print(x)
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
|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=22||Example: a= 2+6|