Python is the undisputed language of Artificial Intelligence.
Why Python? It is simple to read, incredibly powerful, and has a massive ecosystem of AI tools. To the right, you'll see a fully functional Python environment running directly in your browser. No installation required.
The most basic command in Python is print(). It simply displays whatever you put inside the parentheses on the screen.
Try it out! Type the exact command above into the code editor on the right and hit the Run Code button.
In AI, we deal with massive amounts of data. We store this data in variables. Think of a variable as a labeled box that holds information.
At its core, Machine Learning is just a lot of math happening very quickly. Python makes this easy:
5 + 310 - 24 * 520 / 43 ** 2 (This is 3 squared)Python runs a program one statement at a time, from the top of the file to the bottom. Each line is executed completely before the next one starts. This sounds obvious, but it explains the most common beginner error: using a variable before you have created it. If line 1 prints total and line 2 assigns total = 10, Python raises a NameError on line 1, because at that moment the name does not exist yet.
Lines that begin with # are comments. Python ignores everything after the hash sign, so comments are the place to explain why you did something. Good comments are one of the cheapest habits that make code readable weeks later.
The "labeled box" picture is a useful start, but a more accurate one is a name tag attached to a value. The statement x = 5 means "make the name xrefer to the value 5". Assigning again, x = x + 1, creates the value 6 and moves the tag onto it. Names are case sensitive (Accuracy and accuracy are different names), cannot start with a digit, and cannot contain spaces. By convention Python programmers write names in snake_case, such as model_accuracy.
Every value in Python has a type, and the type decides which operations make sense. You will meet four constantly:
847 or -3.0.95. Model accuracies, losses and probabilities are all floats."cat". Single or double quotes both work.True or False, which you will use heavily in the next lesson.The built-in type() function tells you what you are holding:
print(type(847)) # <class 'int'>
print(type(0.95)) # <class 'float'>
print(type("cat")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(20 / 4)) # <class 'float'> (division always gives a float)Python follows the usual order of operations: exponents first, then multiplication and division, then addition and subtraction, with parentheses overriding everything. Two operators are worth learning early because they appear in data work all the time: // is floor division (it drops the remainder) and % gives the remainder.
print(2 + 3 * 4) # 14, multiplication happens first
print((2 + 3) * 4) # 20, parentheses change the order
print(17 // 5) # 3, how many whole 5s fit in 17
print(17 % 5) # 2, what is left over
print(2 ** 10) # 1024These are not just toy facts. If you split 1000 images into batches of 32, 1000 // 32 is 31 full batches and 1000 % 32 is the 8 images left over for a final, smaller batch.
Printing a raw float such as 84.70000000000001 looks sloppy. An f-string lets you put a value inside text and control how it is displayed: prefix the string with f, place the expression in curly braces, and add a format after a colon.
correct = 847
total = 1000
accuracy = correct / total * 100
print(f"Accuracy: {accuracy:.1f}%") # Accuracy: 84.7%The :.1fmeans "show one digit after the decimal point". This exact pattern will appear throughout the course whenever we report a metric.
"Score: " + 95 raises a TypeError because Python will not guess how to add text to a number. Use an f-string or str(95).SyntaxError usually points at the line after the real mistake, so check the line above.7 / 2 is 3.5, not 3. Use // when you want the whole-number result.Error messages are information, not punishment. Read the last line first: it names the error type and often tells you exactly what went wrong.
print() displays values; comments start with #.=.type() reveals which you have.// and % give quotient and remainder.:.1f make output readable.Imagine an AI model was tested on 1000 images and predicted 847 of them correctly.
correct and set it to 847.total and set it to 1000.print() the result.Hint: Divide correct by total, then multiply by 100.