The Pursuit of Programming #1: What, Why, and How to Start
A Step-by-Step Tutorial on Setting up Python and Writing Your First "Hello, World" Program
As promised when starting this newsletter, I will begin at the very beginning. While learning to program is fun and leads to endless creativity, teaching it in a way that engages students is no easy feat. In the Pursuit of Programming series, my aim is to explain programming concepts creatively so that they can be understood intuitively.
In this post, we will discuss what programming is, how to install Python, and set up our development environment. We will wrap up by writing our first program. If this sounds like fun, let's get started!
What is Programming
Computers are relatively simple machines that cannot do anything on their own. To perform any task, we must provide them with detailed instructions in their own language. This language allows us to communicate with computers, and writing instructions in this language is called programming.
To better understand this concept, imagine you have a remote control car that understands only four basic commands: move forward, move backward, turn left, and turn right. To navigate the car from point A to point B, you must provide a sequence of these commands, such as:
1. Move forward for 15 meters.
2. Turn left 90 degrees.
3. Move forward for 20 meters.
4. Turn right 90 degrees.
5. Move forward 5 meters.
Programming a computer is similar to providing these instructions to the remote control car. However, unlike the car, which is designed for a specific purpose, computers are general-purpose devices, so their instruction language is more versatile. Nevertheless, the fundamental concept of providing step-by-step instructions remains the same.
Getting Setup - Installing Python
We will use Python in this series of posts to learn programming. Python is an excellent choice for beginners due to its user-friendly syntax, which reads like English and does not hinder understanding programming concepts. Python also features an interactive programming environment that makes experimentation a breeze. In this section, we will cover how to set up Python on various operating systems.
Installing Python on Windows
To install Python on Windows, follow these steps:
Visit the official Python download page to download the Python installer: https://www.python.org/downloads/windows/. As of writing this post, the latest Python version is 3.11.3. Note that if you have Windows 7, you need to download Python 3.8.16 or earlier because later Python versions do not support Windows 7. If you are unsure which of the several Windows installers to choose, base your decision on your CPU architecture. Most household computers use an AMD or Intel CPU, and 64-bit is the most common architecture these days, so select "Windows Installer (64-bit)".
After downloading the installer, double-click it to start the installation process.
When the installer starts, select the "Add python.exe to PATH" option so you can run Python from the command line.
If this is your first time installing Python, click "Install Now" and continue with the installation. The installer will apply default settings suitable for most users, including IDLE (an interactive Python code editor), pip (a package manager for installing additional Python packages), and shortcuts for starting Python.
Once the installer completes, verify the installation by going to
Start
, typing "cmd
" in the search bar, and pressing Enter. This will open a command prompt. Type the following command in the command prompt and press Enter:python --version
You should output similar to this:
Python 3.11.3
This means Python is installed on your system. If you see an error, such as "command not found," you likely did not select the option to add Python to the PATH environment variable during installation, as recommended in step 3. In this case, uninstall Python, reinstall it, and ensure you select that option this time.
Installing Python on MacOS
Mac comes preinstalled with Python. To check the version of Python, open a terminal window (click the spotlight icon and type "terminal" in the dialog that appears). In the terminal window, type the following command and press Enter:
python3 --version
If the output says "command not found," you need to download and install Python. To do this, go to the URL https://www.python.org/downloads/macos/ and select the latest version for Mac. Once the installer is downloaded, double-click to start it and follow the instructions. After it finishes, verify the installation by rerunning the ‘python3 --version
’ command in the terminal.
Installing Python on Linux
Most popular Linux distributions, such as Ubuntu and Fedora, come preinstalled with Python. As we primarily focus on learning Python syntax basics, the installed Python version is not crucial as long as it is a Python 3 version. Verify the Python version by running the following command in a terminal window:
python --version
If the output says something similar to "Python 3.x.x
," you're good to go.
Playing With Python 🐍
Now that Python is installed, let's explore it a bit. We'll do this in the Python shell—an interactive command-line interpreter for Python that allows us to write small amounts of code and see the output immediately.
Starting Python Shell on Windows
Click on Start
and enter "cmd
." This will open the Windows command prompt. In the Windows command prompt, type "python
" and press Enter. This will start the Python shell within the same command prompt window. You should now see the Python shell prompt (>>>
).
Starting Python Shell on MacOS and Linux
Open a terminal window, type "python
" (Mac users need to type "python3
"), and press Enter. You should see the Python shell prompt (>>>
).
Writing Our First Python Code
Let’s write our first line of Python code in the Python shell.
>>> print("hello, world")
hello, world
You don't need to type the ">>>
" part; that's the Python shell prompt. This prompt indicates that the Python shell is ready for our input, meaning we can type our code.
print(“hello, world”)
is our first line of Python code. The print
function tells Python to display a message on the screen. In this case, we are printing the message "hello, world." (We enclose the message in double quotes so Python knows where the message begins and ends. We will discuss strings in more detail in a later post.)
After typing this code and pressing Enter, the message appears on the screen.
Next, we can perform some basic arithmetic in the Python shell, such as:
>>> 2 + 3
5
>>> 5 * 2
10
>>> 10 / 5
2.0
>>> 5 - 1
4
>>>
These lines are valid Python code. Python understands basic arithmetic operators, which can be used with numbers and variables. We will discuss variables in an upcoming post in this series and demonstrate how to use these operators with them.
Installing Visual Studio Code (VSCode)
Now that we've had a taste of Python, it's time to complete our development environment setup. We have installed Python, but we also need a place to write code. While the Python shell is excellent for testing small bits of code, we need a proper code editor for working on projects. Visual Studio Code (VSCode) is a popular choice nowadays, as it supports not only Python but also many other programming languages.
To install VSCode, download the installer from the official website: https://code.visualstudio.com/. After downloading the installer for your OS, open it and follow the instructions to install it. Once installed, you can launch VSCode from your OS's application launcher (for Windows, go to the Start menu and type "visual studio code" in the search bar).
One more step remains before we're all set: we must install the Python extension for Visual Studio Code to enable Python code execution. To do this, click the gear icon in the bottom left corner of the VS Code window or use shortcut Ctrl + Shift + X
.
This action opens the VSCode Extensions Marketplace. In the text box on the left side of the screen, type "python." This search will display all Python-related extensions. Select the official Python extension provided by Microsoft, which typically appears as the first option (see screenshot below).
Click "Install," and the extension should install within a couple of minutes.
Writing and Running Our First Python Program
Now, let's create a folder for our Python code and write our first program in that folder. First, create a folder on your Desktop, as it is easy to locate. Name the folder something like "python_tutorial."
In VSCode, create a new file by either going to File > New File or using the shortcut Ctrl + N
. In the file, type the following line:
print("hello, world")
Press Ctrl + S
to save the file. In the save dialog box, navigate to the "python_tutorial" folder on your Desktop and save the file there as "helloworld.py."
To run the program in VSCode, go to the Run menu and click "Run Without Debugging." This action will launch the terminal within VSCode and display the program's output.
Congratulations! You've just completed your very first Python project, which prints a message on the screen. Feel free to experiment by changing the message. As we saw in the previous section, Python understands arithmetic computations, so you can also print the results of such calculations. For example:
print(2 + 6)
We don't need quotes here because they're usually unnecessary when printing numbers. For a fun experiment, try adding quotes around the code above and observe the result. Share your answer in the replies or comments, explaining what happened and why the output changed.
Conclusion
We have reached the end of this post. We've accomplished a lot here: learning what programming a computer entails, discovering Python, installing it, and setting up its development environment using Visual Studio Code. We also wrote and ran our first program. I encourage you to play around and explore other things you can do.
In upcoming posts, we will delve deeper into programming concepts such as variables, data types, loops, and more. Don't worry if these terms are unfamiliar to you.
If you encounter any errors while following the steps in this post or running the programs, feel free to reply or comment here. I will try to help you. If you discover something fun while experimenting with Python, please share it here so we can all learn together.
Until the next post, have fun on your Python adventures🐍