Your First Python Code
Write real Python code today. Variables, strings, numbers, and print -- the building blocks of everything you'll create with AI.
Time to write code. Open Google Colab (colab.research.google.com) and create a new notebook. Follow along by typing each example into a cell and pressing Shift+Enter to run it.
Don't just read this lesson. Type every example. That's how you learn.
Print: Your First Command
The print() function displays text on the screen. It's the most basic thing you can do in Python, and you'll use it constantly.
print("Welcome to Community Incubator!")
When you run this, you'll see: Welcome to Community Incubator!
You can print multiple things:
print("AI Foundations Course")
print("Module 6: Python + AI")
print("Let's go!")
Each print() shows up on a new line. Notice that the text inside the parentheses is wrapped in quotation marks. That's how Python knows it's text (called a "string") and not a command.
Variables: Giving Names to Things
A variable is like a labeled box. You put something in it, give it a name, and use that name later.
name = "Maria"
city = "San Antonio"
print("Hello, " + name + "! Welcome from " + city + ".")
Output: Hello, Maria! Welcome from San Antonio.
You can change what's in a variable at any time:
name = "Maria"
print(name)
name = "Carlos"
print(name)
This prints Maria first, then Carlos. The variable name just holds whatever you last put in it.
Naming rules for variables:
- Use lowercase letters and underscores:
my_name,user_age,home_city - Don't start with a number:
1namewon't work, butname1will - No spaces:
my namewon't work, usemy_name - Make names descriptive:
xis bad,user_ageis good
Numbers: Math in Python
Python handles numbers naturally. No quotation marks needed for numbers.
age = 28
hourly_wage = 19.50
hours_per_week = 40
weekly_pay = hourly_wage * hours_per_week
print("Weekly pay: $" + str(weekly_pay))
Output: Weekly pay: $780.0
Notice we used str() to convert the number to text before combining it with the dollar sign. Python won't let you mix text and numbers directly with the + sign.
Here's an easier way to mix text and numbers using f-strings (the f before the quotes):
age = 28
hourly_wage = 19.50
hours_per_week = 40
weekly_pay = hourly_wage * hours_per_week
print(f"Age: {age}")
print(f"Weekly pay: ${weekly_pay}")
print(f"Annual estimate: ${weekly_pay * 52}")
The f in front of the string lets you put variables inside curly braces {}. Python fills them in automatically. This is the method you'll use most often.
Basic Math Operators
# Addition
print(10 + 5) # 15
# Subtraction
print(10 - 5) # 5
# Multiplication
print(10 * 5) # 50
# Division
print(10 / 3) # 3.3333...
# Lines starting with # are comments
# Python ignores them -- they're notes for humans
Strings: Working with Text
Strings are text. You can do a lot with them:
greeting = "Hello"
name = "San Antonio"
# Combine strings
full_greeting = greeting + ", " + name + "!"
print(full_greeting) # Hello, San Antonio!
# String length
print(len(full_greeting)) # 21
# Uppercase and lowercase
print(greeting.upper()) # HELLO
print(greeting.lower()) # hello
Getting Input from Users
You can ask users to type something:
name = input("What's your name? ")
print(f"Nice to meet you, {name}!")
When you run this in Colab, a text box appears. Type your name and press Enter.
Let's make a mini program:
name = input("What's your name? ")
neighborhood = input("What neighborhood are you from? ")
goal = input("What do you want to learn about AI? ")
print(f"\n--- Your AI Learning Profile ---")
print(f"Name: {name}")
print(f"Neighborhood: {neighborhood}")
print(f"Goal: {goal}")
print(f"Status: Ready to learn!")
The \n creates a blank line. This little program collects information and displays it back -- the same basic concept behind every web form you've ever filled out.
Putting It Together
Here's a practical example -- a simple tip calculator:
meal_cost = float(input("How much was your meal? $"))
tip_percent = float(input("Tip percentage (e.g., 20 for 20%): "))
tip_amount = meal_cost * (tip_percent / 100)
total = meal_cost + tip_amount
print(f"\nMeal: ${meal_cost:.2f}")
print(f"Tip ({tip_percent}%): ${tip_amount:.2f}")
print(f"Total: ${total:.2f}")
The :.2f means "show exactly 2 decimal places" -- perfect for dollar amounts. And float() converts the typed input into a number that can have decimals.
You just wrote a real, useful program. It takes input, does math, and shows results. That's programming.
Try This
Build a "weekly budget calculator" in Google Colab. Ask the user for their weekly income and three expenses (rent, food, transportation). Calculate how much they have left over. Display everything with nice formatting using f-strings.
Talk About It
Was writing code easier or harder than you expected? What surprised you? If you got stuck, what did you do to figure it out?