Python Concepts/Introduction and Setup

From Wikiversity
Jump to: navigation, search


Books-aj.svg aj ashton 01f.png
  • Get Python on your computer.
  • Learn how to work Python from command line.
  • Learn how to make a Python script.
  • Learn the bare minimum of IDLE.


Getting Python[edit]

Before you can enter into the magically world of Python, you'll need to have Python on your computer. Luckily, there's a chance that you won't need to download it, since it could already be installed on your computer (feeling happy Linux users?). To find out if you already have Python installed on your computer, open up your computer's CLI and type python. If you see a response from Python, some stuff will pop up on the screen, including the word Python followed by the version number and build information. There is an example below showing Python running on 32-bit Windows XP.

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Here is another example on a 64-bit Windows 7.

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:16:31) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

If you need to install Python, you should download the most recent stable version. Python 2.7.9 release is the latest in the 2.X series, and Python 3.4.3 is the latest in the 3.X series. You'll find Python's major implementation, CPython, available on Python's website.

Attention yellow.svg Note: This resource uses Python 3.X over Python 2.X, because Python 3.X is is under active development and its being used a lot more than it used to be. The final version of Python 2.X is Python 2.7, which was released in 2010; no further major releases of Python 2.X are planned.[1] It would be wise to view Python 2.X as legacy and join the Python 3.X bandwagon, since Python 3.X is the future.

For Mac OS X and Microsoft Windows users, you can easily download an installer that does all the work for you. If you're using Linux or any Unix-like operating system, you should be able to build the binaries by downloading the source code. Some Linux distributions and flavors have easier ways to obtain Python, like having pre-made binaries for download and installation. Many popular Linux distributions and flavors will have documentation on their websites.

Attention yellow.svg Note: Many distributions of Linux and Mac OS X come pre-installed with Python. I know, that isn't worth of note, but it is. Even though Python 3.X has been around for sometime now, the operating systems mentioned come with Python 2.X by default![2] You may have Python install on your computer, but unfortunately it isn't Python 3.X compatible! If this is a problem, you can easily download and install/build Python 3.X on your computer.

Online Interpreter[edit]

If you feel concerned about downloading Python onto your computer or you just can't do that, then you're in luck. The Python Software Foundation offers an interactive Python shell from PythonAnywhere on their website. It includes more implementations than just CPython, plus you don't need to install it to use it. On the downside, you cannot execute scripts and files from the shell, which will hinder more advanced Python topics that you'll see later on in the resource.

The Shell[edit]

The shell, command prompt, 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 examples of Python code being used in a shell. This is what the shell may look like when you run Python on your computer.

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

You may not get the first couple of lines exactly like it's pictured 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.

>>> print("Hello, world!")
Hello, world!

Attention yellow.svg Note: The startup information about Python is excluded from most examples for space and readability.

As you can see, a newline without the prompt is output. Another important thing to know is that the prompt changes to three periods (...) if you enter in a command with indentations. Overall, you'll need to be careful not to mistake the prompt for actual Python code.

>>> if True:
...     print("Hello, world!")
Hello, world!

Before we go ahead, you should open up the python prompt in your operating system's shell. Simply open up a new shell[3] and type python into it. (If you have versions 2 and 3 installed you may need to specify the version, e.g. python3 in order to override the default, e.g. 2.7.) Congratulations, you now have a working Python interpreter.

Python Script[edit]

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, plain text files and bytecode files. Plain text files are simple files that you can create and edit with just a simple plain text editor, like Notepad, Notepad++, or Python's IDE, IDLE. The other type of file, bytecode files, are compiled CPython files. Don't worry about this type of file, which will be discussed later on in this course.

Attention yellow.svg Note: If you do not have a plain text editor, you can find a list here. It is very unlikely that you don't have a plain text editor, since they usually come with most operating systems. Another solution is to use Python's IDLE, which will be discussed in the next section.

A Python script file name ends with the extension .py 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.

print("Hello world, from a Python script!")
input("Press enter to continue . . . ")

Attention yellow.svg Note: If you are using a Unix-like operating system, like Linux or Mac, you'll need to include a shebang at the beginning of your script, and make the file executable. The first line should read #!/usr/bin/env python. This tells the shell where the interpreter is. The great thing about a shebang is that it starts with a hash tag (#), which is ignored as a comment on operating systems that do not use shebangs. Now that's portability!

Attention yellow.svg Note: Window users, Python files are executed with py.exe. By default, it will start with Python 2.X if it's installed on your computer. If you execute py, it will tell you which version is running. If you have Python 2.X, but you want it to execute Python 3.X, you'll need to use the switch -3. If you want Python 3.X to be the default Python version on your computer, open up your control panel and create the environment variable PY_PYTHON, then set its value to 3. If you want Python 2.X to be the default Python version, delete PY_PYTHON and change its value to 2.

Now, run the script by either clicking on it or by running it from your system's shell. The script should prints Hello world, from a Python script! and Press enter to continue . . . , then it should pause until you press the enter key. Congratulations, you got your first Python script to work.

Attention yellow.svg Note: Windows users, if you cannot create a file with the extension .py, then you need to either create a text file and then save it with the .py or you'll need to change your Hide extensions for known file types settings in the control panel. You should be able to find it under Folder Options. There are options if you don't want to do either of these steps, but be carefull.[note 1]

Attention yellow.svg Note: If you're having trouble getting a script to work, you can always ask a teacher or contributor for help.


This is an example of IDLE 3.4.3 running on Windows XP.

IDLE, or Integrated Development Environment, is the default IDE that comes when you install CPython. On windows, it can be accessed by right clicking the Python script and then by clicking on Edit with IDLE. An alternative is to open IDLE via the shell using the command: python -m idlelib.

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 monocolored 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 Options in the menubar.


Crystal Clear app kedit.svg
  • Try out the Python Interpreter. Get the general feeling of using it.
  • Create a Python script. Then, try and run it, errors or not.
  • Open up IDLE and play around with it. Use and customize it at your own will.
Sweden road sign A10.svg Completion status: Ready for testing by learners and teachers. Please begin!


  1. 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 your 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. The 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! It probably won't work at first, so you'll need to restart the computer.