Your first program
Every programmer's first program does one thing: it says hello. Yours will too — and you'll run it right here on this page.
Printing
Python has a built-in function called print(). Whatever you put between the
brackets, Python writes to the screen.
print("Hello, world!")
Hello, world!
Press Run and watch the output appear. That's a real Python program — three words long, but real.
Printing more than one thing
Give print() several things separated by commas and it prints them all on one
line, with spaces between:
print("My name is", "Ayesha")
print("I am", 13, "years old")
My name is Ayesha I am 13 years old
Notice the second line mixes text and a number. Python doesn't mind — print()
takes almost anything.
Text needs quotes. Numbers don't.
Text in Python is called a string, and strings always wear quotes. Numbers go bare:
print("13") # a string that happens to look like a number
print(13) # an actual number
print(13 + 2) # numbers can do maths
13 13 15
The first two lines look the same, but they're different things to Python —
"13" + 2 would be an error, because Python won't add a number to text. You'll
meet this difference again in the variables lesson.
Common mistake
Forgetting a quote or a bracket is the classic first error. Run this and read what Python says:
print("Where does this end?
Python points at the line and tells you the string was never closed. Error messages look scary for about a week — then they become your best debugging tool. Read them; they're usually right.
Your turn
Make Python print your own name on one line, and your age on the next.
Prefer learning with a teacher?
WasiLearn Academy runs small-group classes covering this material, online and in Karachi.
See the classes