Python in one hour.
Python is the main language of AI. You don't need to install anything or own a powerful laptop: Google Colab runs Python free in your browser. Today you'll write real code, from zero.
Set up in 2 minutes
- Open colab.research.google.com and sign in with your Google account.
- Click New notebook.
- Type code in a cell and press Shift + Enter to run it.
1. Printing and variables
print("Assalamu alaikum, Hyderabad!")
name = "Ayesha"
age = 19
marks = 87.5
print(name, "is", age, "years old and scored", marks)
A variable is a labelled box that stores a value. Text goes in quotes ("strings"), numbers don't.
2. Lists
cities = ["Hyderabad", "Bengaluru", "Chennai"]
print(cities[0]) # Hyderabad (counting starts at 0!)
cities.append("Pune")
print(len(cities)) # 4
3. Loops
prices = [120, 250, 90, 300]
total = 0
for p in prices:
total = total + p
print("Total bill:", total) # 760
Notice the indentation (spaces) inside the loop. Python uses indentation to know what's inside the loop.
4. Conditions
marks = 72
if marks >= 75:
print("Distinction")
elif marks >= 60:
print("First class")
else:
print("Keep going!")
5. Functions
def gst_price(price, rate=18):
return price + price * rate / 100
print(gst_price(1000)) # 1180.0
print(gst_price(1000, 5)) # 1050.0
A function is a reusable recipe: give it inputs, get an output.
What does this print? fruits = ["mango", "banana", "guava"] then print(fruits[1])
Python counts from 0: index 0 is mango, 1 is banana.
6. A mini project: monthly expense tracker
expenses = {"rent": 8000, "food": 6000, "travel": 1500, "phone": 300}
total = sum(expenses.values())
print("Total:", total)
for item, amount in expenses.items():
percent = amount / total * 100
print(item, "=", round(percent, 1), "%")
That {} is a dictionary: pairs of keys and values, like a menu with item names and prices.
Errors are normal. Read the last line of the error message, it usually tells you what's wrong (a missing quote, wrong spacing, a spelling mistake). You can also paste the error into an AI and ask "explain this error simply".
What will print(len([3, 7, 1, 9])) show?
len() counts the items in the list: 4.
Your task (45 min)
In Google Colab, recreate every example in this lesson by typing it yourself (don't copy-paste). Then change the expense tracker to your own monthly expenses. Save the notebook to your Drive as "My first Python".
Lesson 9 complete!
You can use Google Colab and write Python with variables, lists, loops, conditions and functions. Next: data, the fuel of AI.
Next: Data: the fuel of AI →