Creating Your First Python Program
This segment will guide you through creating and running your very first Python program. We'll use a simple "Hello, world!" example to get you started.
Writing the Code
-
Open a basic text editor like Notepad (Windows) or TextEdit (macOS). Avoid using word processors like Microsoft Word, as they add formatting that Python can't understand.
-
Type the following line of code into the text editor:
print("Hello, world!")
This single line of code tells Python to display the text "Hello, world!" on your screen.
print()
is a built-in Python function that outputs text.
Saving the File
- Go to File > Save As... in your text editor.
- Choose a location on your computer where you want to save the file.
- In the "File name" field, type a name for your file, such as
hello.py
. Crucially, make sure to add the.py
extension to the end of the filename. This tells your computer that the file contains Python code. - In the "Save as type" dropdown (if available), select "All Files (*.*)". This ensures that the
.py
extension is saved correctly. - Click Save.
Running the Program from the Command Prompt
Now that you've saved your Python file, you can run it from the command prompt (Windows) or terminal (macOS/Linux).
-
Open the Command Prompt:
- Windows: Press the Windows key, type
cmd
, and press Enter. - macOS: Open the "Terminal" application (found in /Applications/Utilities/).
- Linux: Open your distribution's terminal application.
- Windows: Press the Windows key, type
-
Navigate to the Directory: Use the
cd
command to navigate to the directory where you savedhello.py
. For example, if you saved it in your "Documents" folder, you might type:cd Documents
If you saved it directly in your user directory, you might use:
cd ~
(The
~
symbol represents your home directory.) -
Run the Program: Type one of the following commands and press Enter:
python hello.py
or
py hello.py
The
python
orpy
command tells your computer to execute the Python interpreter on thehello.py
file. -
See the Output: If everything is set up correctly, you should see the following output in the command prompt:
Hello, world!
Congratulations! You've successfully created and run your first Python program.
Troubleshooting
- "python" or "py" is not recognized as an internal or external command: This usually means that Python is not added to your system's PATH environment variable. Refer to the previous segment in this tutorial for instructions on how to add Python to your PATH. https://docs.python.org/3/using/windows.html#environment-variables
- SyntaxError: Double-check your code for typos or incorrect syntax. Make sure you've typed
print("Hello, world!")
exactly as shown. - FileNotFoundError: Ensure you are in the correct directory in the command prompt and that the filename
hello.py
is spelled correctly.
Expected Outcome for this step:
User will be able to create a basic Python script, save it with the '.py' extension, and run it successfully from the command line.
No comments on this specific step yet. Be the first!
(Form to add comment for this step coming soon)