Edna's Python Archives

Beginner Python — clear notes from the archives

One random Random 3 Browse all
  • variable

    Basics

    What this creature is

    A variable is a name you give to a value stored in memory. You assign a value with = and can use that name later in your program.

    Why you will regret ignoring it

    Variables let you reuse values, update them, and keep your code readable. Without them, you would repeat the same literals everywhere.

    Example

    name = "Alex"
    age = 20

    Common disgrace

    Using the same variable name for different things in one program, then getting confused when the value is not what you expect.

    Trial by code

    Create two variables: one for a word (string) and one for a number. Print both on one line.

  • string

    Data types

    What this creature is

    A string is text inside quotes (single or double). It can include letters, digits, spaces, and symbols.

    Why you will regret ignoring it

    Most user-facing data is text. Python uses strings for names, messages, and anything you read or type as words.

    Example

    greeting = "Hello, world."
    print(greeting)

    Common disgrace

    Starting a string with one kind of quote and ending with another, or leaving a string unclosed, which causes a syntax error.

    Trial by code

    Store a short sentence in a variable. Print it and print its length using len(...).

  • integer

    Data types

    What this creature is

    An integer is a whole number (no decimal part). You use it for counting, indexing, and math where fractions are not needed.

    Why you will regret ignoring it

    List positions, scores, and loop counts are often integers. Mixing integers and floats on purpose is fine; mixing them by accident causes bugs.

    Example

    items = 3
    total = items + 2

    Common disgrace

    Expecting division of two integers to behave like “real” division in every case — in Python 3, / gives a float; use // for integer division when you need it.

    Trial by code

    Set a variable to 10, subtract 3, and print the result.

  • float

    Data types

    What this creature is

    A float is a number with a decimal point. Use it for measurements, ratios, and any value that is not a whole number.

    Why you will regret ignoring it

    Many real-world values (money, science, graphics) need decimals. Floats are the usual way to represent them in Python.

    Example

    pi_approx = 3.14
    half = 0.5

    Common disgrace

    Comparing two floats with == when they came from math operations — tiny rounding differences can make them not exactly equal.

    Trial by code

    Store two floats, multiply them, and print the result rounded to two decimal places.

  • list

    Collections

    What this creature is

    A list is an ordered sequence in square brackets. Items have positions (indexes) starting at 0. You can add, remove, and loop over items.

    Why you will regret ignoring it

    Lists let one variable hold many values in order — for example, lines of text, scores, or names.

    Example

    words = ["variable", "string", "loop"]
    print(words[0])

    Common disgrace

    Thinking the first item is at index 1. In Python, the first item is at index 0.

    Trial by code

    Make a list of three strings. Print the last item using a negative index.

  • if statement

    Control flow

    What this creature is

    An if statement runs a block of code only when a condition is true. You can add elif for other tests and else for a default.

    Why you will regret ignoring it

    Programs need to choose what to do. if lets you branch based on values, user input, or errors.

    Example

    score = 80
    if score >= 50:
        print("Pass")
    else:
        print("Review")

    Common disgrace

    Forgetting the colon (:) after if / elif / else, or not indenting the block under the condition — both cause syntax errors.

    Trial by code

    Write an if/else that prints one message if a number is greater than 0 and another if it is not.

  • loop

    Control flow

    What this creature is

    A loop repeats code. A for loop goes through each item in a sequence. A while loop runs while a condition stays true.

    Why you will regret ignoring it

    Loops save you from copying the same code many times and let you process lists, files, or ranges efficiently.

    Example

    for word in ["a", "b", "c"]:
        print(word)

    Common disgrace

    Writing a while loop whose condition never becomes false, so the program runs forever until you stop it.

    Trial by code

    Use a for loop to print the numbers 1 through 5 (hint: range can help).

  • function

    Structure

    What this creature is

    A function is a named block of code you define once and call by name. It can take parameters and return a value with return.

    Why you will regret ignoring it

    Functions organize logic, avoid repetition, and make programs easier to test and fix.

    Example

    def greet(name):
        return "Hello, " + name
    print(greet("visitor"))

    Common disgrace

    Calling a function before Python has read its def, or using print when you meant to return a value to use later.

    Trial by code

    Define a function that doubles a number and returns it. Call it with 21 and print the result.

  • input

    I/O

    What this creature is

    input() reads one line of text from the user (usually the terminal) and returns it as a string.

    Why you will regret ignoring it

    It lets programs respond to what the user types, which is essential for simple interactive programs.

    Example

    name = input("Your name: ")
    print("Welcome,", name)

    Common disgrace

    Treating input as a number without converting — input always returns a string until you convert with int(), float(), etc.

    Trial by code

    Ask for two words using input() and print them on one line with a space between them.

  • print

    I/O

    What this creature is

    print() writes values to standard output (usually the terminal) as text. It adds a newline at the end unless you change end.

    Why you will regret ignoring it

    Printing is the simplest way to see what your program is doing while you learn and debug.

    Example

    print("Done.")
    print(42, "items", sep=" | ")

    Common disgrace

    Printing inside a tight loop and producing so much output that you cannot find the line you care about.

    Trial by code

    Print three values on one line using print with sep and end set to values you choose.

Until next time.