DataCareerHub / Python for Data Science
Python Fundamentals and Data Structures
Fictional practice · Reference code executed locally · The supplied local reference was executed against the included fixture. This does not verify a production service or the learner’s own environment.
Choose types that preserve meaning
Choose types that preserve meaning
Python values have types and operations with specific meanings. Strings preserve identifiers such as O001; integers preserve cent arithmetic; floats represent approximate numeric values. A boolean is a logical value, though Python treats bool as an int subclass, which matters for validation. A list preserves a sequence, a dictionary maps keys to values, and a set represents unique membership. Choose from the business meaning instead of converting everything that looks numeric. The included source records arrive as text and need explicit parsing before multiplication.
Follow a concrete example
Inspect the raw string values before parsing. The identifier O001 must retain its prefix and leading zeros; quantity"2" needs an integer conversion before multiplication. Keep the conversion close to a domain check so parseable values such as-1 do not automatically enter accepted data. A dictionary maps an order key to one validated parent record, not an arbitrary last duplicate.
Further reading: Python 3.12 tutorial, pandas 2.2 merge, NumPy 2.2 reference. The case and exercises are authored for DataCareerHub.
Your task
Inspect one order and line dictionary. Classify each field and predict the result of multiplying text "2" by 3 versus integer 2 by 3.
Use learner_worksheet.txt and the provided source/code files. Save your own artifact and compare the separate solution after attempting the task.
Worked solution
Identifiers remain strings. "2"*3 repeats text as "222"; int("2")*3 gives 6. quantity and price are parsed integers under the source contract; order dates retain an explicit ISO representation.
Iterate with a clear population rule
Iterate with a clear population rule
A loop applies a defined operation to records; a condition determines which records belong in the result. Build an order lookup, retain completed status and accumulate line value by month. Keep the running accumulator separate from source dictionaries so the original evidence remains unchanged. A dictionary key supplies the grouping dimension, while a set can count unique orders. Avoid deleting items from a list while iterating because positions shift. Reuse the earlier analysis module for pandas syntax; this lesson deliberately implements the logic with core Python first.
Follow a concrete example
Trace a three-line accumulator on paper. For each accepted record, derive integer cents, select the ISO month key, read the previous subtotal and add exactly once. Compare the hand calculation with a loop and then with the complete reference. When the same parent has two lines, add both line values but count the parent once for order measures.
Inspect the reference implementation
Language: Python 3.12. The complete downloadable file includes the named helpers and the fresh-output command-line runner. Trace the source, transformation and result against this lesson’s contract before running it locally.
def py_basics(source,out):
values=sales(source);month={}
for r in values:
key=r['order_date'][:7];month[key]=month.get(key,0)+r['value_cents']
return {'raw_orders':len(rows(source,'orders.csv')),'accepted_lines':len(values),'monthly_cents':month,'grand_cents':sum(month.values()),'average_completed_order_eur':sum(month.values())/21/100}
Further reading: Python 3.12 tutorial, pandas 2.2 merge, NumPy 2.2 reference. The case and exercises are authored for DataCareerHub.
Your task
Trace the first three completed lines through the month accumulator, then run worked_lab.py. Explain the role of get(key,0).
Use learner_worksheet.txt and the provided source/code files. Save your own artifact and compare the separate solution after attempting the task.
Worked solution
get supplies a starting zero for a new month before adding its line value. The complete result is April 60000 cents, May 59000 and grand 119000. The completed population has 42 lines and 21 distinct orders.
Test boundaries and explain the result
Test boundaries and explain the result
A script executing without an exception is not proof of analytical correctness. Check the selected population, key uniqueness, units and reconciliation. Calculate the grand total independently from monthly values. Distinguish line count from order count and keep cancelled records visible in source coverage. Use assertions to express invariants you can explain, not arbitrary constants copied from a solution. Baseline constants belong to the unchanged fixture; a controlled quantity change should alter the total while structural invariants remain valid.
Follow a concrete example
Define a baseline assertion for the unchanged119000-cent fixture and a separate invariant that monthly sums equal the grand total. Change one accepted quantity in a copy. The baseline total should change; conservation and key checks should still hold. Distinguishing these two kinds of tests prevents an expected-results file from becoming a rule that forces bad corrections.
Further reading: Python 3.12 tutorial, pandas 2.2 merge, NumPy 2.2 reference. The case and exercises are authored for DataCareerHub.
Your task
Write three invariants and one changed-input test. Explain the reported grand average order value.
Use learner_worksheet.txt and the provided source/code files. Save your own artifact and compare the separate solution after attempting the task.
Worked solution
Monthly sums reconcile to 119000 cents. AOV is 1190/21≈56.6667 EUR. A valid quantity increase changes value by that line’s unit price without changing key uniqueness. Three cancelled orders remain outside the reported population.
Practice evidence