- Mutable objects (List, Set, Dictionary): Can be modified in place after creation
- Immutable objects (Tuple): Cannot be modified; must create new object to change values
- List
[]- Ordered, mutable collection- Tuple
()- Ordered, immutable collection- Set
{}- Unordered, mutable collection of unique elements- Dictionary
{}- Unordered collection of key-value pairsUnderstanding mutability is crucial for data manipulation, memory efficiency, and data integrity in Python programming and data science.
- Unix Epoch: January 1, 1970 - the reference point for Unix timestamps
- Timestamp: Number of seconds elapsed since the Unix epoch
- Date Formatting: Using
strftime()to format dates in specific patterns
datetimemodule for date and time operationsstrftime()for formatting dates (e.g., "%b %d %Y" → "Oct 21 2022")timestamp()to get seconds since Unix epoch
:,.4f- Comma separator + 4 decimal places (e.g.,1,234,567.1235):.2e- Scientific notation with 2 decimals (e.g.,1.23e+06)
Code Name Example Output fFixed-point f"{123.456:.2f}"123.46eScientific f"{1234:.2e}"1.23e+03%Percentage f"{0.1234:.2%}"12.34%dDecimal f"{1234:d}"1234
:- starts the format specification,- adds thousand separators (commas).4- specifies 4 decimal placesf- fixed-point notation (decimal format)Understanding date/time manipulation is essential for data science tasks like time series analysis, data logging, and temporal data processing.
- Type introspection: Using
type()to identify object types at runtime- Type names: Accessing type names with
type().__name__- Function returns: Functions can return values while performing side effects (printing)
type(object)- Returns the type of an objecttype().__name__- Gets the string name of the type- Conditional logic based on type checking
- Return values in functions
Type checking is fundamental for data validation, error handling, and creating flexible functions that handle different data types appropriately.
- Falsy values: Different representations of "null" or "empty" in Python
- Type checking order: Checking
boolbeforeint(sinceFalse == 0isTrue)- NaN detection: Using
math.isnan()to identify Not-a-Number floats- Return codes: Using 0 for success, 1 for error
None- The true null valuefloat("NaN")- Not a Number (mathematical undefined)0- Zero integer""- Empty stringFalse- Boolean false
False == 0returnsTruein Python!- Always check
booltype beforeintwhen both could be 0- Use
is Nonenot== Nonefor identity checkUnderstanding Python's truthiness and falsy values is crucial for data validation, especially when cleaning datasets with missing or null values in data science.