← Intro to programming with Python
Your first program: print()
Make the computer show words and numbers.
A program is a list of instructions that the computer follows exactly, from the top line to the bottom line. It is like a recipe, except the cook never guesses: if the recipe says "add salt" twice, salt goes in twice. The first instruction most people learn is print(), which shows something on the screen. It matters because it is how your program talks back to you, so you can see what it is doing.
print("Good morning!")
print(7)
print("A bus ticket costs", 3, "dollars")Words go inside quotes, so Python knows they are text to show and not instructions. Numbers need no quotes. Commas let one print() show several things, with a space between each. Every print() starts a new line, so this shows three lines.
Common mistake: forgetting the quotes. print(Hello) makes Python look for something NAMED Hello, and you get a NameError. print("Hello") shows the word Hello.
Another common mistake: a capital letter. Python is case-sensitive, so Print("Hi") does not work. It must be print, all lowercase.
Every opening bracket needs a closing one. print("Hi" without the ) gives a SyntaxError, which is Python saying "I could not read this line".