Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
GimmeJob
Sign in
Programming · Python · Chapter 01 / 16

Programming learning path

Getting started & syntax

Running Python, the REPL, variables, comments and script structure.

Running Python and using the REPL

Two ways to run Python code: an interactive REPL for quick experiments, and script files for real programs.

Typing python (or python3) with no arguments opens the REPL — Read-Eval-Print Loop — where each input is executed immediately, and when that input is a bare expression, its value is automatically displayed; statements such as assignments execute without automatically printing anything, which is exactly why name = "Ada" in the example below produces no visible output on its own line. Real programs live in .py files and are run with python filename.py. Modern editors and IDEs also offer a Jupyter-style notebook cell interface, but the REPL and plain scripts are the two forms every Python programmer needs to be comfortable with first.

Key points

  • The REPL executes each input immediately, but only a bare expression's value is displayed automatically
  • A .py script is run with python filename.py from a terminal
  • Exit the REPL with exit(), quit(), or Ctrl-D (Ctrl-Z then Enter on Windows)

Code

>>> 2 + 2
4
>>> name = "Ada"
>>> print(f"Hello, {name}!")
Hello, Ada!
>>> exit()

A short REPL session: an expression, a variable assignment, and a formatted greeting.

Common pitfalls

  • REPL state is lost when you close it — anything worth keeping belongs in a .py file
  • The REPL echoes bare expression results automatically, but a script never does unless you call print()

Practice exercise

Open a REPL, compute the area of a circle with radius 4 (use 3.14159 for pi), then save the same calculation as a script called area.py that prints the result.

How print() actually works — separators, line endings, multiple values — and how to read a line of user input.

print() accepts any number of positional arguments, converts each to a string, joins them with sep (a single space by default) and finishes the line with end (a newline by default) — both are keyword arguments you can override. input("prompt: ") displays a prompt, blocks until the user types a line and presses Enter, and always returns that line as a str, even if the user typed digits, so numeric input needs an explicit conversion like int(input(...)).

Key points

  • print(a, b, c) joins arguments with sep (default single space)
  • print(..., end="") suppresses the trailing newline
  • input() always returns a str; convert explicitly for numbers

Code

name = input("Your name: ")
age = int(input("Your age: "))
print("Name", "Age", sep=" | ")
print(name, age, sep=" | ")

Reading two pieces of input and printing them as an aligned pair of rows.

Common pitfalls

  • Forgetting int()/float() around input() causes '5' + '3' to produce '53', not 8
  • input() blocks forever waiting for a line — it never times out on its own

Practice exercise

Write a script that asks for two numbers with input(), converts them to float, and prints their sum, difference and product on three separate lines.

Comments, docstrings and readable style

When to write a comment, when to write a docstring, and the naming/spacing conventions from PEP 8.

A # comment explains why code does something non-obvious; it is stripped before execution and invisible to help(). A triple-quoted docstring as the first statement in a module, function or class instead documents what that object does and is stored on __doc__, discoverable at runtime. PEP 8 is Python's official style guide: 4 spaces per indentation level, with spaces preferred over tabs (tabs are mainly for staying consistent within an already tab-indented codebase, and mixing the two for indentation is disallowed); its default maximum line length is 79 characters for code (72 for comments and docstrings), though a team may deliberately agree on a longer limit up to 99 — conventions followed closely enough across the ecosystem that most codebases read consistently.

Key points

  • # comments explain why; docstrings document what, and are readable via __doc__/help()
  • PEP 8: 4-space indents, snake_case names, PascalCase classes
  • Consistent style across a codebase matters more than any individual preference

Code

Python
editable · browser sandbox
Result
Run the code to see the result.

A docstring documenting the function's purpose, and a comment explaining a non-obvious detail.

Common pitfalls

  • A comment like # increment i by one above i += 1 repeats what the code already says.
  • Mixing tabs and spaces for indentation raises a TabError in Python 3

Practice exercise

Write a function is_leap_year(year) with a one-line docstring, a comment explaining the century-divisible-by-400 rule, and PEP 8-compliant naming.

The shape of a real Python script

Why real scripts put logic inside functions and guard the entry point with if __name__ == "__main__":

Every .py file has a special variable __name__, which Python sets to "__main__" when that file is run directly, but to the module's actual name when it is imported by another file. Wrapping a script's entry logic in if __name__ == "__main__": means the file can be run directly for its own behavior, but can also be safely imported elsewhere to reuse its functions without that entry logic running unintentionally. This, combined with keeping real logic inside functions rather than loose at module level, is the standard shape of a maintainable Python script.

Key points

  • __name__ is "__main__" only when the file is run directly, not when imported
  • if __name__ == "__main__": guards code that should not run on import
  • Keeping logic inside functions makes it testable and reusable, unlike loose module-level code

Code

Python
editable · browser sandbox
Result
Run the code to see the result.

A script that can be run directly or imported for its main() function without side effects.

Common pitfalls

  • Loose top-level code without the __main__ guard runs immediately on every import, even in tests
  • Forgetting main() entirely and writing everything at module level makes the script hard to test or reuse

Practice exercise

Turn a flat script that prints the first 10 square numbers into one with a square_numbers(n) function and a __main__ guard, then import it from a second file without anything printing automatically.

Source registry

3 chapter references
The Python Tutorial

Baseline syntax, control flow, functions and module basics

Python Software Foundation · Official documentation
Source ↗
PEP 8 — Style Guide for Python Code

Naming, layout and readability conventions

Python Software Foundation · Official specification
Source ↗
The import system

Module resolution, sys.path, packages and import caching

Python Software Foundation · Official reference
Source ↗