•By Sumit Pandey, PhD•3 min read

Python for Idiots: Write Trash, Get Results

Write and run your first Python script with print, a list and random.choice. Start with something scrappy that works, then learn variables, loops and functions.

Updated

Stop Writing "Clean" Code

If you tell a software engineer you learned Python, they will immediately annoy you with words like "SOLID principles," "Type Hinting," and "Design Patterns."

Ignore them.

They are optimizing for a future that doesn't exist yet. You are optimizing for right now.

Python is Just Glue

Python is not a language for building cathedrals. It is duct tape. You use it to stick two things together (like a spreadsheet and an email) to solve a problem.

Does duct tape look pretty? No. Does it hold the bumper on your 2004 Toyota Camry? Yes.

The "Hello World" Test

Java (The "Smart" Way)

Look at this mess. You need a class, a method, a static void main... just to say hi.

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Python (The Idiot's Way)

print("Hello World")

See? Python respects your laziness.

Permission to Be Messy

Here is the secret: The most successful code is usually a mess.

Facebook started as messy PHP. Google started as messy Python. Your startup will die if you spend 3 weeks designing a "UserFactoryAbstractInterface."

The Idiot's Style Guide

  1. Don't write classes. Just write functions. Or better yet, just write lines of code.
  2. Copy-paste is your friend. Inheritance is confusing. CMD+C / CMD+V works every time.
  3. Variable names don't matter. x, df, stuff, if you know what it is, it's fine.

Your First "Script"

Don't build an "app." Write a script that does one dumb thing.

import random
 
excuses = [
    "My wifi died",
    "My dog ate the server",
    "I am currently migrating to the cloud (napping)"
]
 
print(random.choice(excuses))

Congratulations. You are now a Python developer. Go break something.

Run the thing

With Python 3 installed, save the code above as excuses.py. Open a terminal in that folder and run:

python3 excuses.py

On Windows with the Python launcher installed, use py excuses.py. The Python interpreter guide covers the available launch commands. No packages to install: random comes with Python.

You should see one excuse from the list. Run it again. It might pick the same excuse; random does not mean taking turns. Add your own line to the list and run it once more. That is the whole edit, run, inspect loop.

When this script stops feeling mysterious, move on to variables and lists, loops and decisions, then functions. Messy is a starting point. Once other people depend on your code, readable names and tests earn their keep.

Share this article