Week-4-2
Comparison Operators Overview
Comparison Operators: Used to compare values, determining the relationship between them (e.g., equal, greater than, less than).
Comparison of Data Types
Allowed Comparisons:
Integers can be compared with integers.
Strings can be compared with strings.
Floating point numbers can be compared with other floating point numbers.
Can also compare integers with floating point numbers.
Issues with Floating Point Comparisons
Precision Problems:
Floating point numbers often have imprecise internal representations in computers.
Visual comparison might suggest equality, but tiny differences may exist at the decimal level.
Recommendation: Avoid using equality comparisons for floating point numbers; instead, use greater than or less than comparisons.
Common Comparison Mistakes
Floating Point with Integers:
Example: Comparing
4 + 1
to5.0
may yield unexpected results due to precision.Equality Comparisons: Should not be relied upon for floating point numbers.
Comparison of Complex Structures
Dictionaries:
Two dictionaries must have identical keys and values to be considered equal.
Tuples:
Comparison begins from the first position, proceeding until a difference is found, determining overall comparison truth.
Lists:
Comparison with different data types (e.g., integers vs. characters) results in an error.
Assignment vs. Comparison
Common Error:
Using assignment operator
=
instead of comparison operator==
can lead to logical errors in conditions.
IN and NOT IN Operators
Membership Testing:
IN
: Checks if an item is present in a container (like a list or dictionary).NOT IN
: Checks if an item is absent from a container.
Example with Lists
List of Football Players:
Input a player's name, check membership in a defined list, and execute corresponding print statements based on the presence of the name.
Example with Strings
Substring Testing:
To check if a substring exists within a larger string using
IN
orNOT IN
operators.
Dictionaries and Keys
Checking Keys:
Membership tests for dictionaries check if a given item is a key, not a value.
Identity Operators
IS and IS NOT:
IS
: Checks if two variables point to the same object in memory.IS NOT
: Checks if two variables do not point to the same object.
Operator Precedence
Order of Operations:
Parentheses
()
take highest precedence.Followed by Arithmetic Operations, Relational Operators, Logical NOT, AND, and finally OR.
Code Block Indentation
Python Indentation:
Code blocks are defined by indentation level.
Example: Proper indentation ensures which statements execute based on conditions.
Conditional Expressions
Syntax Equivalent to If-Else:
Conditional expressions can assign values based on conditions in a concise manner, mirroring if-else structures.
Practical Example: Abbreviation Decoder
Structure:
Employ a series of conditional tests to decode user inputted abbreviations, returning a relevant output or a default message.
Lab Example: Finding the Smallest Integer
Specifications:
Input three integers and output the smallest one using conditional comparisons to determine the least value.