Intro to Python

We’ll be using Python 3 because it’s free, powerful, full stack (Python is the second-best language for everything) and the lingua franca of data science. There may also be a little SQL.

Installation

Download

There are different installations out there, Anaconda is awesome but a bit bloated for what we need to get started. So let’s install standard Python 3 (Python 2 is being deprecated in 2020) and then use python to install the packages we need as we go (some miniconda stuff). Navigate to https://www.python.org/downloads/ and download the latest version for your operating system. Launch the executable, check the Add Python to Path box and accept the default settings.

Test installation

Open a terminal, type in python and press enter. You should see three chevrons >>>, see below. If this doesn’t work you probably missed the Add Python to Path box, just reinstall.

First program

Type print("Hello!") at the interactive prompt. You could also do some basic maths and then exit() when you’re ready to move on.

$ python
>>> print("Hello!")
Hello!
>>> (3 + 2) * 3
15

  • Why doesn’t it print 9?
  • What would the output from these be?
>>> 2 + 3 * 3
>>> 3 + 2 * 3
>>> exit()
exit

Some good ‘getting started’ guides

IDLE - Integrated Development and Learning Environment

Python is named after the 70’s comedy show Monty Python so there are many references to famous sketches in ‘the docs’ and code. For example, the package manager or pip, is commonly referred to as the ‘cheese shop’. Most languages have an integrated development environment, or IDE, which programmers use to write and maintain their code. Idle or the Integrated Development and Learning Environment (after Eric Idle) is the python one that ships with the standard installation. Some people prefer PyCharm but a text editor (like notepad++, sublime or vim) and the terminal also work well. Jupyter notebooks are extremely popular with the data science community. Amazon SageMaker comes with Notebooks, machine learning libraries and everything a developer would need to train, build and deploy a solution.

We’ll stick with IDLE for now as it is cross platform and designed for learning. It has a decent debugger which means we can step through code, even IDLE’s, which is part of the TKinter module. Reading or stepping through well written code is one of the quickest ways to gain fluency.

As we look at these different tools it will soon become apparent that there is a python module for just about everything. So if you are curious about a technology, say web development, try learning flask.

Open IDLE

Earlier we ran a program through the python intepreter using the terminal. Let’s do the same with IDLE. The main appeal of python is this interactive, experimental approach. It is possible to run complicated programs, load various data structures and then enter interactive mode and explore from there. Try this out:

name = input("Enter your name\n> ")
print("Hello", name)

First script

Click File > New File. You should now have the interpreter (or shell) next to an untitled text file. Put them side by side and rename the File > Save As hello in a new directory named code in your home directory. You’ll notice IDLE automatically adds the .py extension. Save the file and Run > Run Module or F5 to execute. You should see:

img0 view

Algorithms, Binary Search the Number Game

An algorithm is a set of steps or as Cathy O’Neil outlines in her book Weapons of Math Destruction, “an opinion embedded in code”.

# Guess the number game adapted from:
# https://inventwithpython.com/chapter4.html

import random # import module, try: import this

print("Hello!") 
name = input("What is your name?\n> ")

number = random.randint(1,100) # https://docs.python.org/3.7/library/random

print(f"Well, {name} I am thinking of a number between 1 and 100.")
# print(number) # debug statement

guess_cnt = 0 # initialize guess count

# Comparison operators, WHILE true and IF true
while guess_cnt < 7:  # true and false, binary and comparison operator

    # Get guess from user and cast to an int
    guess = int(input(f"Take a guess (total: {guess_cnt})\n> "))

    guess_cnt += 1 # increment the count

    # IF true,  check the user's guess
    if guess < number:
        print("Your guess is too low.")
    if guess > number:
        print("Your guess is too high.")
    if guess == number:
        break

if guess == number:
    print("Nice!  You guessed it.")
else:
    print(f"The number I was thinking of was {number}.")

# How might we:
# - use binary search?
# - check the user's input?
# - ask the user if they want to play again?

Next Steps

Updated: