Programming Fundamentals

Getting Started

↩ Back
01

What Programming Is

Definition
Program

A program is an ordered list of instructions. The machine reads them one at a time, from top to bottom, and does what each one says.

Defining traits
OrderedSwapping two lines can change the result, or break the program.
LiteralThe machine follows the written instruction, never the intention behind it.
RepeatableSame instructions and same input produce the same output on every run.
Concept
Write the Algorithm Before the Code

An algorithm is the recipe: the steps stated in plain language. Code is that recipe rewritten in a language the machine accepts.

1
State the problem. Find the largest of three numbers.
2
Write the steps in words, in order, with no gaps.
3
Translate each step into Python.
4
Run it, read the output, correct what differs from what you wanted.
most mistakes are a gap in step 2, not in step 3
Example
Largest of Three Numbers
Algorithm, in words
Program, in Python
1
Take three numbers: a, b, c.
a = 7 b = 15 c = 9
2
Compare a and b. Keep whichever is larger.
larger_of_first_two = max(a, b)
3
Compare that result with c. Keep whichever is larger.
largest = max(larger_of_first_two, c)
4
What remains is the largest of the three.
print("largest:", largest)
Output
largest: 15
Interactive
Run hello.py
watches the file move through every stage, one line at a time
hello.py
name = "Ada" print("Hello,", name) print("Nice to meet you")
Console
console
press Run to execute hello.py
SOURCE CODE hello.py 3 lines, waiting you run it PYTHON INTERPRETER reads one line checks the rules carries it out then moves to the next result OUTPUT ··· printed to the console

Python reads your file while it runs, so nothing happens until you ask it to run, and a mistake on line 40 stays quiet until the first 39 lines have already done their work. Notice the first line stores a value and prints nothing at all: only print writes to the console.

02

What Python Is

Definition
Python

A high level, interpreted, dynamically typed programming language created by Guido van Rossum in 1991 and built around readable syntax.

high level interpreted dynamically typed general purpose large library ecosystem

High levelYou write what to do, and memory management is handled for you.
InterpretedNo separate compile step: you save the file and run it.
Dynamic typesA variable takes the type of whatever value you put in it.
General purposeThe same language covers scripts, websites, data analysis, and machine learning, not one narrow niche.
Large ecosystemReady-made packages exist for almost anything, installed with a single command instead of written from scratch.
Comparison
Python against C and Java
Python C / Java
Before runningrun the file straight awaycompile first, then run
Typesattached to values at run timedeclared in the code
Execution speedslowerfaster
Lines for a small taskfewmore
Common usedata, scripting, prototyping, AIsystems, performance critical software

Python trades raw speed for speed of writing. In most of what you will do here, your own time costs more than the machine's.

Example
The same program in two languages
Python
print("Hello, world!")
Java
public class Main { public static void main(String[] args) { System.out.println("Hello, world!"); } }

Both write the same line. Python asks you to name the action and nothing more, which is one reason it is so often chosen as a first language.

03

Where You Write Code

Interactive
Pick an environment

A plain .py file. Text saved on disk, launched from a terminal with python hello.py. The whole file runs from the first line to the last, every time.

✓ Good forprograms meant to be reused, shared, or scheduled
✗ Awkward fortesting one small idea and seeing the result immediately
Try it
print("Order received") print("Total: $12.50")
console
press Run to see the output
Comparison
Scripts against notebooks
Script (.py) Notebook (.ipynb)
Unit you runthe whole fileone cell
Order of executionalways top to bottomwhichever order you click
Where results appearthe terminalunder each cell
Prose and imagescomments onlytext cells, plots, tables
Version controlcleanmessy, since output is stored inside the file

Explore in a notebook, ship in a script. Most people use both and move code from one to the other.

04

Printing with print()

Definition
The print function

A built in function that writes its arguments to the console and then moves to a new line.

print(value1, value2, ..., sep=" ", end="\n")
TextQuotes mark a string: print("hi") writes hi.
NumbersNo quotes needed: print(7 * 6) writes 42.
Several valuesSeparate them with commas and Python inserts a space between them.
Rules
Three things beginners hit
1
Quotes matter. print("2 + 2") writes the text 2 + 2, while print(2 + 2) writes 4.
2
Parentheses are part of the call. Writing print "hi" is a syntax error in Python 3.
3
Names are case sensitive. Print and PRINT are different names, and Python knows neither.

Changing the defaults
print("a", "b", "c", sep="·") # joined with a dot instead of a space print("loading", end="...") # stays on the same line instead of starting a new one
Interactive
Print playground
Write print statements, then run them
strings, numbers, + − * / // % **, sep=, end=
console
press Run to see the output

This console understands print statements, which is all today's topic needs. Remove a quote or a parenthesis to see what an error message looks like.

05

Commenting Code

Definition
Comment

Text inside a program that Python ignores. Everything after a # on a line is skipped, so comments cost nothing when the program runs.

# a full line comment print("hi") # an inline comment
Explain whyThe code already shows what happens. A comment carries the reason.
Mark sectionsA short line above a block tells the reader where they are.
Disable a linePut # in front of a line to stop it running while you test.
Interactive
What Python actually reads
# Ticket prices for the school trip # Values fixed by the council in March print("Ticket prices") print("Adult:", 12) # in euros print("Child:", 12 / 2) # half of the adult price # print("Student:", 12 * 0.8) waiting for approval

With the box ticked you see the program as the interpreter sees it. Comments are written for the next person reading the file, and that person is often you in three months.

Check yourself
Which comment earns its place?
The line being commented
print("Total:", 19.99 * 1.21)
Pick one.
06

Summary

Recap
What you can do now
1
Describe a program as an ordered, literal list of instructions, and write the algorithm before the code.
2
Say what makes Python high level, interpreted, and dynamically typed, and when that helps.
3
Choose between a script and a notebook, knowing both run the same Python.
4
Print text, numbers, and several values at once, and adjust sep and end.
5
Write comments that give the reason behind a line instead of repeating it.