Chapter 1: Getting Started
|
Source: Python Crash Course, 3rd Edition by Eric Matthes |
In this chapter, you’ll run your first Python program, hello_world.py.
First, you’ll check whether a recent version of Python is installed on
your computer; if it isn’t, you’ll install it. You’ll also install a
text editor to work with your Python programs. Text editors recognize
Python code and highlight sections as you write, making it easy to
understand your code’s structure.
Setting Up Your Programming Environment
Python differs slightly on different operating systems, so you’ll need to keep a few considerations in mind. In the following sections, we’ll make sure Python is set up correctly on your system.
Python Versions
Every programming language evolves as new ideas and technologies emerge, and the developers of Python have continually made the language more versatile and powerful. As of this writing, the latest version is Python 3.11, but everything in this book should run on Python 3.9 or later. In this section, we’ll find out if Python is already installed on your system and whether you need to install a newer version. Appendix A contains additional details about installing the latest version of Python on each major operating system.
Running Snippets of Python Code
You can run Python’s interpreter in a terminal window, allowing you to try bits of Python code without having to save and run an entire program.
Throughout this book, you’ll see code snippets that look like this:
>>> print("Hello Python interpreter!")
Hello Python interpreter!
The >>> prompt β referred to as a Python prompt β indicates you
should be using the terminal window. The bold text is the code you
should type and execute by pressing Enter. Most examples in this
book are small, self-contained programs run from your text editor, but
sometimes basic concepts are shown in terminal snippets to demonstrate
them more efficiently. When you see >>> in a code listing, you’re
looking at a terminal session.
We’ll also use a text editor to create a simple program called Hello World! β a long-held tradition in the programming world that serves a real purpose: if it runs correctly on your system, then any Python program you write should work as well.
About the VS Code Editor
VS Code is a powerful, professional-quality text editor that’s free and beginner-friendly. It works well for both simple and complex projects, and it can be installed on all modern operating systems with support for most programming languages, including Python.
Appendix B provides information on other text editors. If you want to begin programming quickly, use VS Code to start β you can always consider other editors once you’ve gained experience.
|
If you already have a text editor installed and know how to configure it to run Python programs, you are welcome to use it instead. |
Python on Different Operating Systems
Python is a cross-platform programming language, which means it runs on all major operating systems. Any Python program you write should run on any modern computer that has Python installed. However, the setup method varies slightly per OS.
In this section, you’ll check whether a recent version of Python is
installed, install it if not, and then install VS Code. These are the
only two steps that differ per operating system. Afterward, you’ll run
hello_world.py and troubleshoot anything that doesn’t work.
Python on Windows
Windows doesn’t usually come with Python, so you’ll likely need to install it along with VS Code.
Installing Python
First, check whether Python is installed. Open a command window by
entering command into the Start menu and clicking Command Prompt.
In the terminal, enter python in lowercase.
-
If you see a Python prompt (
>>>), Python is installed. -
If you see an error or the Microsoft Store opens, Python is not installed. Close the Microsoft Store if it opens β it’s better to use an official installer.
If Python is not installed, or if you see a version earlier than 3.9, download an installer from python.org. Hover over the Downloads link and click the button for the latest version. After downloading, run the installer and make sure you select Add Python to PATH β this will make it easier to configure your system correctly.
Running Python in a Terminal Session
Open a new command window and enter python. You should see:
C:\> python
Python 3.x.x (main, Jun ..., 13:29:14) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
|
If you don’t see this output or something similar, see the more detailed setup instructions in Appendix A. |
Enter the following in your Python session:
>>> print("Hello Python interpreter!")
Hello Python interpreter!
To close the terminal session, press Ctrl+Z then Enter, or
enter exit().
Installing VS Code
Download an installer from code.visualstudio.com. Click Download for Windows and run the installer. Then skip ahead to Running a Hello World Program.
Python on macOS
Python is not installed by default on the latest versions of macOS, so you’ll need to install it if you haven’t already done so.
|
Python 2 was included on older versions of macOS, but it’s an outdated version you should not use. |
Checking Whether Python 3 Is Installed
Open a terminal via Applications > Utilities > Terminal or press
Cmd+Space, type terminal, and press Enter. Enter python3
to check your version.
-
If you see a message about installing command line developer tools, cancel the pop-up β it’s better to install these after installing Python.
-
If the output shows Python 3.9 or later, skip ahead to Running Python in a Terminal Session.
-
If you see a version earlier than 3.9, follow the next section.
On macOS, use python3 wherever this book shows python.
The python command either points to an outdated system version or
generates an error.
|
Installing the Latest Version of Python
Go to python.org, hover over Downloads, and click the button for the latest version. After the installer runs, a Finder window will appear β double-click Install Certificates.command to allow easier installation of additional libraries you’ll need later.
Running Python in a Terminal Session
Open a new terminal and type python3:
$ python3
Python 3.x.x (v3.11.0:eb0004c271, Jun ..., 10:03:01) [Clang 13.0.0 ...] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Enter the following:
>>> print("Hello Python interpreter!")
Hello Python interpreter!
Close the interpreter with Ctrl+D or exit().
|
On newer macOS systems, you’ll see a percent sign ( |
Installing VS Code
Download the installer from code.visualstudio.com. Click Download, then drag the Visual Studio Code installer from your Downloads folder to your Applications folder and double-click to run it. Then skip ahead to Running a Hello World Program.
Python on Linux
Linux systems are designed for programming, so Python is already installed on most Linux computers. There’s very little to install and only a few settings to change to start programming.
Checking Your Version of Python
Open a terminal (on Ubuntu, press Ctrl+Alt+T) and enter python3:
$ python3
Python 3.10.4 (main, Apr ..., 09:04:19) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Press Ctrl+D or enter exit() to leave the Python prompt.
Wherever this book shows python, use python3 instead.
If your version is earlier than 3.9 or you want the latest available, refer to the instructions in Appendix A.
Running Python in a Terminal Session
Open a terminal, enter python3, and try the following:
>>> print("Hello Python interpreter!")
Hello Python interpreter!
Close the interpreter with Ctrl+D or exit().
Installing VS Code
On Ubuntu Linux, install VS Code from the Ubuntu Software Center:
click the Ubuntu Software icon, search for vscode, select
Visual Studio Code, and click Install. Once installed, search your
system for VS Code and launch it.
Running a Hello World Program
With a recent version of Python and VS Code installed, you’re almost ready to run your first Python program. First, install the Python extension for VS Code.
Installing the Python Extension for VS Code
VS Code supports many programming languages. To get the most out of it as a Python programmer, install the Python extension:
-
Click the Manage icon (gear icon, lower-left corner of VS Code).
-
Click Extensions.
-
Enter
pythonin the search box. -
Select the Python extension supplied by Microsoft.
-
Click Install and follow any prompts for additional tools.
|
On macOS, if a pop-up asks you to install the command line developer tools, click Install. Despite any warning about install time, it typically takes only 10β20 minutes on a reasonable connection. |
Running hello_world.py
Make a folder called python_work on your desktop for your projects.
Use lowercase letters and underscores for spaces in file and folder
names, as Python uses these naming conventions.
Open VS Code and close the Get Started tab if open. Create a new file
with or Ctrl+N (Cmd+N on macOS). Save it
as hello_world.py in your python_work folder. The .py extension
tells VS Code your file is written in Python.
Enter the following line in the editor:
print("Hello Python world!")
To run the program, select or press Ctrl+F5. A terminal should appear at the bottom of the VS Code window showing:
Hello Python world!
If you don’t see this output, check every character on the line you
entered. Did you accidentally capitalize print? Did you forget a
quotation mark or parenthesis? Programming languages expect very
specific syntax.
Troubleshooting
If you can’t get hello_world.py to run, try the following:
-
Read the traceback. When a program contains a significant error, Python displays a traceback β an error report that may identify the problem.
-
Take a break. Step away from your computer and try again with fresh eyes.
-
Check your syntax. Mismatched quotation marks or parentheses are common culprits.
-
Start over. Delete
hello_world.pyand re-create it from scratch. -
Get a second set of eyes. Ask someone else to follow the steps on your computer and watch carefully β they may catch a step you missed.
-
See Appendix A. The detailed installation instructions there may resolve your issue.
-
Ask for help. Find someone who knows Python, or consult the online resources in Appendix C (forums, live chat).
The setup instructions in this chapter are also available at ehmatthes.github.io/pcc_3e, where you can copy and paste code and click links directly.
Never worry that you’re bothering experienced programmers β every programmer has been stuck at some point, and most are happy to help. State clearly what you’re trying to do, what you’ve tried, and the results you’re getting.
Running Python Programs from a Terminal
You’ll run most programs directly in your text editor. However, it’s sometimes useful to run programs from a terminal β for example, to run an existing program without opening it for editing.
Make sure you’ve saved hello_world.py in your python_work folder on
the desktop before trying the steps below.
On Windows
Use cd (change directory) to navigate, and dir to list files:
C:\> cd Desktop\python_work
C:\Desktop\python_work> dir
hello_world.py
C:\Desktop\python_work> python hello_world.py
Hello Python world!
On macOS and Linux
Use cd to navigate and ls to list files:
~$ cd Desktop/python_work/
~/Desktop/python_work$ ls
hello_world.py
~/Desktop/python_work$ python3 hello_world.py
Hello Python world!
Try It Yourself
These exercises are exploratory in nature. Starting in Chapter 2, the challenges will be based on what you’ve learned.
1-1. python.org: Explore the Python home page at python.org to find topics that interest you. As you become familiar with Python, different parts of the site will be more useful to you.
1-2. Hello World Typos: Open hello_world.py. Make a typo somewhere
in the line and run the program again. Can you make a typo that generates
an error? Can you make sense of the error message? Can you make a typo
that doesn’t generate an error, and why?
1-3. Infinite Skills: If you had infinite programming skills, what would you build? Write brief descriptions of three programs you want to create. Keeping an “ideas” notebook is a good habit β refer to it whenever you want to start a new project.
Summary
In this chapter, you learned a bit about Python in general, installed
Python and a text editor on your system, ran snippets of Python code in
a terminal session, and ran your first program, hello_world.py. You
also picked up some troubleshooting basics.
In the next chapter, you’ll learn about the different kinds of data you can work with in Python programs and start using variables.