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.
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
>>> 2 + 3 * 3
>>> 3 + 2 * 3
>>> exit()
exit
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.
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)
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:
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?