Your first program
A Python program is a text file ending in .py that Python runs from top to bottom, one line at a time. The print() function shows output, # starts a comment that Python ignores, and you run a file with python hello.py.
print() shows output
print("Hello, world!")
print("Python runs", "line by line")
print()
print("The empty print() above made a blank line")Hello, world! Python runs line by line The empty print() above made a blank line
Give print() several values separated by commas and it puts a space between them. Text in quotes is called a string — you'll learn more about strings soon.
Comments
Anything after # on a line is a comment. Python skips it. Use comments to explain why the code does something, not what every line does.
# Greet the user
print("Welcome!") # comments can also follow codeWelcome!
Running a .py file
- Create a file called
hello.pyin any editor — VS Code and PyCharm are popular and free. - Write
print("Hello, world!")in it and save. - Open a terminal in the same folder and run
python hello.py(orpython3 hello.py).
The interactive shell
Type python on its own to open the interactive shell, shown by >>>. It runs each line as soon as you press Enter — perfect for quick experiments. Type exit() to leave.
>>> 10 * 3
30
>>> print("hi")
hiYour first error
Errors are normal — every programmer sees them all day. Run this and read the last line of the message.
print("Missing a closing bracket"Practice
Print a three-line greeting card: Happy birthday!, then an empty line, then From Python.
Happy birthday! From Python