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:
Python3
# Constant Expressions
x = 15 + 1.3
print(x)
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
# Arithmetic Expressions
x = 40
y = 12
add = x + y
sub = x - y
pro = x * y
div = x / y
print(add)
print(sub)
print(pro)
print(div)
Output52
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
# Integral Expressions
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
# Floating Expressions
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
# Relational Expressions
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)
# Logical Expressions
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
# Bitwise Expressions
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
# Combinational Expressions
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
# Multi-operator expression
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
Computer Fundamentals Tutorial This Computer Fundamentals Tutorial covers everything from basic to advanced concepts, including computer hardware, software, operating systems, peripherals, etc. Whether you're a beginner or an experienced professional, this tutorial will enhance your computer skills and take them to the next level
4 min read
Fundamental
Computer HardwareComputer hardware refers to the physical components of a computer that you can see and touch. These components work together to process input and deliver output based on user instructions. In this article, weâll explore the different types of computer hardware, their functions, and how they interact
10 min read
What is a Computer Software?Computer Software serves as the backbone of all digital devices and systems. It is an integral part of modern technology. Unlike hardware which comprises physical components, software is intangible and exists as a code written in programming language. This article focuses on discussing computer soft
8 min read
Central Processing Unit (CPU)The Central Processing Unit (CPU) is like the brain of a computer. Itâs the part that does most of the thinking, calculating, and decision-making to make your computer work. Whether youâre playing a game, typing a school assignment, or watching a video, the CPU is busy handling all the instructions
6 min read
Input DevicesInput devices are important parts of a computer that help us communicate with the system. These devices let us send data or commands to the computer, allowing it to process information and perform tasks. Simply put, an input device is any tool we use to give the computer instructions, whether it's t
11 min read
Output DevicesOutput devices are hardware that display or produce the results of a computer's processing. They convert digital data into formats we can see, hear, or touch. The output device may produce audio, video, printed paper or any other form of output. Output devices convert the computer data to human unde
9 min read
Memory
Computer MemoryMemory is the electronic storage space where a computer keeps the instructions and data it needs to access quickly. It's the place where information is stored for immediate use. Memory is an important component of a computer, as without it, the system wouldnât operate correctly. The computerâs opera
9 min read
What is a Storage Device? Definition, Types, ExamplesThe storage unit is a part of the computer system which is employed to store the information and instructions to be processed. A storage device is an integral part of the computer hardware which stores information/data to process the result of any computational work. Without a storage device, a comp
11 min read
Primary MemoryPrimary storage or memory is also known as the main memory, which is the part of the computer that stores current data, programs, and instructions. Primary storage is stored in the motherboard which results in the data from and to primary storage can be read and written at a very good pace.Need of P
4 min read
Secondary MemorySecondary memory, also known as secondary storage, refers to the storage devices and systems used to store data persistently, even when the computer is powered off. Unlike primary memory (RAM), which is fast and temporary, secondary memory is slower but offers much larger storage capacities. Some Ex
7 min read
Hard Disk Drive (HDD) Secondary MemoryPrimary memory, like RAM, is limited and volatile, losing data when power is off. Secondary memory solves this by providing large, permanent storage for data and programs.A hard disk drive (HDD) is a fixed storage device inside a computer that is used for long-term data storage. Unlike RAM, HDDs ret
11 min read
Application Software
MS Word Tutorial - Learn How to Use Microsoft Word (2025 Updated)Microsoft Word remains one of the most powerful word processing program in the world. First released in 1983, this word processing software has grown to serve approximately 750 million people every month. Also, MS Word occupies 4.1% of the market share for productivity software.With features like re
9 min read
MS Excel Tutorial - Learn Excel Online FreeExcel, one of the powerful spreadsheet programs for managing large datasets, performing calculations, and creating visualizations for data analysis. Developed and introduced by Microsoft in 1985, Excel is mostly used in analysis, data entry, accounting, and many more data-driven tasks.Now, if you ar
11 min read
What is a Web Browser and How does it Work?The web browser is an application software used to explore the World Wide Web (WWW). It acts as a platform that allows users to access information from the Internet by serving as an interface between the client (user) and the server. The browser sends requests to servers for web documents and servic
4 min read
What is a Excel SpreadsheetIf you search only about Spreadsheet then you will find it is a computer applicaion that save, display, and manipulate data in rows and columns. In the year 1978 first spreadsheet program is developed by Daniel Bricklin and Bob Frankston named as VisiCalc. Now, in the recent year Microsoft Excel is
12 min read
System Software
Programming Languages
C Programming Language TutorialC is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
Python Tutorial - Learn Python Programming LanguagePython is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Java TutorialJava is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
JavaScript TutorialJavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read