Python for Data Science and Programming Fundamentals
Python for Data Science and Programming Fundamentals
This domain tests practical Python: data types, containers, control flow, functions, classes, exceptions, file handling, and the idioms used in notebooks. Questions often ask you to predict output or choose the safer construct.
Core rules
- Lists, dictionaries, and sets are mutable; strings and tuples are immutable.
- Indexing is zero-based; slicing excludes the stop index.
==compares values;iscompares object identity.- Default arguments are evaluated once, so avoid mutable defaults such as
def f(items=[]). - Use
try/except/else/finallyand raise specific exceptions. - Open files with
with open(...) as fso they close reliably. - Follow PEP 8:
snake_casefor functions and variables,CapWordsfor classes.
Worked example
Count the most common words in a file:
from collections import Counter
def top_words(path, n=3):
with open(path, encoding='utf-8') as f:
words = f.read().lower().split()
return Counter(words).most_common(n)
Reasoning: with guarantees closure; lower() normalizes case; split() tokenizes on whitespace; Counter counts without a manual dictionary loop; most_common returns tuples sorted by descending frequency. If punctuation must be removed, preprocess before counting rather than changing the return type.
Traps
- Mutating a list while iterating over it.
- Assigning the result of
list.append, which isNone. - Forgetting
selfin instance methods. - Catching broad
Exceptionand hiding real bugs. - Assuming
range(1, 5)includes 5; it yields 1 through 4. - Mixing class attributes and instance attributes.