We are going to start working on the rock paper scissors game shortly, but our work has already begun. The very first thing we need to do is to set up a “development environment”. If you already have done this, and have done some programming with Python, then you can skip this lesson.
It goes without saying that there are many different ways in which you can develop Python programs. We describe one particular method in this lesson. Although it is not required, if you use the same setup as described here, then it will be a bit easier for you to follow the lessons in this book.
2.1. Command Line Interface (CLI)
In this book, we will mainly use the CLI tools for development. The IDEs, or Integrated Development Environment, can be useful, but they are not absolutely necessary to do programming. In fact, it is better for the beginning programmers to start with the CLI tools. The IDEs can obscure some basic processes of software development.
If you are on Linux or Mac, then you should be familiar with terminal programs. For example, on Ubuntu many people use the Terminal program that comes with the standard desktop distribution. On Mac, iTerm2 is one of the more popular console programs.
If you are on Windows, then we strongly recommend that you install WSL, Windows Subsystem for Linux, with a Linux distribution such as Ubuntu 20.04LTS. Then you can use the Windows Terminal app with a Unix shell.
We will primarily use Unix shell for illustrations in this book, when relevant. If you use Command Prompt (CMD) or PowerShell on Windows, then you may need to do some “mental translations”.
2.2. Python Installation
We will need the Python interpreter tools. In some systems, Python may come pre-installed. Let’s try and see if you already have Python. Open a terminal app, and type the following:
$ python3 –version .
Or
$ python-version
If either of these commands returns an output that indicates that you have a python version 3.10 or later, e.g., Python 3.10.0 or Python 3.10.1, etc., then you are good to go. Use that command which generated the desired version output. Otherwise, you will need to install a recent version of Python.
The dollar sign $ is used to represent the shell prompt in this book. You type the commands after the “$” prompt. The shell prompt may look different on your system.
You can download the Python source code from their official download page, Download Python, and you can build it yourself if you feel brave enough. That is, however, not recommended for the beginners.
You can just use one of the platform-dependent pre-built Python distributions. (Although some of them. may not be “official”, most Python developers use one of these distributions.) Try to install the most recent stable version. (3.10.4 is the current stable release as of this writing, and by the time when you read this, newer versions like 3.11 may have been released.)
It is not required to use the most recent version to learn Python. But, if you are just starting, then there is no reason to use an older version. Every time a new version of Python is released, you will have to go through some (small or big) relearning process, sooner or later. By starting with the most current version, you are doing yourself a big favor by not having to go through these relearning processes. Resides, some of the sample code of this book use some features from Python 3.10 or later.
If you are on Windows, here’s the binary distribution: Python Releases for Windows. Note that if you use WSL on Windows, as we recommend, then you may instead want to install an appropriate distribution for Linux on your Windows subsystem.
If you are on Mac, you can find the installers on this page: Python Releases for macOS.
If you use Linux (including Linux on WSL), then you may have to search the Web to find the appropriate installation instruction for your specific Linux distribution. Most Linux distributions come with Python, but the version might be old. As stated, we recommend you install version 3.10 or later.
For example, you can do a Web search with the keyword phrases like these: “how to install the latest version of python on ubuntu”, “install python 3.10 on debian”, etc. Note that you can have multiple installations of Python (e.g., with different versions) on your system.
If you use Ubuntu 20.04 or 22.04LTS, for example, then you can install Python 3.10 using the apt tool.
sudo apt install -y python3
The python3 package currently includes the version 3.10.
Make sure that your installation was successful by trying out a few python commands. For example,
$ python -h
The actual command name can be different on your system. It could be python or python3 or something else. Although Python version 2.x has been deprecated, due to the historical reasons, many (Linux) Python distributions may still use the name python3, or something similar, for the python command.
Does it successfully run? Do you get an output that starts with “usage: python [option]… [-c cmd -m mod |file|-] [argi…” or something similar? If you type python–version (or, python3-version, etc.), then what is the output?
What about this?
$python3 -c ‘print(“hello world!”)’
Do you get an output, hello world!? (You can run a simple Python script, without having to save it in a file, using the -c flag.)
OK, assuming that everything has gone smoothly, let’s move on to the next task.
Some Python distributions come with a GUI Python tool, called IDLE. It is a basic IDE, which lets you save and load Python programs. It also includes a Python interactive shell with the syntax highlighting. If you prefer to use this tool, and if it is not already installed, then again do a Web search to find out how to install it on your platform. On Ubuntu, you can again use apt, for example, sudo apt install idle-python3. Then you can start IDLE with idle-python3.
2.3. Visual Studio Code
Although we are not going to use any (specific) “IDEs” in this book, we will still need a good text editor for programming. Those days are over when we used Notepad to program. There are many good options, including Sublime Text, and emacs and vim if you are a Unix/Linux user. In this book, we will install and use VS Code (aka Visual Studio Code), for illustration.
So, what is the difference between the IDEs and the programmer’s editors? In fact, there is really not much difference. These editors are so good these days that you can do pretty much anything you can do with the full-blown IDEs. The difference is really minimal until you need the full power of the IDES (which can be “never” for most people).
One main difference for the beginning programmers, and for those of us who program in multiple programming languages, is that the IDEs are often specific to one (or a few) particular programming language(s). (They often come with particular language “SDKs” already installed.) When you program in Python, you may use PyCharm, or Spyder, etc. When you program in Go, you may use GoLand or something else. When you program in C#, you may use Visual Studio. What do you use when you program in Java? What about C++? Javascript? Haskell? Rust? ….
It used to be the case that this was required despite this obvious inconvenience. But, those days are over now. (When you do heavy GUI programming, you may still benefit from using the specialized IDEs.)
When you use the modern programmer’s editors (also known as the “lightweight IDEs”), you install an extension(s) specific to each programming language that you use, which may need to connect to the “language server” processes. These language servers provide all necessary services that have been traditionally provided by the specialized IDEs.
Now, let’s install VS Code, that is, if you don’t have it already installed on your computer. Here’s the download page: Download Visual Studio Code. Follow the instructions specific to your platform.
If you use WSI. on Windows, then the single installation of VS Code (on Windows) can be used both on your Windows host machine and on your Ubuntu subsystem, for example. Here’s an instruction: Developing in WSL.. No need to be intimidated. There are only three steps, the first two of which you have already done at this point.
Even for the third step, what you really need is the Remote – WSL extension. VSCode has an interesting mechanism through which you can install multiple extensions as if they are a single extension. It is called an “extension pack”. If you would like, you can install the Remote Development extension pack.
Windows users get special treatments here, but if you are serious about learning
programming, even as a hobby programmer, then it is important for you to learn the CLI basics on Unix-like systems. If you don’t learn anything about Python, or anything else, from this book, it will be still worthwhile your time if you start using WSL/Linux subsystem for programming on Windows. As a matter of fact, the “Windows Subsystem for Linux” is the best thing that ever happened to the programmers using Windows.
Install WSL
Note that this is a long term investment. Eventually, over time, you will learn more Unix shell commands and shell scripting, and you will become a better developer (regardless of what programming languages you use).
BTW, here’s the link to the Windows Terminal app on Windows Store: Windows Terminal by Microsoft. You can run CMD, PowerShell, or even Linux Shell such as BASH on Windows Terminal.
2.4. Python Extension
Now that we have VSCode on our machine, let’s open it. Open your terminal program (Terminal, iTerm2, or Windows Terminal, etc.), and cd to any folder. For example,
$ mkdir hello_1
$ cd hello 1
Then try this, on your shell:
$code
If VSCode is correctly installed, and if the command code is in your “system path” (which may have required some additional steps depending on your platform), then it will open an instance of VSCode. You can use an icon on your GUI desktop to open VSCode, but we will always use the command line in this book for illustration. The dot. represents the “current working directory” in Unix-like systems.
They call the currently open folder the “workspace” in VSCode. In fact, you can include multiple folders (from different locations) in one workspace. You can also open, and use, multiple instances of VSCode at the same time as long as they are associated with different workspaces.
Now install the Python extension (by Microsoft). You can do this from the “Extensions” menu, e.g., on the left hand side, inside VSCode. The search box at the top (of the left hand side panel) can be useful when you are searching for particular extensions.
If you are done, then close the VSCode. You can delete the empty directory as follows (from its parent directory), if you want:
$ rndir hello_1
For more information on using Python with VSCode, refer to this page: Python in Visual Studio Code.
2.5. Test Program
Here’s a small Python program.
Listing 2. dev setup/hello.py
1def hello(name: str) -> None:
print(“hello” + name) 2
3
4
Shello(“joe”)
Create a folder anywhere on your file system. This is a temporary folder that you can delete later. You can use the previously created directory, hello_1, if you haven’t deleted it, or you can create a new one. Let’s use the name “hello world” this time. (The code sample is located in the folder dev setup in the author’s computer, as indicated by the label. The directory names are not significant in this example.)
$ mkdir hello world && cd $
Il’ you use BASH, then $ refers to the previously used “command line argument”, hello_world in this case. (Note the underscore. The dollar prefix $ is used to refer to the “variables” in shell, and in the shell scripts.) Otherwise, you can just do co hello world. The second command after && is executed only if the first command succeeds.
If you use CMD or PowerShell on Windows, then you may need to use md and del to create and delete a folder, respectively, instead of mkdir and rmdir. You can also use Windows File Explorer if you prefer to use the graphical user interface.
Next, open VSCode in that directory:
$code
Create a new file in that folder, and name it “hello.py”. (You can use the “File | New File” menu to create a new file, or use the keyboard shortcut, Control-N or Command+N, depending on your platform.) Type in the code above in the file and save it
The line numbers, displayed on the left-hand side of the code box, are not part of the program. Do not include them in your program source file.
Then open a builtin terminal in VSCode. You can again use the menu, “View Terminal”, or use the keyboard shortcut (as shown on the menu, either Control-(back tick) or Command+(back tick)). This is one of the few VSCode keyboard shortcuts you may want to learn by heart (because you will likely use it frequently since it is very convenient to do development using the builtin terminals).
Note that you can even open and use multiple terminals at the same time. We will leave it to the readers to figure out how to do this.
Note that we do not include any screenshots in this book. Screenshots can be easily outdated, and they can be misleading in some cases. But, more importantly, it is a good training to try and figure things out for yourself. Remember, learning to program is 10 times, or 100 times, harder than learning to use a new software (like VSCode).
In the terminal, type the following, and press Enter. (The “Press Enter” part is always implied.).
$ python hello.py
The argument hello.py following the python command is the name of the file that we just used to save the sample code. (Here and throughout this book, replace the command python with whatever you use on your system to start the Python interpreter.)
Do you see an output like this?
hello joe
If so, then CONGRATULATIONS! You are ready to go!
If not, don’t despair. As with any real world problems, when you program, there are a million things that can go wrong (without any exaggerations).
The thing is, though, this book cannot help each reader with every possible problem. You will have to find a solution. In many cases, the particular error messages you are getting will help you troubleshoot the issue.
In this particular case, there can be many reasons. Are you in the correct directory? Is the file saved? In the correct directory? Did you type the code exactly as shown above? Including all the white spaces? Without the line numbers? Are you running the python command in the correct location? And so forth.
Whenever you run into an issue, however, just remember that it is one more chance that you can learn something new. (And, you will end up making the same mistakes over time. Again and again. Don’t be discouraged. Nobody learns these things in their first try.)
2.6. How to Run Python Programs?
The python command generally follows the same pattern/convention that is common with many commands available on Unix. There are a number of different ways to run a Python program (or, script).
First, we can specify the Python script file following the python command, as a “command line argument”, as we just did with the hello.py file.
$python hello.py
hello joe
Second, we can run a Python “module” with the -m “command line flag”.
$ python – hello
hello joe
Note that we do not include the file name extension “.py” in the argument following them flag. In Python, a Python file is both a script and a module, and the file name, without the file extension, is the name of the module, by default. We will discuss this further later in the book.
We also have seen another way to run a Python command in the first lesson, namely, using the c command line flag.
$python -c print(“hello”); \
print(“joe”)”
hello joe
The backslash (1) plus newline combination is used in Unix shell to input a single line command in multiple lines. (The terminology is pretty confusing, but unfortunately that’s the way it is.) That is, the above command is the same as this:
$ python -e ‘print(“hella”); print(“jce”)’
hello
joe
Note that the strings “hello” and “joe” are printed out in two separate lines. This is because each print function adds a newline to its argument.
Running a Python script (text) using the flag is not very practical for all but some short scripts or simple commands. In the next lesson, we will see a few more different ways of running a Python program.
If you use IDEs, including some programmer’s editors like VSCode, then you can easily run, and “debug”, your Python programs in the IDEs. As stated, however, we will exclusively use the CLI commands for illustration in this book.
2.7. Code Review
Before we continue, let’s take a look at the program that we just wrote. What do you see?
There are three lines of code (excluding the empty lines), and the second line is “indented” relative to the other two lines. This means that the second line “belongs to the “less-indented” line above (that ends with a colon:), the first line in this case.
def hello(name: str) > None:
print(“hello name)
A Python program is essentially a series of “statements”. Lines 1-2 form one statement (because line 2 is part of the larger structure that includes both lines 1 and 2). This is called the “compound statement in Python (because it comprises one or more other statements). Line 5 is another statement. A “simple statement”.
hello(“joe”)
The first statement “defines” a function (using a Python “keyword” def), which is named hello. In this case, the hello function, when it is “called”, prints out a text, which is the concatenation of the string “hello” and the function argument (named name). The plus “operator” + is used to concatenate two strings. (Note that we are assuming the name name is also a string here.)
The second statement, line 5, then calls this newly defined function with an argument “joe”. (Again, using a pair of parentheses.) When we run this program, we get the output, hello joe (without the double quotes). Note that the function parameter name (line 1) is replaced by the supplied argument “joe” when we “call the function” like this (line 5).
In fact, we could have instead called it as follows:
hello(name=”joe”)
They are more or less equivalent to each other. You will get the same output from this alternative way of calling a function using the “keyword argument”. This latter “syntax” is more explicit, but it is more verbose. You will have a lot of choices when you program.
You do not have to understand all this now. We will go through this later in more detail, while working on the rock paper scissors game.
2.8. Type Annotations
One thing to note, before we close this chapter, is that we use the “type annotations” in this book. For example, the word str used in line 1 of the sample code indicates that the function takes a string argument (: str). And, it “returns none” (→ None).
At the risk of oversimplification, it is generally easier to write a code in the dynamically typed programming languages like Python, compared to the statically typed languages like C/C++, Java, Go, or C#. On the flip side, you will generally end up with more bugs when you program in the dynamically typed languages. This is because the compilers of the statically typed languages do a lot more to find certain classes of bugs. In languages like Python, you may run into certain types of bugs only at run time, e.g, in production, which is generally not the best time to find bugs. (Software testing has limitations as well.)
Python has a feature called the “type hint” or “typing” (which is a standard library “module”). It is optional, but by using typing, you can get (some of) the benefits of using the statically typed programming languages. Clearly, this is a tradeoff. There are pros and cons. In particular, typing adds some overhead when you develop a software. And, you will have to use additional tools like “MyPy” to get the full benefits of typing, which is well beyond the scope of this book.
It should be noted that many of the experienced Python programmers still do not use typing even though it was first introduced to Python a few years ago. This is partly because it is hard to change the habit. If you have been programming in Python in a certain way for years, then it is rather hard to adopt a new way of programming (even if you intellectually understand the benefits). As the saying goes, you can’t teach an old dog new tricks,
That is one of the reasons why we decided to introduce typing in this beginner’s book. We are not going to do anything with typing. We will just use a small subset of the feature called the “type annotations”. It is optional. If you think that this is just too much complication for a beginner like you, then you can simply ignore it. The hello function definition above, for example, could have been written as follows without the type annotations. (It is a lot easier to “remove” than “add”.)
def hello(name):
print(“hello name)
It is your choice. As a matter of fact, that is precisely why we introduce the type annotations in this book: To give a choice to the beginning Python programmers. If you don’t know if something exists, then you don’t have a choice. You follow the same old “traditional way of programming in Python without even knowing that there is an alternative. Possibly a better alternative.
The readers of this book have a choice. After finishing this book, if you decide to use typing more in your future Python projects, good for you. If you decide otherwise, then that’s fine too. As stated, a vast majority of Python programmers still have not adopted the typing.
Just to be clear, the Python interpreter (mostly) does not know or care about typing. (The type “annotations” have to follow certain Python syntax, however.) When we use the type annotations in the sample code of this book, those are just for us, programmers. But, even at this level of (minimal) use, we believe that the benefits are enormous. By forcing you to think about the types in the early stage, it will make you a better programmer in the long run.
2.9. Summary
We installed the Python CLI tool, VS Code, and the necessary extensions. We tested our setup using a simple Python program. And, we reviewed our first program together.
Although this book can be used as a reading material, you will get the most out of this book by doing all, or most of, the tasks (and, exercises). If you intend to do so, then the dev environment setup is a crucial step. Make sure that you have a working environment before you continue. (If you cannot install Python 3.10, for instance, an older version should be good enough to follow most of the lessons in this book. It is never all or none.)
One thing to note is that this is not a competition. You are in no rush. Take your time, and enjoy what you are doing. Also, remember, troubleshooting is part of “life”.
Leave a Reply
You must be logged in to post a comment.