Python - From Scratch to Expert
I. Python Fundamentals
Section titled “I. Python Fundamentals”1. Arithmetic & Order of Operations
Section titled “1. Arithmetic & Order of Operations”Python follows the PEMDAS rule for operator precedence.
- Parentheses
() - Exponents
** - Multiplication
*, Division/, Floor Division//, Modulus%(evaluated left-to-right) - Addition
+, Subtraction-(evaluated left-to-right)
2. Variables & Functions
Section titled “2. Variables & Functions”- Comments: Use
#for single-line comments. - Docstrings: Use
""" description """immediately after defining a function to document it. - Function Template:
def function_name(input_variable=default_value):# computational statementsreturn output_variable
- Note: You can assign default values to parameters to prevent errors if arguments are missing during the call.
- Scope: Python defines scope via indentation (whitespace).
- LEGB Rule: Python looks for variables in this order: Local -> Enclosing -> Global -> Built-in.
3. Data Types & “Peculiarities”
Section titled “3. Data Types & “Peculiarities””- Strings:
- Immutable (cannot be changed after creation).
- Can have length 0 (
""). - Math:
String * Integerrepeats the string.String * Floatraises an error. - Casting:
float("3.5")works, butfloat("three")fails.
- Booleans:
- Used in conditional logic.
Trueequates to 1,Falseto 0.
4. Operators
Section titled “4. Operators”Conditional Operators
| Symbol | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
< / <= | Less than / Less than or equal |
> / >= | Greater than / Greater than or equal |
Logical & Identity Operators
- Logical:
and,or,not - Identity:
is,is not(Checks if two variables point to the same object in memory, not just equal values). - Membership:
in,not in(Checks if a value exists in a sequence like a list or string).
II. Data Structures
Section titled “II. Data Structures”1. Strings & Slicing
Section titled “1. Strings & Slicing”- Escape Characters:
\n: Newline\t: Tab\': Single quote (inside single-quoted string)\\: Backslash
- Slicing Syntax:
iterable[start:stop:step]- First
xentries:[:x] - Last
yentries:[-y:] - Reverse string:
[::-1]
- First
2. Lists vs. Dictionaries
Section titled “2. Lists vs. Dictionaries”- Lists: Ordered, mutable sequences. Accessed by integer index.
- Dictionaries: Key-Value pairs
{key: value}. Unordered (conceptually). Accessed by Key.- Note: Keys must be immutable (strings, numbers, tuples).
III. Pandas for Data Science
Section titled “III. Pandas for Data Science”1. Core Components
Section titled “1. Core Components”- DataFrame: A table consisting of rows (records) and columns.
- Series: A single column (sequence of data). A DataFrame is essentially a collection of Series sharing an index.
2. Data Selection (Indexing)
Section titled “2. Data Selection (Indexing)”df.iloc[](Index-based): Selects by integer position (0 to length-1).- Example:
df.iloc[0](First row).
- Example:
df.loc[](Label-based): Selects by the name of the index/column.- Example:
df.loc['row_name', 'column_name'].
- Example:
- Conditionals:
df.loc[df.column_name == 'value'].
3. Data Cleaning & Handling Nulls
Section titled “3. Data Cleaning & Handling Nulls”- Detection:
pd.isnull()(ordf.isnull()) andpd.notnull(). - Correction:
fillna(value): Fills NaN with a specific value.replace(old, new): Swaps specific values.
- Renaming:
df.rename(columns={'old_name': 'new_name'}).
4. Data Transformation
Section titled “4. Data Transformation”- Summary:
df.describe()gives count, mean, std, min, max, etc. - Sorting:
df.sort_values(by='column'). - Mapping:
map(): Used on Series to substitute each value with another.apply(): Used on Series or DataFrame to apply a function across an axis.
- Grouping:
df.groupby('column').mean()splits data into groups, applies a function, and combines results.
5. Combining Data
Section titled “5. Combining Data”concat(): Stacks dataframes (top-to-bottom or side-by-side). Good for similar structures.join(): Combines based on index.merge(): Combines based on column values (SQL-style joins: inner, outer, left, right).