Sitemap
Tharindu Dhananjaya

Tharindu Dhananjaya

Software Engineer (Undergraduate)

Press enter or click to view image in full size

Python Operators and expressions

3 min readAug 25, 2023

--

Press enter or click to view image in full size

In Python, operators and expressions are fundamental concepts used for performing various operations on data. They allow you to manipulate values, variables, and other elements in your code. Let’s delve into Python operators and expressions:

Operators: Operators are symbols that represent actions or computations. They operate on operands (values or variables) and produce a result. Python supports a wide range of operators, categorized into different types:

1-Arithmetic Operators:

These operators perform basic mathematical operations like addition, subtraction, multiplication, etc.

Press enter or click to view image in full size

2-Assignment Operators:

These operators combine arithmetic operations with assignment. They update the value of a variable.

Press enter or click to view image in full size

3-Comparison Operators:

These operators compare two values and return a Boolean (True/False) result.

Press enter or click to view image in full size

4-Logical Operators:

Logical operators combine Boolean values and return a Boolean result.

Press enter or click to view image in full size

5-Bitwise Operators:

Bitwise operators perform operations on individual bits of integers.

Press enter or click to view image in full size

6-Identity Operators:

Identity operators compare the memory addresses of objects.

Press enter or click to view image in full size

7-Membership Operators:

Membership operators check if a value is present in a sequence.

Press enter or click to view image in full size

Expressions:

Get Tharindu Dhananjaya’s stories in your inbox

Join Medium for free to get updates from this writer.

An expression is a combination of values, variables, operators, and function calls that, when evaluated, produces a value. Expressions can be simple or complex, involving multiple levels of computation.

Press enter or click to view image in full size

In this example, '(x + y) * 2 + (x - y) / 3' is a complex expression. It consists of arithmetic operations, parentheses for grouping, and variables ‘x’and ‘y’.

Python follows operator precedence rules when evaluating expressions. Operators with higher precedence are evaluated before operators with lower precedence. Parentheses can be used to override the default precedence.

Understanding these concepts is crucial for writing effective Python code, performing calculations, making decisions, and working with data. Expressions are the building blocks of most programs, as they define how values are manipulated and transformed.

--

--