Introduction to Python
This activity will walk through how to install and run the Python tools we will be using
Task 1: Install a syntax highlighting text editor
Google docs, Microsoft Word, and Apple's Pages are excellent products for writing papers, but they are absolutely unfit for the task of writing code. We'll be using a text editor built for these tasks. For this class there are two acceptable options, Sublime Text and Atom. Sublime provides a bit more functionality in its default configuration, but will display a pop-up every 20 saves asking to purchase the license. If you continue with the free version, it won't limit the functionality in any way. Atom focuses on expandability and provides more packages to expand its basic functionality. It also doesn't have a paid license, so it will never ask you for money.
Task 2: Install Python and its packages
Python is a programming language that provides a useful core functionality, but it is also supported by numerous additional libraries written by a large community. A library is just a set of additional functionalities that can be used by Python code you write. Installing the required libraries for all the functionality you are looking for can be a time-consuming process. Fortunately, people have created packaged versions that includes all the tools we'll be using, called Anaconda. Unfortunately however, anacondas are in the boa family, while pythons are in their own family. So the wordplay the creators of the Anaconda package were going for just doesn't work.
As a programming language, Python recognized limits to its syntax and design many years ago. However, they found that the changes they wished to make would be too disruptive to projects that were already working on. Therefore, they created version 3 of Python, which is what we'll be using in class. Python 2 will no longer be maintained by the core team after 2020 (there is even a countdown), so it is much better to develop in Python 3 so our code will be usable many years down-the-line.
Download the Python 3.6 version at the Anaconda download page. For Mac users, download the Graphical Installer. Once downloaded, run the installer. For both Mac and Windows users, install to the suggested default location.
Task 3: The Command-Line
The command line is a interactive way of running non-graphical programs on your computer. Both Windows and Macs have a command line tool, so click on the link to the appropriate page for your computer
Click here to run through the Mac Terminal walk-through
Click here to run through the Windows cmd walk-through
Task 4: Try it out
Open the text editor you downloaded in task 1 and create a new file. In this file copy and paste the following code:
#! /usr/bin/env python3
It is a good practice to add this line to all your python scripts, but it is not absolutely essential. It helps identify to your computer how to run your program.
Next, copy and paste the following line to your file:
print() is a function that prints the value inside to the command line from which you are running your python script. As we saw in Google Sheets, any text contained within quotes ("") will be interpreted as a text string. In this case, we're going to print the words Hello, World!.
Click on the menu File->Save. Save this file with the name "hello.py" to the "cs0030_workspace" directory you created above in your home directory.
Open up your command line program, and navigate to the cs0030_workspace directory. Type
python hello.py to run your new program! If you you don't see the phrase Hello, World! or if it prints an error, try to identify any issues in your code.
Once you're done, either show your working hello.py Python file to a TA in class or share it with email@example.com by midnight, 2/16 to be checked off for this activity.