Lesson 1 of 7, about 20 minutes
Set up and write your first program
Welcome to programming. By the end of this lesson you'll have written and run real Python code.
Why Python?
- It reads almost like English, so it's easy to learn.
- It's the standard for AI and data. Tools like pandas, scikit-learn, TensorFlow, PyTorch and the AI provider SDKs are all Python-first.
- It's in demand. Data analyst, ML engineer and backend developer jobs often list Python.
Google Colab: Python in your browser
Instead of installing Python, we'll use Google Colab. It's free, runs on Google's computers, and only needs a Google account and a browser.
- Go to
colab.research.google.comand sign in with your Gmail account. - Click New notebook.
- You'll see a box called a code cell. Type code there.
- Press Shift + Enter (or the play button) to run it. The output appears below.
A notebook mixes code cells and text cells. It's saved to your Google Drive automatically.
Your first program
print("Habari! I am learning Python for AI.")
Run it. print() is a function: a named command that does something. Here it displays text. Text in quotes is called a string.
Python as a calculator
print(1500 + 2000) # addition
print(45000 - 15000) # subtraction
print(250 * 12) # multiplication
print(10000 / 4) # division gives 2500.0
print(10 // 3) # whole-number division gives 3
print(10 % 3) # remainder gives 1
print(2 ** 10) # power gives 1024
Anything after # is a comment. Python ignores it. Comments are notes for humans.
Errors are normal
Try running this broken code:
print("Hello)
You'll get a SyntaxError because the closing quote is missing. Every programmer sees errors all day. Read the last line of the error message: it usually tells you what's wrong and on which line. Fix it and run again.
When you're stuck, paste your code and the full error into an AI assistant and ask: "Explain this error simply and how to fix it." That's how professionals work now. Just make sure you understand the fix.
Getting input
name = input("What is your name? ")
print("Karibu, " + name + "!")
Run it, type your name in the box, and press Enter.
Try it. Write a program that prints how many seconds there are in a week, then how many hours there are in 365 days. Use comments to explain each line.
Key takeaways
- Google Colab runs Python free in your browser.
print()shows output.#starts a comment.- Errors are normal. Read the last line, fix, and rerun.
Enjoyed this? Create a free account, then unlock all 7 lessons, the exam and the certificate for US$59.
Unlock the full course