Prepstellar

Python Fundamentals · Getting started

20 cards

What Is Python?

Swipe, scroll or use ← →
  1. Which problems Python is built for

    Before any syntax, it helps to know what kind of work this language is aimed at. Python is a high-level, interpreted programming language suited to scripting and rapid application development. In practice that means two situations: a computer task that should be automated, and a first working version that should exist quickly rather than after a long build cycle.

    The introductory documentation names the jobs directly:

    The job Why Python is offered for it
    Search and replace over a large number of text files The work is repetitive and mechanical.
    Renaming and rearranging many photo files in a complicated way The rule is easy to describe, tedious to apply by hand.
    A small custom database The program is real, but it does not need a large toolchain.
    A graphical application or a simple game The result is interactive, not a one-line command.
    A test suite for a C, C++ or Java library Writing test code should not be slower than the code it tests.
    A command language inside an existing application The application needs programmability without inventing a language.
    1 / 20
  2. Which problems Python is built for

    Those six jobs look unrelated, yet they share a shape. Each one is either work a person is doing by hand and should stop doing by hand, or work whose first draft is being delayed by the write, compile, test and re-compile cycle.

    That is the honest way to decide whether Python fits a problem. Ask what the input is, what output is wanted, and how often it must happen. If the answer is "often, and the rule is easy to state", the task belongs to a program. If the answer is "once, and I only need to move a file", a shell command may still be the shorter route.

    2 / 20
  3. Quick check

    A team must rename and rearrange thousands of photo files by a complicated rule, and wants to repeat the work next month. Which description fits the job Python is meant for?

    1. AA graphics toolkit that edits each image by hand

      Editing each image by hand is exactly the manual work the example replaces.

    2. BAutomating a repeated file task in a programming language

      Right. Renaming and rearranging many photo files in a complicated way is one of the documented tasks Python takes on.

    3. CA compile-and-link build step that runs before any file is touched

      Interpreted execution means no separate compilation and linking stage stands between writing the rule and running it.

    3 / 20

  4. More than a shell script, simpler than C

    You could write a Unix shell script or a Windows batch file for some of these tasks. Shell scripts are best at moving files around and changing text data, and they are weak once the job becomes a graphical application or a game. You could also write a C, C++ or Java program, but getting even a first-draft program can take a lot of development time.

    Python sits between those two options, and stays a real programming language while doing so.

    Option Strength Where it runs out
    Shell script or batch file Moving files, changing text data Little structure for a program that grows
    C, C++ or Java Control and speed A first draft costs a lot of development time
    Python Structure plus a fast first draft Chosen when readability and iteration matter
    4 / 20
  5. More than a shell script, simpler than C

    Two properties do most of the work in that middle position.

    • Python provides more structure and support for large programs than shell scripts or batch files. Programs can be split into named units instead of growing into one long file.
    • Python supplies high-level built-in data types, including flexible arrays and dictionaries, that help express complex operations compactly. Because its data types are general, Python applies to a much wider problem domain than tools such as Awk or Perl, while many of the same things stay at least as easy.

    Python also offers much more error checking than C. "Simple to use" is not the same as "limited": the same language covers the small automation job and the program it grows into.

    5 / 20
  6. Quick check

    A short shell script has grown into a program with several parts that other people now edit. What does moving it to Python offer?

    1. AMore structure and support for large programs

      Right. Python is a real programming language and offers far more structure and support for large programs than shell scripts or batch files.

    2. BFewer built-in data types, so the program must stay small

      Python adds high-level built-in types such as flexible arrays and dictionaries rather than removing them.

    3. CA guarantee that the program only moves files and changes text

      Moving files and changing text is where shell scripts are strongest; Python covers a much wider problem domain.

    6 / 20

  7. Why the development loop is short

    Python is an interpreted language, and Python programs do not require a separate compilation and linking step before the interpreter can run them. For development, that changes the rhythm of the day. An edit can be tried immediately, so a wrong assumption is discovered in seconds instead of after a build.

    The second half of that benefit is the interactive prompt. The interpreter can also be used interactively to experiment with language features, create throw-away programs, and test functions during development. It doubles as a handy desk calculator.

    7 / 20
  8. Why the development loop is short

    Put together, the two properties turn development into a conversation rather than a series of builds. A function can be written, called with one awkward value, corrected and called again before it ever becomes part of a program. That is what bottom-up development means in practice: the small pieces are known to work before the whole is assembled.

    You want to Interpreted execution gives you
    Try a language feature you are unsure about An immediate answer at the prompt
    Check one function while building a program A test of that unit alone, bottom-up
    Write something you will throw away No project scaffolding to set up first
    Re-run after an edit The run itself, with no build stage in between
    8 / 20
  9. Quick check

    A developer wants to test single functions as they are written, without waiting for a build after each edit. Which capability answers both needs?

    1. ACompile and link before each test

      Python needs no separate compilation and linking step, so this is the cycle being avoided rather than the answer.

    2. BWait until the whole application is finished

      Interactive use exists precisely so features and functions can be tried before a full program exists.

    3. CUse the interpreter interactively during development

      Right. The interpreter can be used interactively to experiment with language features, write throw-away programs, and test functions during development.

    9 / 20

  10. 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.

  11. Programs that stay short and readable

    Programs written in Python are typically much shorter than equivalent C, C++ or Java programs. Three properties explain most of that difference, and all three are worth recognizing early because they shape how Python code looks.

    • High-level data types can express complex operations in one statement, so a loop and a temporary structure often collapse into a single line.
    • Indentation groups statements, instead of beginning and ending brackets. Layout and meaning are the same thing.
    • Variables and arguments need no declarations, so a small experiment can be written without ceremony.

    Shorter is not weaker here. Error checking is stronger than in C, and the language still offers modules, classes and exceptions when the program needs them.

    10 / 20
  12. Quick check

    Why is a Python program typically shorter than the equivalent C or Java program?

    1. AEvery variable and argument carries an explicit declaration

      No variable or argument declarations are necessary, which is one of the reasons Python programs are shorter.

    2. BHigh-level data types express complex operations in a single statement

      Right. High-level built-in types let one statement carry work that would take several elsewhere.

    3. CStatements are grouped with beginning and ending brackets

      Python groups statements by indentation rather than by beginning and ending brackets.

    11 / 20

  13. One language across your machines

    Python is available on Windows, macOS, and Unix operating systems. For a small tool that has to work on a colleague's laptop, your own machine and a server, that availability is the difference between one program and three platform-specific rewrites.

    Portability is a property of the language and its interpreter, not a promise that every detail of an operating system disappears. What it does guarantee is that the program itself, and everything you learn about writing it, travels between those systems.

    12 / 20
  14. One language across your machines

    Portability is only useful if the program can actually reach the system it runs on, and that is the job of the modules that come with Python. Python's standard modules include facilities for file input and output, system calls, sockets, and graphical user interfaces — including interfaces to toolkits such as Tk. Those modules can serve as the basis of your programs, or simply as examples while learning.

    The practical effect is that a first version rarely has to invent common machinery. Reading a file, talking to the operating system or opening a socket is already available, which is a large part of why the first draft arrives quickly.

    13 / 20
  15. Quick check

    A small tool must run on a colleague's Windows laptop, your macOS machine and a Linux server. What does Python's availability mean for that plan?

    1. AThe tool must be rewritten as a shell script for each of the three systems

      Rewriting per platform is what a platform-specific shell script would force; Python does not.

    2. BOnly the Unix machines can run it, so the laptop needs a different tool

      Python is available on Windows and macOS as well as Unix systems.

    3. CThe same language covers all three families

      Right. Python is available on Windows, macOS and Unix operating systems, so one language serves the whole set.

    14 / 20

  16. Working with code that is not Python

    A project rarely starts from nothing. There may be a C library that must stay where it is, or an application that needs to become programmable. Python can be extended with functions or modules implemented in C or C++, and its interpreter can be embedded in another application as an extension or command language.

    Those are two directions of the same door:

    Direction What it means When it matters
    Extending A new built-in function or module written in C is added to the interpreter A critical operation must run at maximum speed, or a library exists only in binary form
    Embedding The interpreter is linked into an application written in C The application should be scriptable without designing a whole new language
    15 / 20
  17. Working with code that is not Python

    Embedding is the case that answers the very first motivation on the list: a program that could use an extension language, whose author does not want to design and implement a whole new one for it.

    Extending answers a different worry. Choosing Python for the program as a whole does not mean giving up existing native code — a vendor-specific graphics library available only in binary form, or an operation that must run at maximum speed, can stay in C and be reached from Python. The high-level language and the low-level code cooperate instead of competing for the whole project.

    16 / 20
  18. Quick check

    A C application needs a programmable command language, and one speed-critical operation should stay in C. Which design matches Python's extensibility?

    1. AEmbed the interpreter and add the C operation as a new built-in module

      Right. The interpreter can be linked into a C application as an extension or command language, and a new built-in function or module can carry the critical operation.

    2. BReplace the C operation with a batch file and drop the programmable interface

      A batch file provides neither the embedded programmable interface nor a home for the C operation.

    3. CRewrite the host application in a shell language

      Shell scripts are best at moving files and changing text; rewriting the host in one abandons both requirements.

    17 / 20

  19. Key takeaways

    Choosing a language is a judgement, not a slogan, so it is worth stating when Python is the right answer and when it is not. Python fits when a task benefits from readable code, quick experimentation, automation, portability across the major desktop operating systems, or reuse through modules. A shell script may remain appropriate for narrowly moving files or changing text data, and a lower-level compiled language may be chosen under other constraints.

    18 / 20
  20. Key takeaways

    • Python combines high-level programming structure with interpreted execution for scripting and rapid application development. It is chosen when readable code, quick experimentation, automation, portability or reuse through modules matter.
    • The interactive interpreter supports experimentation and function testing without a separate compile-and-link cycle, so ideas can be validated before they become maintained programs.
    • Python spans automation, reusable programs, and extensible applications on Windows, macOS, and Unix. One language carries the small job and the program it becomes.
    • The built-in reach — file input and output, system calls, sockets, graphical interfaces — plus extension and embedding in C means a program can grow beyond its first expression without recreating every common facility.
    19 / 20
  21. Quick check

    A team needs a portable first draft of a small graphical application, and more program structure than a batch file can give. Which summary of Python fits both constraints?

    1. AA platform-specific command language for moving files

      That describes a shell script's strength, and it fails both the graphical draft and the structure the team asked for.

    2. BA full high-level language, available on the major desktop systems

      Right. Python is a real programming language with far more structure than a batch file, and it is available on Windows, macOS and Unix.

    3. CA build tool that produces a compiled application for one operating system

      Python runs through its interpreter with no separate compile-and-link step, and it is not tied to a single operating system.

    20 / 20

  22. 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.