๐ Programming Basics#
โPrinting โhello worldโ is cute, but letโs get realโprogramming is your secret weapon to control the digital realm. ๐๐ปโ
๐ฉโ๐ป Computer Programs in a Nutshell#
๐ ๏ธ Takes some input from the user (or a nosy robot).
๐ง Processes it with computations (the brainy part).
๐ Spits out something useful (or at least tries to).
๐ TL;DR: A program is just an obedient machine that does whatever you tell itโliterally, whatever. So choose your words wisely. ๐
๐ The First Programmer (Respect, Ada)#
Ada Lovelace, 1815
She wasnโt just writing code; she was predicting the future! One of her brilliant feats was sketching out how to calculate Bernoulli numbersโbefore computers were even a thing. Mind = Blown. ๐คฏ
Also: Charles Babbageโs Analytical Engine
It was like the prehistoric ancestor of your laptop. Check out this beauty:
๐ง Computational Thinking#
Humans vs. Machines
๐ง Humans: Flexible, creative, and occasionally a little messy.
๐ค Computers: Literal, logical, and absolutely unforgiving.
Takeaway: When programming, channel your inner robot and get comfy with giving painfully specific instructions. Precision is the name of the game. ๐ฏ
โ๏ธ ExampleโDrawing a Square#
You: Pick up a pen, doodle a square, done. Easy, right? โ๏ธ
Computer: โWhatโs a square?โ ๐ต
To make a computer draw one, you have to:
Start at a specific point.
Draw a straight line of fixed length.
Turn exactly 90ยฐ.
Repeat the process until you close the shape.
Pro Tip: Computers are brilliant at following orders but terrible at improvising. ๐
import turtle
# Set up the screen
screen = turtle.Screen()
screen.setup(width=600, height=600)
screen.bgcolor("white")
# Create the turtle
pen = turtle.Turtle()
pen.speed(1)
# Draw a square
for _ in range(4):
pen.forward(100) # Move forward 100 units
pen.right(90) # Turn 90 degrees
# Finish up
turtle.done()
More Advanced Example#
turtle.setup(width=600, height=500)
turtle.reset()
turtle.hideturtle()
turtle.speed(0)
turtle.bgcolor("black")
c = 0
x = 0
colors = [
# reddish colors
(1.00, 0.00, 0.00),
(1.00, 0.03, 0.00),
(1.00, 0.05, 0.00),
(1.00, 0.07, 0.00),
(1.00, 0.10, 0.00),
(1.00, 0.12, 0.00),
(1.00, 0.15, 0.00),
(1.00, 0.17, 0.00),
(1.00, 0.20, 0.00),
(1.00, 0.23, 0.00),
(1.00, 0.25, 0.00),
(1.00, 0.28, 0.00),
(1.00, 0.30, 0.00),
(1.00, 0.33, 0.00),
(1.00, 0.35, 0.00),
(1.00, 0.38, 0.00),
(1.00, 0.40, 0.00),
(1.00, 0.42, 0.00),
(1.00, 0.45, 0.00),
(1.00, 0.47, 0.00),
# orangey colors
(1.00, 0.50, 0.00),
(1.00, 0.53, 0.00),
(1.00, 0.55, 0.00),
(1.00, 0.57, 0.00),
(1.00, 0.60, 0.00),
(1.00, 0.62, 0.00),
(1.00, 0.65, 0.00),
(1.00, 0.68, 0.00),
(1.00, 0.70, 0.00),
(1.00, 0.72, 0.00),
(1.00, 0.75, 0.00),
(1.00, 0.78, 0.00),
(1.00, 0.80, 0.00),
(1.00, 0.82, 0.00),
(1.00, 0.85, 0.00),
(1.00, 0.88, 0.00),
(1.00, 0.90, 0.00),
(1.00, 0.93, 0.00),
(1.00, 0.95, 0.00),
(1.00, 0.97, 0.00),
# yellowy colors
(1.00, 1.00, 0.00),
(0.95, 1.00, 0.00),
(0.90, 1.00, 0.00),
(0.85, 1.00, 0.00),
(0.80, 1.00, 0.00),
(0.75, 1.00, 0.00),
(0.70, 1.00, 0.00),
(0.65, 1.00, 0.00),
(0.60, 1.00, 0.00),
(0.55, 1.00, 0.00),
(0.50, 1.00, 0.00),
(0.45, 1.00, 0.00),
(0.40, 1.00, 0.00),
(0.35, 1.00, 0.00),
(0.30, 1.00, 0.00),
(0.25, 1.00, 0.00),
(0.20, 1.00, 0.00),
(0.15, 1.00, 0.00),
(0.10, 1.00, 0.00),
(0.05, 1.00, 0.00),
# greenish colors
(0.00, 1.00, 0.00),
(0.00, 0.95, 0.05),
(0.00, 0.90, 0.10),
(0.00, 0.85, 0.15),
(0.00, 0.80, 0.20),
(0.00, 0.75, 0.25),
(0.00, 0.70, 0.30),
(0.00, 0.65, 0.35),
(0.00, 0.60, 0.40),
(0.00, 0.55, 0.45),
(0.00, 0.50, 0.50),
(0.00, 0.45, 0.55),
(0.00, 0.40, 0.60),
(0.00, 0.35, 0.65),
(0.00, 0.30, 0.70),
(0.00, 0.25, 0.75),
(0.00, 0.20, 0.80),
(0.00, 0.15, 0.85),
(0.00, 0.10, 0.90),
(0.00, 0.05, 0.95),
# blueish colors
(0.00, 0.00, 1.00),
(0.05, 0.00, 1.00),
(0.10, 0.00, 1.00),
(0.15, 0.00, 1.00),
(0.20, 0.00, 1.00),
(0.25, 0.00, 1.00),
(0.30, 0.00, 1.00),
(0.35, 0.00, 1.00),
(0.40, 0.00, 1.00),
(0.45, 0.00, 1.00),
(0.50, 0.00, 1.00),
(0.55, 0.00, 1.00),
(0.60, 0.00, 1.00),
(0.65, 0.00, 1.00),
(0.70, 0.00, 1.00),
(0.75, 0.00, 1.00),
(0.80, 0.00, 1.00),
(0.85, 0.00, 1.00),
(0.90, 0.00, 1.00),
(0.95, 0.00, 1.00),
]
while x < 1000:
idx = int(c)
color = colors[idx]
turtle.color(color)
turtle.forward(x)
turtle.right(98)
x = x + 1
c = c + 0.1
turtle.exitonclick()