Python Syntax Quick Reference#
This page provides a quick reference for Python and pandas syntax commonly used in this course.
Bracket Types: When to Use What#
Python uses three types of brackets, each with a specific purpose:
Bracket Type |
Symbol |
Primary Uses |
Examples |
|---|---|---|---|
Square brackets |
|
Lists, indexing, selecting columns |
|
Parentheses |
|
Function calls, tuples, grouping |
|
Curly braces |
|
Dictionaries, sets |
|
Common Mistakes#
# ❌ WRONG: Using parentheses for column selection
df('column_name') # This won't work!
# ✅ CORRECT: Use square brackets
df['column_name']
# ❌ WRONG: Using square brackets for function calls
print['Hello'] # This won't work!
# ✅ CORRECT: Use parentheses
print('Hello')
DataFrame Column Selection#
# Select a single column (returns a Series)
ages = data['age']
# Select multiple columns (returns a DataFrame)
subset = data[['age', 'sex', 'education']]
# Note the double square brackets: outer for indexing, inner for the list
# Select columns by condition
numeric_cols = data[data.columns[data.dtypes == 'float64']]
DataFrame Indexing: .loc[] vs .iloc[]#
Method |
What it uses |
Example |
|---|---|---|
|
Labels/names |
|
|
Integer positions |
|
Key Difference#
# .loc[] uses LABELS (inclusive on both ends)
df.loc[0:5] # Includes rows 0, 1, 2, 3, 4, AND 5
# .iloc[] uses INTEGER POSITIONS (exclusive on the end)
df.iloc[0:5] # Includes rows 0, 1, 2, 3, 4 (NOT 5)
Spacing and Formatting#
Spaces DON’T Matter (Mostly)#
# These are all equivalent:
data['age']
data[ 'age' ]
data [ 'age' ]
# Function arguments with spaces
print('Hello', 'World')
print( 'Hello' , 'World' )
Commas DO Matter#
# ❌ WRONG: Missing comma
df[['age' 'sex']] # This will cause an error!
# ✅ CORRECT: Comma separates items in a list
df[['age', 'sex']]
# ❌ WRONG: Extra comma at the end (in some contexts)
my_list = [1, 2, 3,] # Works but not recommended
# ✅ CORRECT: No trailing comma
my_list = [1, 2, 3]
Quotes: Single vs Double#
# Both work the same for strings
name1 = 'Alice'
name2 = "Alice"
# Use the opposite quote type for nested quotes
message = "She said 'Hello'"
message = 'She said "Hello"'
# Or use escape characters
message = 'She said \'Hello\''
Common String Operations#
# Column names are strings
column_name = 'age'
data[column_name] # Same as data['age']
# String values need quotes
data[data['sex'] == 'Male'] # ✅ CORRECT
data[data['sex'] == Male] # ❌ WRONG: Male without quotes is a variable
Method Chaining#
# You can chain multiple operations
result = (data
.query('age > 30')
.groupby('ART')
['visuospatial_domain_z']
.mean())
# Same as:
temp1 = data.query('age > 30')
temp2 = temp1.groupby('ART')
temp3 = temp2['visuospatial_domain_z']
result = temp3.mean()
Quick Troubleshooting Checklist#
When you encounter an error, check:
✅ Are you using the right bracket type?
✅ Do you have matching opening and closing brackets?
✅ Are all your commas in the right places?
✅ Are strings surrounded by quotes?
✅ Are you using
.loc[]or.iloc[]correctly?✅ Do column names match exactly (including capitalization)?
Resources#
Python Types Reference - Learn about common types (int, str, list, Series, DataFrame, Axes)
Troubleshooting Common Errors - Understand and fix Python errors