# What is Expression in Python | Types, Example

An expression in Python is a statement (or logical line) that consists of an operand and operator.

The operand in any expression can be a constant, variable, or a call to the built-in or user-defined function that returns the value, etc.

Any operator, such as arithmetic assignment, relational, etc may be in the expression.

The combination of variables and constants, along with Python operators forms the complex expressions.

A simple example of expression is 2 + 5. Another simplest expression is the assignment expression in which we assign the value on the right-hand side to the variable on the left-hand side of the = operator. For example:

```x = 20
y = 10.50
```

The above two statements are assignment expressions that assign the literal value 20 to the variable x and 10.50 to the variable y.

A Python interpreter evaluates the result of expressions according to operators precedence rules, which determine the order in which the interpreter evaluates the operators in the expression.

For example, the below expression does not calculate correctly the average marks of five subjects.

```Average = 70 + 89 + 90 + 88 + 99 / 5
print(Average) # Output: 356.8
```

Python interpreter will perform the first operation (99 / 5). Then, it will perform the next addition that yields an incorrect answer. By using the parentheses, we can solve this problem as:

```Average = (70 + 89 + 90 + 88 + 99) / 5
print(Average) # Output: 87.2
```

## Types of Expressions in Python

In Python language, there are four types of expressions that are as:

• Arithmetic expression
• Assignment expression
• Relational/Conditional expression
• Logical expression

Let us have a look at each expression one by one with the help of example programs.

### Arithmetic Expression

In arithmetic expression, we perform the mathematical calculation using arithmetic operators like +, -, *, /, etc. For example:

```# Program to calculate the sum of two numbers.
num1 = 20
num2 = 40
sum = num1 + num2 # An arithmetic expression.
print("Sum of two numbers is ", sum)
```

When we will execute the above example code, it will generate the following output on the console.

```Output:
Sum of two numbers is 60
```

Here, sum = num1 + num2 is an arithmetic expression that does a mathematical calculation using + operator for two operands num1 and num2. Also, num1 = 20, and num2 = 40 are two assignment expressions.

### Assignment Expression

In the assignment expression, we assign a value on the right side to the variable on the left side of the equal operator. For example:

```p = 20
q = 40
```

The preceding two lines are the assignment expressions that assign the value 20 to the variable p and 40 to the variable q.

### Relational/Conditional Expression

The relational expression compares two operators using relational operators, such as >, <, >=, <=, etc. It is also called conditional expression.

Generally, we use the conditional expression in the branching (if, if…else, etc.) and in the looping (while) statement. A relational expression always produces two boolean values, either true or false depending on the specified condition.

Let’s take some simple programs based on the conditional expressions.

Example 1:

```# Python program to find greater between three numbers.
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))

if num1 > num2 and num1 > num3:
print(num1, 'is a greater number among the three numbers.')
elif num2 > num1 and num2 > num3:
print(num2, 'is a greater number among the three numbers.')
else:
print(num3, 'is a greater number among the three numbers.')
```
```Output:
Enter the first number: 20
Enter the second number: 10
Enter the third number: 30
30 is a greater number among the three numbers.
```

In this example, when you will execute the program, the interpreter will prompt to enter three numbers one by one. For example, if you enter the first number as 20, the second number as 10, and the third number as 30, then it will print a message “30 is a greater number among the three numbers”.

Example 2:

```# Python program to check whether you can vote or not.
Age = int(input("Enter your age: "))
if Age >= 18:
print('You can cast vote.')
else:
print('You cannot cast vote.')
```
```Output:
You can cast vote.
```

In this example, when you will execute the program, the interpreter will prompt to enter your age. For example, if you enter the age as 20, then it will display a message “You can cast vote”. Otherwise, it will display “You cannot cast vote”.

### Logical Expression

The logical expression uses the logical operators, such as and, or, and not. It produces a boolean value, either true or false value. For example:

```p = 10
q = 15
r = 5
s = 20
result = p > q and r > s
print("Logical expression (p > q and r > s) returns ", result)
```
```Output:
Logical expression (p > q and r > s) returns  False
```

In this example, the logical expression combines two conditional expressions with a logical and operator. In the expression, the result of (p > q) is False and the result of (r > s) is False. So, the final result is False.

In this tutorial, we have covered about the expression in Python and its types with examples. Hope that you will have understood the basic points of Python expressions and practiced all example programs.