Four Fours
Objectives
- Meet the Python interactive console
- Introduce working with Python files
Background
Finally time for Python! Python is a general-purpose programming language, which just means it can do a lot of things – way more than Picobot. It’s a very flexible language with readable syntax, powerful abstraction, and widespread use. Knowing Python is an incredibly practical skill and can be used in so many different areas: data science, video game development, dev ops (scripting), web development, and many more (too many to list). If you have a use in mind, come talk to any of us about it!
Over the next few days, we’ll be going through concepts in Python that will be the tools in your programmer toolbelt.
Getting Started
One of the great features of Python is its interactive console. We’ll be
referring to it just as “console” for brevity. Try opening it up either in the
terminal or directly in
vscode! When you’re in
your terminal of choice, type python and hit Enter. You should see
something like this, with your cursor positioned right after the >>>:
Make sure it doesn’t say Python 2.7.12 or any version of Python starting with a 2. It is an old version of Python. You should not use it!
Try a simple computation:
Not let’s try computing a power. Python uses ** as the power operator.
Careful trying any larger computations. You might crash Python!
If you find yourself in a scenario where you’ve hit Enter after typing something in either the Python console or terminal and something goes wrong, try hitting Ctrl+C a few times (hitting multiple times can matter in some cases). It tells the computer to stop whatever it’s doing and bring back a prompt.
Try copying the following into the terminal (in vscode or not).
We used four fours to make zero. We’re going to do this a bunch more in this exercise. But we don’t want to have to paste in a line of code every time we want to execute it. So, before we get to the exercise itself, let’s talk about how to create a Python file to store your code for easy editing and executing.
In previous exercises, we’ve done the following to make new text files. Note the
$ prompt indicating we’re in the terminal – not the Python console.
To make a Python file, we’ll do the same thing except instead of .txt we’ll
append .py to the filename. If you’re already using the vscode terminal, you
can just use the menu to make a new file. Otherwise:
In reality, both .txt and .py files both just contain characters (letters,
symbols, etc), but the file extension allows the Python interpreter to
recognize a file as having Python in it. You can think of a file extension as a
label to help your computer figure out how to handle a particular file. Don’t
worry too much about what a Python interpreter is. If you think of code as
instructions, the Python interpreter is who you’re instructing.
Now, paste the following code into your file and save it.
# Vassar CS Camp
# Filename:
# Name:
from math import *
print("Zero is", 4 + 4 - 4 - 4)Back in your terminal, navigate using cd to wherever you saved your file. You
want your working directory to be the directory that contains the .py file you
just saved. Once you’re there, enter the following replacing [your-file-name]
with whatever you saved your file as:
Make sure you have the .py on the end!
Tab completion is your friend! When you press Tab while typing in the terminal, it will try and finish what you’re typing for you. It knows the entire structure of your filesystem, so whether you’re typing the name of a file in your working directory or typing a long path, it can finish long names for you. If nothing shows up on the first press, hit Tab again and a list of possible autocompletions will print out. It will only every complete a word if what you’ve typed is the prefix of exactly one item in the relevant directory.
The Exercise
For the exercise, we’ll be adding more lines to this file. Remember to save after every addition – namely before you try and run the code (using
pythonin the terminal). Otherwise, it will run the code you had at your last save. See the previous exercise about vscode to turn on auto-saving!The goal is to use four fours in a mathematical expression to produce every integer value between 0 (which we already did) and 20. You can use the following operators:
+(addition)-(subtraction)*(multiplication)/(division)()(grouping)**(power/exponential)sqrt(x)(square root of x)factorial(x)(factorial of x)Right now your program only prints one line, but a complete program will print out the following:
Feel free to do more than 20 if you’re so inclined! And remember to ask for help if you’re stuck!
May the fours be with you.