Python Operators and Expressions for Data Science
In Python, operators are special symbols that perform operations on values (operands). Expressions are combinations of values, variables, and operators that evaluate to a single value. Understanding these building blocks is crucial for manipulating data and building logic in data science.
Arithmetic Operators
These are used for mathematical operations. They form the basis of many data transformations and calculations.
Operator | Description | Example |
---|---|---|
Addition | 5 + 3 = 8 | |
Subtraction | 10 - 4 = 6 | |
Multiplication | 6 * 7 = 42 | |
/ | Division | 15 / 3 = 5.0 |
% | Modulo (Remainder) | 10 % 3 = 1 |
** | Exponentiation | 2 ** 3 = 8 |
// | Floor Division | 10 // 3 = 3 |
Comparison (Relational) Operators
These operators compare two values and return a Boolean result (True or False). They are fundamental for conditional logic and filtering data.
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 5 (True) |
!= | Not equal to | 5 != 6 (True) |
Greater than | 7 > 3 (True) | |
< | Less than | 3 < 7 (True) |
= | Greater than or equal to | 5 >= 5 (True) |
<= | Less than or equal to | 3 <= 7 (True) |
Logical Operators
Used to combine conditional statements. They are essential for creating complex filtering criteria and decision-making processes.
Operator | Description | Example |
---|---|---|
and | Returns True if both statements are true | (5 > 3) and (10 < 20) (True) |
or | Returns True if one of the statements is true | (5 > 3) or (10 > 20) (True) |
not | Reverses the result, returns False if the result is true | not (5 > 3) (False) |
Assignment Operators
Used to assign values to variables. The most common is the simple assignment operator (=).
Operator | Example | Same as |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 2 | x = x - 2 |
*= | x *= 4 | x = x * 4 |
/= | x /= 2 | x = x / 2 |
Operator Precedence
When an expression contains multiple operators, the order of evaluation is determined by operator precedence. Parentheses
()
Think of operator precedence like the order of operations in mathematics (PEMDAS/BODMAS). It dictates which part of an expression is calculated first.
Operator precedence defines the order in which operations are performed in an expression. For example, multiplication and division are performed before addition and subtraction. Parentheses ()
have the highest precedence, forcing operations within them to be evaluated first. This is critical for ensuring calculations yield the expected results, especially in complex data transformations.
Text-based content
Library pages focus on text content
To compare two values and return a Boolean result (True or False).
Expressions in Data Science Context
In data science, you'll frequently use expressions to:
- Calculate new features from existing ones (e.g., ).codeprofit = revenue - cost
- Filter datasets based on conditions (e.g., ).codesales[sales['quantity'] > 10]
- Aggregate data using logical combinations (e.g., ).codetotal_sales = sum(price * quantity) if region == 'North' else 0
- Implement algorithms where intermediate calculations are vital.
The double asterisk (**).
Learning Resources
A comprehensive overview of all Python operators with clear examples and explanations.
The official Python documentation's introduction to expressions and the operators used within them.
A detailed blog post explaining the rules of operator precedence in Python and how to use parentheses effectively.
Focuses specifically on arithmetic operators, providing practical examples relevant to calculations.
Explains the functionality of logical operators (and, or, not) with illustrative code snippets.
Covers comparison operators and their use in evaluating conditions, essential for data filtering.
Details the various assignment operators and how they simplify variable updates.
A learning resource that breaks down the concepts of expressions and statements in Python.
While broader, this tutorial often touches upon basic Python operations and expressions used in data manipulation.
An interactive tutorial that covers basic data types and the operators used to manipulate them.