Output
print shows a value so you can see it.
print("Sankofa Code")
print(52 * 7)- JavaScript writes console.log() instead.
Foundations reference
Variables through algorithms, with the JavaScript equivalent noted beside each idea.
Python reads close to English and uses indentation where other languages use braces. Everything below runs in the editor on any lesson page.
print shows a value so you can see it.
print("Sankofa Code")
print(52 * 7)A name for a value. Python needs no keyword at all.
score = 10
score = 25 # reassigned
NAME = "Amara" # capitals mean do not change thisA hash starts a comment. Indentation is not style here, it is the syntax.
# a note for a human
if score > 10:
print("inside the if")
print("outside again")Whole numbers, decimals, text, booleans and the absence of a value.
42 # int
3.14 # float, a separate type
"text" # str
True / False # bool, capitalised
None # nothingText. An f-string drops values straight in.
name = "Amara"
greeting = f"Hello, {name}"
len(name) # 5
name.upper() # "AMARA"
name[0] # "A"
" hi ".strip() # "hi"
"a,b,c".split(",") # ["a", "b", "c"]Input arrives as text. Convert before doing arithmetic.
int("42") # 42
float("3.5") # 3.5
str(42) # "42"
"7" + "3" # "73" joined
int("7") + 3 # 10 added
"42".isdigit() # check before convertingAn ordered collection. JavaScript calls the same idea an array.
names = ["Ama", "Kofi", "Zuri"]
names[0] # "Ama"
len(names) # 3
names.append("Nia")
"Ama" in names # True
names[0:2] # a copy of the first twoNamed fields. JavaScript calls the same idea an object.
member = {"name": "Ama", "hours": 40}
member["name"] # "Ama"
member.get("city", "?") # a safe default
member["city"] = "Atlanta"
member.keys()A set holds unique values. A comprehension builds a list in one line.
unique = set(names) # duplicates removed
evens = [n for n in nums if n % 2 == 0]
doubled = [n * 2 for n in nums]
sorted(members, key=lambda m: m["hours"], reverse=True)Ordered tests. The first true branch wins, so put the most specific first.
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "Keep going"for walks a collection directly. while repeats until a condition stops being true.
for n in numbers:
total += n
for i in range(5): # 0 to 4
print(i)
while balance < goal:
balance += 75Wrap risky work, handle the failure, and let finally clean up either way.
try:
n = int(text)
except ValueError:
n = 0
finally:
print("Always runs")
raise ValueError("empty list")A named, reusable operation. Returning hands the value back; printing throws it away.
def total(items):
result = 0
for i in items:
result += i
return result
def greet(name="friend"):
return f"Hello, {name}"Assigning inside a function creates a local variable. The outer one is untouched.
count = 10
def change():
count = 99 # a different variable
change()
# count is still 10An agreed text format for structured data. Parse it before you can use it.
import json
data = json.loads(text) # text -> data
text = json.dumps(data) # data -> textwith open guarantees the file is closed, even if something fails partway.
with open("notes.txt") as f:
contents = f.read()
with open("out.txt", "w") as f:
f.write("done\n")Counting, accumulating, searching, filtering, mapping and finding extremes.
# running maximum
biggest = numbers[0]
for n in numbers:
if n > biggest:
biggest = n
# duplicates in one pass
seen = set()
for x in items:
if x in seen:
return True
seen.add(x)How the work grows as the input grows. Sequence adds, nesting multiplies.
O(1) indexing
O(log n) binary search
O(n) one loop
O(n log n) a good sort
O(n^2) nested loopsThe six that catch the overwhelming majority of real defects.
[] # empty
[x] # one item
[x, x, x] # all identical
# answer first
# answer last
[-3, -9] # negatives
0 # zeroA reference tells you what exists. The course teaches you when to reach for it, and has you build something real while you learn.