0% found this document useful (0 votes)
4 views6 pages

Arithmetic and Logical Operations

The document provides an overview of Python's arithmetic, assignment, comparison, and logical operators, including their usage and examples. It highlights key concepts such as operator precedence, type conversions, and common pitfalls to avoid. Additionally, it discusses real-world applications of these operators in input validation and data filtering.

Uploaded by

Mercy Okanlawon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Arithmetic and Logical Operations

The document provides an overview of Python's arithmetic, assignment, comparison, and logical operators, including their usage and examples. It highlights key concepts such as operator precedence, type conversions, and common pitfalls to avoid. Additionally, it discusses real-world applications of these operators in input validation and data filtering.

Uploaded by

Mercy Okanlawon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Arithmetic and Logical Operations

Goal: Master Python’s arithmetic operators, logical comparisons, operator


precedence, and common pitfalls.

1. Arithmetic Operators

Used for mathematical calculations.

Operato Exampl
Name Result
r e

+ Addition 5 + 3 8

- Subtraction 10 - 4 6

* Multiplication 2 * 3 6

/ Division 10 / 3 3.333

% Modulus 10 % 3 1

** Exponentiation 2 ** 3 8

// Floor Division 10 // 3 3

Key Notes:

 Division (/): Always returns a float.


print(4 / 2) # Output: 2.0 (not 2)

 Floor Division (//): Rounds down to the nearest integer.


print(-7 // 2) # Output: -4 (rounds down, not toward zero).

 Modulus (%): Returns the remainder.


python

Copy

Download
print(10 % 3) # Output: 1
print(-10 % 3) # Output: 2 (math: -10 = (-4)*3 + 2)

2. Assignment Operators

Combine arithmetic operations with assignment.

Operato
Example Equivalent To
r

= x = 5 x = 5

+= x += 2 x = x + 2

-= x -= 3 x = x - 3

*= x *= 4 x = x * 4

/= x /= 2 x = x / 2

%= x %= 3 x = x % 3

Example:

x = 10
x **= 2 # x = 10^2 = 100
print(x) # Output: 100

3. Comparison Operators

Compare values and return True or False.

Operato
Meaning Example Result
r

== Equal to 5 == 5 True

!= Not equal to 5 != 3 True


Operato
Meaning Example Result
r

> Greater than 10 > 5 True

< Less than 10 < 5 False

>=
Greater than or equal 10 >= 10 True
to

<= Less than or equal to 8 <= 5 False

Use Cases:

 Conditional checks in loops or if statements.


 Validating user input.

Example:

age = 18
is_adult = age >= 18 # is_adult = True

4. Logical Operators

Combine multiple conditions.

Operator Meaning Example Result

and
Both conditions must be (5 > 3) and (2 < 1) False
true

or At least one condition is true (5 > 3) or (2 < 1) True

not Inverts the result not (5 > 3) False

Short-Circuit Evaluation:

 and: Stops evaluating if the first condition is False.

 or: Stops evaluating if the first condition is True.


Example:

x = 10
if x > 5 and x < 20:
print("Valid range!") # Output: Valid range!

5. Operator Precedence

Determines the order of operations. From highest to lowest:

1. Parentheses ()
2. Exponentiation **
3. Multiplication *, Division /, Floor Division //, Modulus %
4. Addition +, Subtraction -
5. Comparison Operators (==, >, etc.)
6. Logical Operators (not, and, or)

Example:

result = 3 * 3 + 5 - 4 / 4
# Step 1: 3*3 = 9
# Step 2: 4/4 = 1.0
# Step 3: 9 + 5 = 14
# Step 4: 14 - 1.0 = 13.0
print(result) # Output: 13.0

Forcing Evaluation Order:


Use parentheses to override precedence:

result = 3 * (3 + 5) - (4 / 4) # Evaluates to 24 - 1.0 = 23.0

6. Working with None

 None represents "no value" or "empty".


 Use is or is not to check for None (not ==).

Example:

x = None
if x is None:
print("x has no value!") # Output: x has no value!

Why is and not ==?

 is checks identity (same object in memory).

 == checks equality (same value).

7. Type Conversions

Convert between data types using:

 int(): Converts to integer.

 float(): Converts to float.

 str(): Converts to string.

Example:

x = 3.14
y = int(x) # y = 3
z = str(y) # z = "3"

Pitfall:

print(int("12.5")) # ValueError: Can’t convert float-string to int.

8. Common Mistakes

1. Confusing = and ==:

python
Copy

Download

if x = 5: # SyntaxError: Use '==' for comparison.

2. Ignoring Operator Precedence:


result = 5 + 3 * 2 # 11 (not 16).

3. Misusing Logical Operators:


if age > 12 and < 18: # Invalid syntax. Use: 12 < age < 18.

4. Comparing Different Types:


print("5" == 5) # False (string vs integer).

9. Real-World Applications

1. Input Validation:
user_age = input("Enter your age: ")
if user_age.isdigit() and int(user_age) >= 18:
print("Access granted!")

2. Data Filtering:
numbers = [10, 23, 5, 45, 7]
filtered = [num for num in numbers if num % 2 == 0] # [10]

10. Key Takeaways

1. Use parentheses to clarify or override precedence.


2. is checks identity; == checks equality.
3. Floor division (//) and modulus (%) behave differently with
negatives.
4. Logical operators use short-circuit evaluation.

You might also like