Prepstellar

Python Fundamentals · Getting started

19 cards

A Python Development Workflow

Swipe, scroll or use ← →
  1. Start from the task, not from the tool

    A Python workflow begins with a task, not with an editor. Automate repeated file changes, build a small application, create tests, or provide programmable behavior inside another system: the problem decides what code and which libraries are needed, and no tool makes that decision for you.

    Three questions settle the shape of the program before a line is written.

    Question Why it decides the shape
    What is the input? Files, arguments, a stream of records — this determines what the program must read
    What is the desired output? A changed file, a report, a running service
    How often must it repeat? Once is an experiment; weekly is a saved program

    Python supports both quick automation and structured programs that can grow beyond a shell script, which is why the same language can carry a throw-away answer and the maintained tool it becomes.

    1 / 19
  2. Quick check

    Before writing any code, which three things settle what shape the program should take?

    1. AThe input, the wanted output, and how often the work must be repeated

      Right. The problem determines the code and the libraries needed, so the input, the desired output and the repeatability need are written down first.

    2. BThe launcher name used on your operating system

      Launcher names vary by platform and installation, and they say nothing about what the program has to do.

    3. CWhich community packages happen to be installed in the environment already

      Installed packages are a later question; the task decides which capability is needed in the first place.

    2 / 19

  3. Explore at the prompt

    The first contact with a new idea should be cheap. The interactive interpreter reads and executes commands when its standard input is connected to a terminal. Type an expression, see the value; call a function, see what it returns.

    This mode earns its place in three situations:

    • Trying an expression whose behavior you are not sure about.
    • Exploring a language feature you have just read about.
    • Testing a function while you are still writing it, before any program uses it.

    The result is immediate, so the feedback loop is as short as it can be: the gap between a wrong assumption and the evidence that it is wrong is one line.

    3 / 19
  4. Explore at the prompt

    Interactive mode also tells you where you are. Commands read from a terminal are prompted with the primary prompt, usually three greater-than signs, and continuation lines inside a multi-line construct are prompted with three dots. Seeing the secondary prompt is how you notice that the interpreter is still waiting for the rest of an if statement rather than ignoring you.

    What this mode is not is a place to keep work. Everything typed at the prompt exists for as long as the session does. That is a feature while exploring — nothing accumulates, nothing has to be cleaned up — and a problem the moment the same result is needed twice.

    4 / 19
  5. Quick check

    You are unsure how one expression behaves and want the answer now. Which mode fits?

    1. AA saved script, run again after each edit

      A script is the right home for repeatable work, but it adds a save-and-run step to a one-line question.

    2. BA module copied into several programs

      Copying code into several programs is the duplication that modules exist to prevent, and it answers nothing about the expression.

    3. CInteractive mode, where the interpreter reads and executes commands as they are typed

      Right. When its standard input is connected to a terminal, the interpreter reads and executes commands interactively, so the result is immediate.

    5 / 19

  6. Save what works as a script

    Exploration is not the final form of a repeatable program. When the interpreter is invoked with a file name or a file as standard input, it reads and executes a script from that file. Saving the code that worked turns a session into an artifact: it can be run again, edited, reviewed and corrected.

    Interactive session Script file
    Lives for The session As long as the file exists
    Good for Trying, checking, throwing away Repeating, editing, sharing
    Cost of running again Retyping everything Running one file

    The move from one to the other is the first real step of the workflow, and it usually happens the second time you find yourself retyping the same three lines.

    6 / 19
  7. Quick check

    A one-off experiment at the prompt now works. What makes the same work repeatable tomorrow?

    1. AKeeping the terminal history of that session and reading it back

      History records what was typed; it is not a program that can be run again.

    2. BSaving the code in a script file

      Right. When the interpreter is invoked with a file name, it reads and executes a script from that file, so saved code can be run and edited again.

    3. CInstalling a community package that does something similar

      A package may help later, but it does not preserve the code that already works.

    7 / 19

  8. Keep your progress in the app

    That’s 3 of 7 quick checks. In the app they stay answered, and every lesson remembers where you left off.

  9. Setup from a file, then keep exploring

    The two modes are not rivals, and there is a documented way to use both in one run. Passing -i before a script runs the script and then enters interactive mode, combining repeatable setup with continued exploration.

    That hybrid fits a very common moment: the objects you want to inspect take several lines to build, and building them by hand every time is exactly the retyping a script removes. The file does the setup; the prompt does the inspecting.

    8 / 19
  10. Setup from a file, then keep exploring

    Read the difference as a matter of what happens after the last line of the file:

    Invocation After the file's last line
    python script.py The interpreter exits
    python -i script.py The interpreter stays, in interactive mode, with everything the script built

    The order matters: -i goes before the script name, because it is an option for the interpreter rather than an argument for the program.

    9 / 19
  11. Quick check

    A developer must build the same objects from a saved file every time, then examine them by hand. Which invocation does both?

    1. ARun the script with `-i` before its file name

      Right. Passing `-i` before a script runs the script and then enters interactive mode, so repeatable setup is followed by manual inspection.

    2. BEdit the file without running it

      An unrun file builds nothing, so there is nothing to inspect.

    3. CRun an unrelated module with `-m` and leave the setup script unused

      The `-m` form runs a module's source as a script; choosing an unrelated one abandons the setup that was required.

    10 / 19

  12. Running a module, and naming the interpreter

    Some modules are useful as programs in their own right. A module can also be executed as a script with the python -m module form, which executes the source file for that module as if its full name had been spelled out on the command line.

    That is worth knowing early for a practical reason: it means a reusable module does not have to choose between being imported by other code and being run directly.

    Form What it runs
    python script.py The file at that path
    python -m module The source of the named module, located for you
    python -i script.py The file, then an interactive session
    11 / 19
  13. Running a module, and naming the interpreter

    One caveat applies to every command above. Launcher names and interpreter locations vary by platform and installation, so the locally appropriate Python command must be used. On many Unix systems the interpreter is installed under a versioned name; on Windows it may be available through a store installation or through the py launcher.

    Writing python in a lesson is shorthand for "the command that starts Python here". Finding out what that command is on your own machine is a one-minute job worth doing before the first script.

    12 / 19
  14. Quick check

    A module in the project is also useful as a program on its own. Which form runs its source as a script?

    1. A`python -i` with no file, then retype the module's code

      The `-i` option enters interactive mode; on its own it does not locate and run a module's source.

    2. B`python -m module`

      Right. `python -m module` executes the source file for that module as if its full name had been given on the command line.

    3. CPassing the module's name to a shell command that is not Python

      The module is Python source, so the Python interpreter is what has to run it.

    13 / 19

  15. Split responsibilities instead of copying code

    As a program grows, the pressure is to copy the part that works into the next program. That is the moment to divide responsibilities instead. Python allows a program to be split into modules that can be reused by other Python programs.

    The test is simple: if the same behavior is needed in two places, it belongs in one place that both can use. Copied code has to be corrected in every copy, and the copies drift apart quietly.

    14 / 19
  16. Split responsibilities instead of copying code

    A worked example makes the boundary visible. A file-processing utility grows to hold three responsibilities: parsing a record, deciding what to do with it, and writing the result. A second program needs only the parsing.

    Responsibility Where it belongs
    Parsing a record A module, imported by both programs
    Deciding what to do The program that owns that decision
    Writing the result The program, or a second shared module if both need it

    Nothing is lost by the split. The parsing code is still ordinary Python, run by the same interpreter, and it can still be tested at the prompt while it is being written.

    15 / 19
  17. Quick check

    A utility has outgrown one file, and two other programs need the same parsing behavior without copied code. What is the appropriate next step?

    1. APaste the parsing code into both programs

      Three copies of the same behavior have to be corrected three times, which is what splitting a program into modules avoids.

    2. BLeave the behavior in an unsaved interactive session

      An unsaved session cannot be reused by another program at all.

    3. CMove the parsing behavior into a module the other programs can reuse

      Right. Python allows a program to be split into modules that can be reused by other Python programs.

    16 / 19

  18. Choose existing facilities before writing your own

    Before writing a capability, check whether it is already distributed. Python includes standard modules that can serve as the basis of programs, including facilities for file input and output, system calls, sockets, and graphical interfaces. They can be used as the foundation of your program, or read as examples while learning.

    The wider ecosystem offers third-party components in addition to the standard library. Evaluate one when the required capability is not provided by the language or by the distributed library — that order keeps a project's dependencies down to the ones it actually needs.

    Step Question
    1 Can the language and its built-in types do this?
    2 Is there a distributed module whose documented capability matches the task?
    3 Does a third-party component fill what is still missing?
    17 / 19
  19. Choose existing facilities before writing your own

    The whole path is therefore a loop rather than a straight line: define the task, experiment interactively, save working code as a script, organize reusable responsibilities as modules, and select library support. A later edit re-enters the same loop — reproduce the behavior, test a smaller idea at the prompt, update the saved code, keep the reusable parts separate.

    • Use interactive mode for immediate experiments and a script for repeatable execution.
    • Move reusable responsibilities into modules instead of duplicating code.
    • Start with built-in and standard-library facilities, then consider third-party packages for additional capability.

    Later lessons teach the syntax and the operational detail behind each stage; this is the map they fit into.

    18 / 19
  20. Quick check

    A team has identified a file-processing task, validated one operation at the prompt, and now needs repeatable execution plus reuse in a second program. Which sequence fits?

    1. ASave a script, extract a module, then use suitable library facilities

      Right. The path runs from interactive feedback to a saved script, then to modules for reuse, and then to the facilities the language and the distributed library already provide.

    2. BRepeat every command by hand each time

      Manual repetition is exactly what saving a script removes, and it leaves nothing to reuse.

    3. CConvert the operation into a platform-specific shell command and duplicate it in both programs

      A platform-specific command gives up portability, and duplicating it recreates the copied code a module is meant to prevent.

    19 / 19

  21. 7 quick checks · then the test

    In the app, finishing the quick checks opens this lesson’s 10-question test, and the ones you miss come back exactly when you’re about to forget them.

The whole course, on your phone

Lessons you can read, audio you can listen to on the way to work, and practice that remembers what you got wrong.