Python Basics

Lessons Python Book Chapter 2
Python Book · Chapter 2

Python Basics

Before we build drifting stars and bouncing colors, we need a tiny bit of Python vocabulary. Not theory — just the pieces you’ll actually touch when you tweak your projects.

Chapter 2 of 6

Variables: your project’s memory.

A variable is just a name that remembers a value. In Python, you don’t declare types — you just assign.

speed = 5
color = (255, 200, 50)
message = “Hello, sandbox!”

You’ll tweak variables constantly: star speed, ball size, color values, gravity strength, chaos slider defaults — everything fun lives here.


Lists: groups of things.

A list is a container that holds multiple values. You’ll use them for stars, balls, particles, and anything that moves in groups.

stars = []
colors = [(255,0,0), (0,255,0), (0,0,255)]

In Drifting Stars, every star is stored in a list. The game loop updates each one every frame.


Loops: the heartbeat of animation.

Every Pygame project runs inside a loop. This loop updates positions, checks events, and redraws the screen.

for star in stars:
  star.x += star.speed
  star.y += star.drift

If HTML was “refresh the page,” Python is “refresh the window 60 times per second.”


Functions: name a behavior.

Functions let you group steps together and reuse them. You’ll use them for drawing stars, updating motion, and handling collisions.

def move_star(star):
  star.x += star.speed
  star.y += star.drift

You don’t need to write many functions yourself — AI will generate most of them. You’ll just tweak numbers inside.


If statements: react to the world.

If statements let your project respond to conditions — bouncing off walls, changing colors, triggering chaos.

if star.x > 800:
  star.x = 0

You’ll use these constantly in Bouncing Colors and the Chaos Equalizer.


Your first moving dot.

This tiny script uses everything above: variables, loops, and drawing. It’s your warm‑up before Drifting Stars.

import pygame
pygame.init()

screen = pygame.display.set_mode((800, 600))
x = 100
speed = 3

running = True
while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = False

  x += speed
  if x > 800:
    x = 0

  screen.fill((10,10,30))
  pygame.draw.circle(screen, (255,200,50), (x,300), 20)
  pygame.display.flip()

pygame.quit()

If you see a glowing dot sliding across the screen — congratulations. You now know enough Python to build every project in this book.

You’re in Python Book · Chapter 2 of 6
Play-First Programming · playfirstprogramming.com A clubhouse for curious builders