Comparison Operators: Used to compare values, determining the relationship between them (e.g., equal, greater than, less than).
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.
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.
Floating Point with Integers:
Example: Comparing 4 + 1
to 5.0
may yield unexpected results due to precision.
Equality Comparisons: Should not be relied upon for floating point numbers.
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.
Common Error:
Using assignment operator =
instead of comparison operator ==
can lead to logical errors in conditions.
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.
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.
Substring Testing:
To check if a substring exists within a larger string using IN
or NOT IN
operators.
Checking Keys:
Membership tests for dictionaries check if a given item is a key, not a value.
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.
Order of Operations:
Parentheses ()
take highest precedence.
Followed by Arithmetic Operations, Relational Operators, Logical NOT, AND, and finally OR.
Python Indentation:
Code blocks are defined by indentation level.
Example: Proper indentation ensures which statements execute based on conditions.
Syntax Equivalent to If-Else:
Conditional expressions can assign values based on conditions in a concise manner, mirroring if-else structures.
Structure:
Employ a series of conditional tests to decode user inputted abbreviations, returning a relevant output or a default message.
Specifications:
Input three integers and output the smallest one using conditional comparisons to determine the least value.