Python -Variables and Operators - Formula Sheet
Arithmetic Operators and Formulas
Arithmetic operators are used to perform mathematical calculations on numeric operands. The table below outlines each operator's formula, action, and returns:
| Operation | Python Formula | Result Description |
|---|---|---|
| Addition | a + b | Calculates the sum of operands a and b. |
| Subtraction | a - b | Calculates the difference when b is subtracted from a. |
| Multiplication | a * b | Calculates the product of multiplying a and b. |
| Standard Division | a / b | Calculates the floating-point quotient of dividing a by b. |
| Modulo (Remainder) | a % b | Yields the mathematical remainder of a divided by b. |
| Exponentiation | a ** b | Calculates the value of base a raised to the power of exponent b. |
| Floor Division | a // b | Calculates the integer quotient of dividing a by b, discarding the fractional part. |
Comparative and Relational Operations
Relational operators determine the comparative relationship between two operands, returning Boolean outcomes (True or False):
- a == b : Checks if operand a is mathematically equal to operand b.
- a != b : Checks if operand a is not equal to operand b.
- a greater than b : Evaluates if operand a is strictly greater than operand b.
- a less than b : Evaluates if operand a is strictly less than operand b.
- a greater than or equal to b : Checks if operand a is greater than or equal to operand b.
- a less than or equal to b : Checks if operand a is less than or equal to operand b.
Logical Expressions
Logical operators perform Boolean algebra calculations on relational conditions:
- p and q : Returns True if both Boolean sub-expressions p and q evaluate to True.
- p or q : Returns True if at least one of the Boolean sub-expressions p or q evaluates to True.
- not p : Negates the Boolean value of expression p (returns True if p is False, and vice versa).
Ternary Conditional Expression Formula
The ternary conditional expression calculates and assigns values based on a evaluated predicate statement:
V = T if C else F
Where:
- V represents the target variable.
- C represents the Boolean test condition to be evaluated.
- T is the expression or value assigned to V if the test condition C is True.
- F is the expression or value assigned to V if the test condition C is False.
More for this chapter
Book Back Questions10 textbook MCQs · solved
Additional MCQs15 extra MCQs · solved
Practice TestInteractive · instant score
Book Back TestTest yourself on the textbook set
Additional MCQ TestTest yourself on the extra set
Study NotesConcepts & methods
More chapters in Computer Science
View all
1 Function
2 Data Abstraction
3 Scoping
4 Algorithmic Strategies
6 Control Structures
7 Python functions
8 Strings and String manipulation
9 Lists, Tuples, Sets and Dictionary
10 Python Classes and objects
11 Database Concepts
12 Structured Query Language (SQL)
13 Python and CSV files
14 Importing C++ programs in Python.
15 Data manipulation through SQL
16 Data visualization using pyplot: line chart, pie chart and bar chart