Running your first Python program
A file that holds the single line print("Hello, world!") is a complete Python program, and running it prints 13 characters.
Type one line, run it, then look at what happened
Before any theory, run something. Install Python 3 from python.org (it is free) and open IDLE, the small editor that comes with it. Choose File, then New File, and type the line below exactly as it is shown, quotes and brackets included. Save it as hello.py, then choose Run, then Run Module, or press F5. If you would rather use a terminal, the command is python3 hello.py on macOS and Linux, or py hello.py on Windows. An online Python editor works too, as long as it runs Python 3.
print("Hello, world!")Hello, world!That is a whole program. print is a built-in function: a ready-made command that Python already knows. The round brackets after it hold the thing you want shown. The double quotes tell Python that Hello, world! is text, not a command, and Python calls a piece of text a string. Notice that the quotes themselves do not appear in the output. They are only there to mark where the text starts and stops. Single quotes work the same way, so print('Hello, world!') gives the identical result.
Python runs the file from the top line down
A program with three lines runs three times as many instructions, one after another. Python reads line 1, carries it out, moves to line 2, and so on until it runs out of lines. Swap two lines and the output swaps too. This top-to-bottom order is the first rule of reading any program, and later units add only a few ways to bend it: an if can skip lines, a loop can repeat them, and a function can jump to lines written elsewhere.
print("Ready")
print("Set")
print("Go")Ready
Set
GoOne print can show several things. Put commas between them and Python prints them on one line with a single space between each. An empty print() prints an empty line, which is handy for spacing out your output.
print("Seats left:", 3)
print()
print("Bus", "number", 7016)Seats left: 3
Bus number 7016Lines that start with # are notes for people
Anything after a # on a line is a comment. Python skips it completely. Comments are for the next person who reads your code, and that person is often you, a week later. A comment can sit on a line of its own or after a statement. You can also put # in front of a line of code to switch it off for a moment without deleting it.
# A greeting for the morning
print("Good morning") # shown on screen
# print("This line is switched off")Good morningThe >>> prompt answers straight away
When IDLE first opens, you see a window with >>> in it. That is the interactive shell. Type 2 + 3 there, press Enter, and 5 appears at once, without any print. This makes the shell a good place to test one small idea. A saved file behaves differently. A line that only says 2 + 3 in a file is worked out and then thrown away, and nothing is shown. In a file, you have to ask for output with print.
>>> 2 + 3
5
>>> print("hi")
hi| Built-in function that shows values on the screen | |
| string | Text between quotes, such as "Hello" |
| comment | Text after # that Python ignores |
| >>> | The prompt of the interactive shell, which answers each line at once |
| .py file | A saved Python program, run from top to bottom |
| IDLE | The simple editor installed together with Python |
- What does this print?
print("Good morning") - What does this print?
print("B") print("A") print("C")A
B
CB
A
CBACC
A
B
- What does this print?
print("Seats left:", 3) - What does this print?
print("3 + 4") - What does this print?
print(3 + 4) - What does this print?
# print("one") print("two") # print("three")one
two
threetwoone
three(nothing)
- Put the lines in order so the program prints this countdown.Output
3 2 1 Go! - A file contains only the line
10 * 6. What appears when you run the file?- 60
- 10 * 6
- Nothing
- An error
- What does this print?
print("Hi", "there", "Mina") - What is the name of the built-in function that shows a value on the screen?
- "Print with a capital P is fine; Python will know what I mean." Python is case-sensitive. Print("hi") stops with NameError: name 'Print' is not defined. Did you mean: 'print'? The message even suggests the fix.
- "The quotes are optional: print(Hello) prints Hello." Without quotes, Hello is read as the name of a variable, and there is none, so Python reports NameError: name 'Hello' is not defined. Text always needs quotes.
- "I copied the line from a document, so it must be right." Word processors turn " into curly quotes like “ and ”. Python rejects them with SyntaxError: invalid character '“' (U+201C). Type the quotes yourself in the code editor.
- print("...") shows text; the quotes are not printed.
- A program runs from the top line down.
- Commas in print put one space between values.
- # starts a comment that Python skips.
- The >>> shell shows results by itself; a file needs print.
Sources for this course (6)
- Python Software Foundation, The Python Tutorial for Python 3.12 (docs.python.org/3.12/tutorial: 2 Using the Python Interpreter, 3 An Informal Introduction to Python (numbers, text, lists), 4 More Control Flow Tools (if, for, range, break and continue, defining functions, default argument values, 4.10 coding style: 4-space indentation), 5 Data Structures (list methods, 5.5 dictionaries, insertion order), 8 Errors and Exceptions (syntax errors, exceptions, reading the traceback, handling exceptions), 15 Floating-Point Arithmetic (0.1 + 0.2))
- Built-in Functions (docs.python.org/3.12/library/functions.html: print, input, len, int, float, str, type, round (round half to even: round(0.5) and round(-0.5) are 0, round(1.5) is 2), range, sum, min, max, sorted)
- Built-in Types (str methods upper, lower, replace, strip; dict.get; list.append, pop, remove, sort returning None)
- General Python FAQ (docs.python.org/3.12/faq/general.html: Guido van Rossum began Python in the 1989 Christmas holidays, posted it to USENET in February 1991, named it after Monty Python's Flying Circus)
- Curriculum for the Korean pages: 교육부 고시 제2022-33호 [별책 10] 정보과 교육과정 (PDF read 2026-10-19: 중학교 정보 [9정03-05] 리스트 등 데이터를 순차적으로 저장하는 구조, [9정03-06] 논리 연산과 중첩 제어 구조, [9정03-07] 함수 활용과 디버거로 오류 수정, [9정03-08] 실생활 문제를 프로그래밍으로 해결, 적용 시 고려 사항 「학생의 수준을 고려하여 적합한 프로그래밍 언어를 선정」; 고등학교 정보 [12정03-04] 자료형, [12정03-05] 표준입출력, [12정03-07] 제어 구조)
- Bug story only: Smithsonian National Museum of American History, Log Book With Computer Bug, 1947, Harvard Mark II, "first actual case of bug being found", probably not Hopper's log book (americanhistory.si.edu/collections/object/nmah_334663, read 2026-10-19)
Checked: 2026-10-19