Python Concepts/Introduction and Setup

Objectives

 * Install Python on your computer.
 * Learn how to run Python from the command line.
 * Learn how to make and run a Python script.
 * (Optional): Learn how to use IDLE.

Python 3 vs Python 2
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  command anywhere the course tells you to use the   command (as   would run Python 2 in that case).

Installing Python
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:


 * Windows Installation
 * MacOS Installation
 * Linux Installation

Online Interpreter
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 Shell
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  into it. (Remember, if you're using a machine that had Python 2 preinstalled on it, you'll need to specifically type  instead.) Congratulations, you now have a working Python interpreter. This is what the shell may look like when you run interactive Python on your computer:

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. They symbolize a prompt that asks you for some input. Since this prompt is shown in examples, you'll need to remember that the prompt isn't some special keyword or thing you'd need to type on a daily bias. Here's an example with the prompt displayed only.

Try typing the following into your prompt. (Don't type the . That part is already there.)

As you can see, a newline without the prompt is output. In this case, the  function printed the text to the screen. (You just wrote your first line of Python code!) Another important thing to know is that the prompt changes to three periods if you enter a command with indentations (the Python interpreter is expecting more input). Overall, you'll need to be careful not to mistake the prompt for actual Python code. You will learn when to use indentations later in this course, but for now, you don't need to worry about them.

To exit the interactive shell, type:

Python Script
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  and will be executable when you execute the script by itself on most operating systems. Create a file called  and write the following into it:

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  file is. (If you do not know how to use the cd command to do that, refer to this article.) Then, once your shell is in the folder with the script, type in. (Again, if you're using a machine that had Python 2 preinstalled on it, you'll need to type .)

You'll notice that the  command did not open the interactive shell this time around, but instead executed the contents of. The script should print  and , then it should let you type until you press the enter key. Congratulations, you got your first Python script to work.

IDLE


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, you can skip this section.

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  file in the menu, and run the script directly from within the program by pressing F5 or selecting "Run" from the menu.

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  in the menu bar.

Assignments

 * Try out the Python Interpreter. Get the general feeling of using it.
 * Create a Python script. Then, try and run it, errors or not.
 * (Optional): Open up IDLE and play around with it. Use and customize it at your own will.


 * Previous Lesson: Why Learn Python
 * Next Lesson: To Get You Started
 * Course Home Page