Series Data Voyage Training Log

~1 min read

Python Basics by Problem Solving

A log of drilling Python fundamentals through practice problems, from print, data types and operators to lists, dictionaries, functions, exception handling, environment variables and JSON.

TrainingPythonFundamentals

Print, Data Types, and Operators

I started by getting comfortable with the details of print(). sep sets the separator between values and end replaces the trailing newline.

  • Learned the arithmetic operators / (float division), // (floor), % (modulo), ** (power)
  • Checked operator precedence through the difference between 5 + 3 * 2 and (5 + 3) * 2
  • Practiced casting between types with int(), float(), str()

Lists and Tuples

A list is "the thing that changes" and a tuple is "the thing that stays." I pressed on each list method one by one.

  • append and extend differ in adding one item or unpacking and adding many
  • Manipulated elements with pop, remove, index
  • Assembled lists with slicing and the +, * operators

Dictionaries

It is the key: value structure. What struck me was how many ways there are to build one: brace literals, the dict() function, and a list of tuples. I also hit the constraint firsthand that dict() can't take keys with spaces.

I practiced pulling data out with keys(), values(), items() and nesting lists or other dictionaries as values.

Functions and Exceptions

def greet(name):
    print(f"Hello, {name}!")
  • Covered arguments, return values, defaults, variadic args *args, and lambdas
  • Caught errors with try / except so the program doesn't halt

I left myself a note: don't get confused by the braces in f-strings.

Environment Variables and JSON

Sensitive values don't belong in code. With python-dotenv I created a .env file and loaded it with load_dotenv() followed by os.getenv(). The real habit of handling API keys and passwords started here.

Finally, I read bank and restaurant data with json.load() and converted it into a pandas DataFrame. I watched a string get "translated" into dicts and lists.

Takeaway

Key point

Solving hundreds of basic problems by hand, I built the habit of asking "why this data type?" rather than about syntax. Each time I weighed list or dict, and whether to wrap it in a function. Those choices became the skeleton of my later data-collection and analysis code.