Chapter 1 · Getting Started
Your First Program: print, Variables and input
- Page 3 of 23
- 3 min read
Every program does the same three things: it takes some data, works on it, and shows a result. On this page you will learn the smallest pieces that make that possible — print, comments, variables and input.
Printing output
print() shows whatever you put between its brackets. Text goes inside quotes; numbers and calculations do not. Give it several things separated by commas and it prints them with spaces between:
print("Python for AI")
print("Lesson", 1, "of", 20)
print(7 * 6)Python for AI
Lesson 1 of 20
42Comments
Anything after a # on a line is a comment: Python ignores it. Comments are notes for humans — including you, three weeks from now. Explain why the code does something, not what every line obviously does.
Variables: names for values
A variable is a name that refers to a value. You create one with =, which means "store", not "is equal to":
model_name = "gpt-5.5" # text (a string)
max_tokens = 500 # a whole number (an integer)
temperature = 0.7 # a decimal number (a float)
is_streaming = False # True or False (a boolean)
print(model_name, max_tokens, temperature, is_streaming)
max_tokens = max_tokens + 100 # a variable can change
print(max_tokens)gpt-5.5 500 0.7 False
600Notice the last two lines. max_tokens = max_tokens + 100 reads the current value (500), adds 100 and stores the result back under the same name. In maths that line would be nonsense; in programming it is everyday work.
Naming rules and style
- Names can contain letters, digits and underscores, but cannot start with a digit:
model_2is fine,2modelis not. - Names are case-sensitive:
Modelandmodelare different variables. - Python style (called PEP 8) uses
snake_case: lower-case words joined by underscores. - Choose names that say what the value means:
temperature, nott.
Every value has a type
Python keeps track of what kind of value each one is. type() tells you:
print(type("hello"))
print(type(42))
print(type(3.14))
print(type(True))<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>You will meet these four types — str, int, float and bool — properly on the next page.
Asking the user: input()
input() shows a question, waits for the user to type something and press Enter, and gives you what they typed as text:
name = input("What is your name? ")
print("Welcome to Python, " + name + "!")If the user types Rahim:
What is your name? Rahim
Welcome to Python, Rahim!Your first error message
Errors are normal. What matters is reading them. Here a variable name is misspelt:
greeting = "Hello"
print(greting)Traceback (most recent call last):
File "main.py", line 2, in <module>
print(greting)
^^^^^^^
NameError: name 'greting' is not defined. Did you mean: 'greeting'?Read error messages from the bottom up. The last line names the problem (NameError: Python does not know the name greting) and even suggests the fix. The lines above it say where: line 2. Most beginner errors are this kind — a typo, a missing quote or bracket — and are fixed in seconds once you read the message.
Try it yourself
- Write a program that asks for the user's name and city, then prints
Rahim lives in Dhaka. - Store the number of hours you will study each day in a variable, and print how many hours that is in 30 days.
- Make a deliberate typo in a variable name and read the error message.