Title: Lesson 1: Installing and starting Python
Course: Basic scripting and programming > Section: Course Content

Learn about different versions of Python. The selected version will be downloaded and installed on the system. The Python integrated development environment is used to create the first program.

Objectives:

- Download and install Python
- Get familiar with the Python interface and interpreter

Entrance requirements:

- Basic computer skills
- Know how to install new software

Required materials

- PC or laptop with Linux or Windows
- The course book “Think Python - How to Think Like a Computer Scientist”
Make sure you get the second edition of the book for Python 3! ( http://www.thinkpython.com )

Learning outcomes:

At the end of the lesson the student:

- knows about different versions of Python
- knows how to obtain Python
- knows how to install Python
- knows how to start Python
- is familiar with the interface and interpreter
- knows about syntax highlighting
- is able to create and run the first Python program

Learning activities:

- Read chapter 1 “The way of the program” of the book “Think Python”

Tasks – Installing and running Python:

Currently there are two different versions of Python, Python 2 and Python 3. There is a difference between the two versions, however for this course it doesn't really matter which version you use; it could be either Python 2 or Python 3. Newer versions of Python 2, like Python version 2.7, are mostly forward compatible with Python 3. If Python is already installed on your system (either version 2 or 3) you can use that. If you want to install Python yourself it is best to install the latest version of Python 3.

- If you want to know more about the difference between Python 2 and 3 you can check this webpage: https://wiki.python.org/moin/Python2orPython3

For the following we will assume that you want to install Python 3 on your system. There are various distributions of Python, but here we will assume that you want to install the official Python distribution. The installation itself depends on your operating system. First we will discuss the installation on Linux, and then Windows and Macintosh.

Installation on Linux

Python 2 is installed on most Linux systems. To check if you have Python, open a terminal and type python or python3 on the command line. If this works then you already have a working version of Python on your system. On Linux you need administrator rights to install software. If you are an administrator then you can install the python3 package using any of the package managers (Synaptic, Ubuntu Software Center or apt). To use the command line simply issue the following command on the command line. This will also install IDLE.

- sudo apt-get install python3 idle3

Installation on Windows

Most Windows systems don't have Python. To install Python you have to download the latest release and install it as administrator. If you don't have administrator rights on your system please ask your system administrator to do this for you.

- Go to the following web page and download the Latest Python 3 Release:
https://www.python.org/downloads/windows/
- Download the “Windows x86-64 executable installer”.
- Right-click the executable file you've just downloaded and chose Run As Administrator. This will install Python 3 for all users on your system.

Installation on Mac OS X

Macintosh users can pick up an installer from the following webpage:

- https://www.python.org/downloads/mac-osx/

Other Devices

Python runs on many other platforms as well. If you want to run Python on an Android device for instance, you can pick up the Qpython or QPython3 app from the Google Play store.

Running IDLE

Any programming language needs a so-called integrated development environment (IDE). The default IDE for Python is called IDLE (Integrated Development and Learning Environment). We will use IDLE throughout this course. IDLE is intended to be a simple IDE and suitable for beginners, especially in an educational environment. IDLE has syntax highlighting, auto-completion, smart code indentation and more. For big programming projects you might want to switch to another IDE for Python.

- Start IDLE on your system.
- On Linux start IDLE by typing “idle3” in the dashboard, using the Applications menu (Programming → Idle3), or by typing idle3 on the command line.
- On Windows start IDLE by using the menu: All Programs → Python 3.x → Idle 3

IDLE uses what is called syntax highlighting for different pieces of code.

- To discover which colors are used for which code go to:
- Options → Configure IDLE → Highlighting
- Which colors are used for Python keywords?
- Which colors are used for Python Definitions (such as function definitions)?
- Which colors are used for Python Builtins?
- Which colors are used for normal text such as variable names?
- Which colors are used for error text?
- Which colors are used for Python comments?
- Which colors are used for Python strings?
- Close the Settings window by pressing Cancel.
- Enter the following code in the Python Shell (after the prompt >>>):
print("hello world")
and press “Enter”

Congratulations! You have just run your first Python statement in IDLE.

- Open a new script window by going to: File → New File
- Enter the same “hello world” print statement as was used before.
- Save the script: File → Save As
- Save the script in a directory were you can find it again. Give the file name the extension .py
- In the script window go to: Run → Run Module
- Your script will now run and print “hello world” in the Python Shell.

Congratulations! You have just created your first Python script. Not surprisingly, the first way of running the hello world print statement is called “interactive mode,” the second way is called “script mode.”

Make sure you have read chapter 1 of the book “Think Python” before continuing on the following tasks.

Tasks – exercises:

- Do exercise 1.1 of the book.
- Do exercise 1.2 of the book.
- Find the website that contains the documentation of the Python language.
- In the Python interpreter, enter the expression help('keywords')
- How many keywords does the Python language have?
- How many built-in functions does Python have? Check chapter 2 of the Library Reference of the Python documentation to find the answer
(https://docs.python.org/3/)
- Can the keywords or built-in functions of Python be used to easily calculate the square root of 2? Does this surprise you?
- In the interpreter, enter dir(__builtins__) What do you get?
- In the Python interpreter, enter the expression 2**1000.
What is calculated? Can you confirm that this is correct or not?

SUBMIT all your answers in a PDF document!

Planned time frame:

5 hours