1 Programming Foundations for Data Science
Learn how Python values, collections, conditions, loops, and functions work together to inspect and transform data.
Variables and values
A is a name bound to a value. Assign a value with =, and choose descriptive names such as temperature or row_count to make code easier to understand. Python determines an object’s at runtime, so the same name can later refer to a value of a different type.
Common built-in types include:
intfor whole numbers, such as12.floatfor numbers with a decimal part, such as3.5.boolforTrueorFalse, often used in tests.strfor text, such as"sample A".NoneTypefor the type ofNone, which is commonly used to represent an absent value.
Text that looks numeric is still a string: "12" is a str, not an int. Convert values explicitly when needed, for example with int("12") or float("3.5"). Conversion can fail if the text is not a valid number, so check data from external sources before relying on it.
Collections
Collections group related values. Choose a collection according to how its data will be accessed or changed.
A (
) is an ordered, changeable sequence. Use it for values you may add, remove, or update, such asscores = [82, 91, 76].A (
) is an ordered sequence that cannot be changed after creation, such aslocation = (40.7, -74.0).A (
dict) is a changeable mapping from keys to values, useful for records with named fields, such asrecord = {"site": "north", "count": 18}.A (
) contains distinct values and is useful for membership checks or removing duplicates, such assites = {"north", "south"}.
Lists and tuples use positions starting at index 0; for example, scores[0] accesses the first item. Dictionaries are accessed by key, as in record["count"]. Use len(collection) to get a collection’s size and value in collection to test membership. Lists and dictionaries can be changed, while tuples cannot; sets do not provide positional indexing.
Conditions
A chooses which instructions to run. if evaluates a condition; elif checks another condition if earlier tests were false; and else handles the remaining case. Comparisons such as ==, !=, <, and >= produce Boolean values. Combine tests with and, or, and not.
Use is None to check specifically for the missing-value marker None. Empty collections and zero are also treated as false in conditions, so distinguish these cases when they have different meanings in your data.
Loops
A repeats instructions. A for visits each item in an iterable, such as a or , and is useful when the items themselves define what to process. A while repeats as long as a condition remains true; ensure its condition can eventually become false to avoid an endless .
Use range(n) when a fixed number of iterations is needed. break exits the innermost , while continue skips to the next iteration.
Functions for reusable operations
A names a reusable operation. Define one with def, give it parameters for its inputs, and use return to send a result back to the caller. If a reaches its end without a return statement, it returns None.
The following collects available scores from records, skips missing scores, and returns None if no scores are available. That check avoids dividing by zero.
In this example, a holds records, each represents a record, a examines each score, and a skips missing values. Functions make repeated operations easier to test and reuse; descriptive names and a short docstring clarify their purpose.