An expression is a combination of operators and operands that is interpreted to produce some other value. In any programming language, an expression is evaluated as per the precedence of its operators. So that if there is more than one operator in an expression, their precedence decides which operation will be performed first. We have many different types of expressions in Python. Let’s discuss all types along with some exemplar codes :
1. Constant Expressions: These are the expressions that have constant values only.
Example:
2. Arithmetic Expressions: An arithmetic expression is a combination of numeric values, operators, and sometimes parenthesis. The result of this type of expression is also a numeric value. The operators used in these expressions are arithmetic operators like addition, subtraction, etc. Here are some arithmetic operators in Python:
Operators |
Syntax |
Functioning |
+ |
x + y |
Addition |
– |
x – y |
Subtraction |
* |
x * y |
Multiplication |
/ |
x / y |
Division |
// |
x // y |
Quotient |
% |
x % y |
Remainder |
** |
x ** y |
Exponentiation |
Example:
Let’s see an exemplar code of arithmetic expressions in Python :
Python3
x = 40
y = 12
add = x + y
sub = x - y
pro = x * y
div = x / y
print (add)
print (sub)
print (pro)
print (div)
|
Output
52
28
480
3.3333333333333335
3. Integral Expressions: These are the kind of expressions that produce only integer results after all computations and type conversions.
Example:
Python3
a = 13
b = 12.0
c = a + int (b)
print (c)
|
4. Floating Expressions: These are the kind of expressions which produce floating point numbers as result after all computations and type conversions.
Example:
Python3
a = 13
b = 5
c = a / b
print (c)
|
5. Relational Expressions: In these types of expressions, arithmetic expressions are written on both sides of relational operator (> , < , >= , <=). Those arithmetic expressions are evaluated first, and then compared as per relational operator and produce a boolean output in the end. These expressions are also called Boolean expressions.
Example:
Python3
a = 21
b = 13
c = 40
d = 37
p = (a + b) > = (c - d)
print (p)
|
6. Logical Expressions: These are kinds of expressions that result in either True or False. It basically specifies one or more conditions. For example, (10 == 9) is a condition if 10 is equal to 9. As we know it is not correct, so it will return False. Studying logical expressions, we also come across some logical operators which can be seen in logical expressions most often. Here are some logical operators in Python:
Operator |
Syntax |
Functioning |
and |
P and Q |
It returns true if both P and Q are true otherwise returns false |
or |
P or Q |
It returns true if at least one of P and Q is true |
not |
not P |
It returns true if condition P is false |
Example:
Let’s have a look at an exemplar code :
Python3
P = ( 10 = = 9 )
Q = ( 7 > 5 )
R = P and Q
S = P or Q
T = not P
print (R)
print (S)
print (T)
|
7. Bitwise Expressions: These are the kind of expressions in which computations are performed at bit level.
Example:
Python3
a = 12
x = a >> 2
y = a << 1
print (x, y)
|
8. Combinational Expressions: We can also use different types of expressions in a single expression, and that will be termed as combinational expressions.
Example:
Python3
a = 16
b = 12
c = a + (b >> 1 )
print (c)
|
But when we combine different types of expressions or use multiple operators in a single expression, operator precedence comes into play.
Multiple operators in expression (Operator Precedence)
It’s a quite simple process to get the result of an expression if there is only one operator in an expression. But if there is more than one operator in an expression, it may give different results on basis of the order of operators executed. To sort out these confusions, the operator precedence is defined. Operator Precedence simply defines the priority of operators that which operator is to be executed first. Here we see the operator precedence in Python, where the operator higher in the list has more precedence or priority:
Precedence |
Name |
Operator |
1 |
Parenthesis |
( ) [ ] { } |
2 |
Exponentiation |
** |
3 |
Unary plus or minus, complement |
-a , +a , ~a |
4 |
Multiply, Divide, Modulo |
/ * // % |
5 |
Addition & Subtraction |
+ – |
6 |
Shift Operators |
>> << |
7 |
Bitwise AND |
& |
8 |
Bitwise XOR |
^ |
9 |
Bitwise OR |
| |
10 |
Comparison Operators |
>= <= > < |
11 |
Equality Operators |
== != |
12 |
Assignment Operators |
= += -= /= *= |
13 |
Identity and membership operators |
is, is not, in, not in |
14 |
Logical Operators |
and, or, not |
So, if we have more than one operator in an expression, it is evaluated as per operator precedence. For example, if we have the expression “10 + 3 * 4”. Going without precedence it could have given two different outputs 22 or 52. But now looking at operator precedence, it must yield 22. Let’s discuss this with the help of a Python program:
Python3
a = 10 + 3 * 4
print (a)
b = ( 10 + 3 ) * 4
print (b)
c = 10 + ( 3 * 4 )
print (c)
|
Hence, operator precedence plays an important role in the evaluation of a Python expression.
Similar Reads
Expressions in Math
Expressions in math are combinations of numbers, variables, operators, and sometimes parentheses that represent a particular value. An expression is a statement involving at least two different numbers or variables and at least one operation, such as addition, subtraction, multiplication, division,
7 min read
Comprehensions in Python
Comprehensions in Python provide a concise and efficient way to create new sequences from existing ones. They enhance code readability and reduce the need for lengthy loops. Python supports four types of comprehensions: List ComprehensionsDictionary ComprehensionsSet ComprehensionsGenerator Comprehe
4 min read
Overuse of lambda expressions in Python
What are lambda expressions? A lambda expression is a special syntax to create functions without names. These functions are called lambda functions. These lambda functions can have any number of arguments but only one expression along with an implicit return statement. Lambda expressions return func
8 min read
Python Naming Conventions
Python, known for its simplicity and readability, places a strong emphasis on writing clean and maintainable code. One of the key aspects contributing to this readability is adhering to Python Naming Conventions. In this article, we'll delve into the specifics of Python Naming Conventions, covering
4 min read
Python - Itertools.compress()
Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Co
2 min read
Solving Expressions with Exponents
Solving expressions with exponents involves understanding and applying rules such as the product of powers, quotient of powers, and power of a power. For instance, am à an = am+n and (am)n = amn. These properties simplify complex calculations. For example, (23)2 à 2â4 simplifies to 26â4 = 22 = 4. Un
5 min read
Python input() Function
Python input() function is used to take user input. By default, it returns the user input in form of a string. input() Function Syntax: input(prompt)prompt [optional]: any string value to display as input message Ex: input("What is your name? ") Returns: Return a string value as input by the user.
4 min read
What are the Logical Expressions in Sympy?
SymPy is a symbolic mathematics Python package. Its goal is to develop into a completely featured computer algebra system while keeping the code as basic as possible to make it understandable and extendable. The package is entirely written in python language. Logical expressions in sympy are express
7 min read
Python Functions
Python Functions is a block of statements that return the specific task. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it ov
11 min read
Algebraic Expressions Practice Questions
Algebraic expressions are fundamental components of algebra that represent quantities and relationships using variables, constants, and operations. They form the basis for solving equations and understanding mathematical relationships. In this article, we will learn what algebraic expressions are an
5 min read