Python Concepts/Introduction and Setup
Python 3 vs Python 2Edit
There are two major versions of Python, Python 3 (newer) and Python 2 (older). You should consider them as separate languages, because code from Python 2 cannot be used in Python 3, and vise versa. This course teaches Python 3 only. In 2020, Python 2 reached end-of-life and support has stopped completely, so there is no point in learning it. It is important to make sure you are installing and using Python 3 instead. However, Python 2 sometimes comes preinstalled on certain operating systems. Installation instructions below will take this into account and guide you through using the correct version of Python. If you end up having both versions installed on your computer, you will need to use the
Before you can enter into the magical world of Python, you'll need to have Python installed on your computer. It may be possible, depending on your operating system, that Python is already installed on your computer. The instructions below will help you determine whether this is the case. Please select the installation instructions for your operating system, then return to this lesson once you have installed Python:
Downloading and installing Python onto your computer is the recommended way to run your code. However, if you feel concerned about downloading Python onto your computer or you just can't do that, then you can use a website to develop and run your code. An excellent free website to use is https://www.online-python.com/, which will let you follow along with most of this course without installing Python. However, this approach will eventually hinder you from learning the more advanced Python topics later on in the resource. Installation will eventually be required.
The Interactive ShellEdit
The shell, terminal, command prompt, command line, or whatever you like to personally call it, is one of the mediums of communication between you and the Python interpreter. Through out much of this resource, you'll see examples of Python code being used in a shell. Essentially, the shell lets you type out Python code and run it directly, without creating a file first.
Before we go ahead, open interactive python in your operating system's shell. Simply open a new shell (command prompt, terminal, etc.) and type
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
You may not get the first couple of lines exactly as shown above, since everyone has different versions, computers, and builds. As you've probably guessed, the interpreter tells us the Python version, date of build, the compiler used to build it, the computer's architecture, and the operating system. This information is important for debugging and is useful for checking Python's version. The most important thing that you'll need to know are those three greater-than signs (
Try typing the following into your prompt. (Don't type the
>>> print("Hello, world!") Hello, world!
As you can see, a newline without the prompt is output. In this case, the
>>> if True: ... print("Hello, world!") ... Hello, world!
To exit the interactive shell, type:
You will eventually find it tedious to retype programs into your shell. Like other interpreted programming languages, you can store and execute Python code from a file. There are two main types of Python files, script files and bytecode files. Script files are simple plain text files that you can create and edit with just a simple plain text editor. The other type of file, bytecode files, are compiled CPython files. These files are not meant to be edited. Don't worry about this type of file; it will be discussed later on in this course.
A Python script file name ends with the extension
print("Hello world, from a Python script!") input("Type anything then press enter: ")
Now that you have created your script, it's time to run it. In your shell (command prompt, terminal, etc.) change directory to the folder where your
You'll notice that the
IDLE, or Integrated Development and Learning Environment, is the default IDE that comes bundled when you install CPython. IDLE is an alternative way to write and run Python scripts in the same program, rather than using a text editor and a shell separately. IDLE is merely a convenience and not required for this course -- if you prefer editing and running scripts like you did with
On Windows, IDLE can be accessed by right clicking the Python script and then by clicking on Edit with IDLE. On all operating systems, the program can be found in the start menu (or the Launchpad for Mac). When you run IDLE, you will notice Python's interactive shell. You can open or create a
When working with IDLE, you'll notice a couple things. First, you'll notice that your code is colored. By default, strings are green, comment red, built-in functions purple, etc. This allows you to see certain parts of your code. This gives you an advantage over mono-colored text. Secondly, you'll notice that IDLE will make automatic indentations when your text is indented. IDLE will also convert presses of the tab key to four spaces.
Although the default settings are great, not all people will like them. Therefore, IDLE is extremely customizable and can be changed as needed. For example, this gives a color-blind person the ability to change the colored text to something more friendly. You can change IDLE's default settings under
Checkpoint: Before moving on to the next lesson, you should be comfortable creating
- ↑ For advanced users only: There are options if you don't want to do either of these steps, but be careful. When you right-click your mouse on the desktop or in an explorer window, you'll be able to select a new file to create. If you have experience working with Windows registries you can add a Python file into New. I should warn you now, it can be dangerous editing your registries, so do so at your own risk. If you're still intent on doing this, run the program regedit.exe either from Run... or from the command prompt. When the program opens open My Computer, then open HKEY_CLASSES_ROOT. From here, scroll down until you find .py and open it. Then right-click .py, move the mouse over new and then select Key. Name this Key ShellNew. Now right-click ShellNew and add a New String this time. Name it NullFile and leave its value empty. There, you've done it! You'll need to restart your computer.
- ↑ "Should I use Python 2 or Python 3 for my development activity?". Python.org. Python Software Foundation. Retrieved 2022-06-25.