- Make sure you have a text editor (I recommend Atom)
- Download this zip with a folder and some files inside.
Reviewing lesson content
In the first meeting, we talked about how this club works, and also some content on expressions and operations.
If you want to review what the lesson was about, you can go back to the CSS curriculum page and view the slides or the video.
This practice shows you the basics of using Atom and Python. If you need help with any of the problems, feel free to contact me through Wechat or just ask in the group chat.
Installing Python on your computer
Python3 is our programming language. Download and install it here:
If you’re having trouble opening the installer, you can right-click the icon and select “Open”.
For those who are complete beginners, I recommend using Atom as your text editor for coding. Here’s a link to downloading it:
Of course, if you prefer other text editors, you can use those too.
P.S. If you want cool tips for using Atom, such as how to make it autocomplete your code, come ask me!
Using the terminal
First, open a terminal, if you haven’t already.
Right now, I’m in my home directory. The home directory is represented by the
~ symbol. When you first open your terminal, you will start in the home directory.
Don’t worry if your terminal window doesn’t look exactly the same; the important part is that the text on the left-hand side of the
~(tilde). That text might also have the name of your computer.
The first command we’ll use is
ls. Try typing it in the terminal:
ls command lists all the files and folders in the current directory. A directory is another name for a folder (such as the
Documents folder). Since we’re in the home directory right now, you should see the contents of your home directory.
To move into another directory, we use the
cd command. Try typing the following command into your terminal:
cd command will change directories — in other words, it moves you into the specified folder. In the example above, we chose to move into the
If we want to go back to our home directory, there are a few ways to do so:
cd ..(two dots). The
..means “the parent directory”. In this case, the parent directory of
Desktophappens to be our home directory, so we can use
cd ..to go up one directory.
cd ~(the tilde). Remember that
~means home directory, so this command tells your terminal to change to the home directory, no matter where you currently are.
cd(that is, the
cdcommand with no arguments). Typing just
cdis a shortcut for typing
At this point, let’s create some more directories. Go on your Desktop and make a folder for CSS! Make sure you name it as “css”.
Make sure you’ve downloaded the files for today’s lab. Unzip the file, then put the folder inside the css folder you just created.
Make sure you are in the
~/Desktop directory, using the necessary
cd commands. Then, cd into the lab01 folder.
cd ~/Desktop cd css cd lab01
Now if we list the contents of the directory (using
ls), we’ll see some files.
lab01.py is where you will be writing your code and answering questions, while tests.py contains the autograder.
The Python interpreter in Terminal
We will be running our Python code using the terminal. Before trying out what’s below, make sure that you have Python3 installed already.
First, let’s try running some Python code line-by-line. Type in python3 in your terminal like below:
Now try typing up some expressions and hit enter! For instance:
>>> 1+1 2 >>> 2*2 4 >>> 3/6 0.5 >>> 1+2+3+4+5*6*7/2/3 45.0
Python will evaluate the expression and tell you what the final value is.
Now, hit control+z to quit the Python interpreter.
Next, let’s learn how to run execute an entire Python file. Make sure that you are in the lab01 folder.
If you type
python3 lab01.py, it should execute the entire file, outputting something like below:
Notice that it says NameError and tells you which line the error is on.
This is because we haven’t done anything to the file yet! So don’t worry.
Now, open up the lab01.py file and we can start doing the questions!
When you’re done filling everything out, use
python3 lab01.py again and run everything! It will tell you whether you passed everything or if there are errors. See the Error section for more info.
Basics of programming with Python
Recall from the meeting that we learned about expressions and operators.
- We know that expressions evaluate to values. For instance:
>>> 1+1 2 >>> 2*2 4 >>> 3/6 0.5 >>> 1+2+3+4+5*6*7/2/3 45.0
- We also learned what primitive expressions are: they evaluate directly to values with minimal work needed:
>>> 3 3 >>> "Hi" 'Hi' >>> True True >>> False False >>> 3.141592 3.141592
Don’t worry about the different data types yet if you don’t know them. For this lab, we’re only going to work with real numbers.
- Finally, we learned that operators combine primitives expressions into more complex expressions. Note that operators are in fact values too—that’s why we can evaluate them.
Usually, in Python, operators’ names go before the inputs, and the inputs are inside parentheses, separated by commas. These are call expressions:
>>> add(2, 3) 5 >>> mul(5, 6) 30
We can nest call expressions too:
>>> add(add(1, 2), mul(3, 4)) 15
However, +, -, *, / are special cases. You can use them between the inputs just like on a calculator:
>>> 2 + 3 5 >>> 5 * 6 30 >>> 1 - 1 0 >>> 3 / 6 0.5
Python follows the PEMDAS rule, so use parentheses when needed:
>>> 1 + 1 / 2 1.5 >>> (1 + 1) / 2 1.0
More handy operators
Floor division //
Basically, it divides numbers then rounds it down.
>>> 1 / 2 # Normal Division 0.5 >>> 1 // 2 # Floor Division 0
This gets you the remainder after division.
>>> 10 / 3 # Normal Division 3.333333333333333 >>> 10 % 3 # Modulo 1
We all know what exponents are. Right?
>>> 2 ** 2 4 >>> 4 ** 0.5 2.0
If you don’t know what a fractional exponent means, don’t worry: an exponent of 0.5 is the same as getting the square root.
Doing The Assignment
Do the actual coding in the
lab01.py file with Atom! I’m only describing what the problems are below.
What Would Python Display? (WWPD) problems:
In these problems, replace the “___” below the question with what Python would display if you evaluated the last line. Example:
#Q1. 1 * 2 answers.append(___) To answer it, replace the ___ with the answer: #Q1. 1 * 2 answers.append(2)
— Section 1 —
Q1. 10 + 2
Q2. 3.5 / 2 * 2
Q3. 7 // 2 * 2
Q4. 7 % 2
Q5. 5 – 2 * 3
Q6. (5 – 2) * 3
Q7. 2 ** 3
Q8. subtract(1, 2)
Q9. subtract(subtract(6, divide(6, 3)), divide(10, 5))
Free Response Questions (FRQs)
I know that the code below may seem a little confusing. But, don’t worry! For this lab, you can not care about what keywords such as def mean.
Just focus on the underline ______, it should be an expression!
Q1. Twenty nineteen
Come up with the most creative expression that evaluates to 2019, using only numbers and the +, *, and – operators.
def twenty_nineteen(): """ >>> twenty_nineteen() 2019 """ return ___ # Replace ____ with your code here
Q2. Get last digit
Get the last digit of an integer x.
def get_last_digit(x): """Get the last digit of an integer x >>> get_last_digit(10) 0 >>> get_last_digit(15) 5 >>> get_last_digit(1337) 7 """ last_digit = ___ # Replace ____ with your code here return last_digit
Q3: Remove last digit
Remove the last digit of an integer x.
def remove_last_digit(x): """Remove the last digit of an integer x >>> remove_last_digit(10) 1 >>> remove_last_digit(25) 2 >>> remove_last_digit(1337) 133 """ new_x = ___ # Replace ____ with your code here return new_x
After you complete all the problems, take a screenshot of the doctests passed like one below. You don’t need the red arrows and boxes. You will upload this at the very bottom of this page.
Notice how there’s a lot of “ok”s and says “Test passed.” and “Fill in the blanks are all correct” in the bottom.
The “ok”s indicate that an FRQ question passed a single test case (meaning it expects some value and did get that value), and the output “Test passed.” indicates that all your FRQs are correct.
“Fill in the blanks are all correct” is pretty self-explanatory.
How would you know if your code or response is incorrect?
If a fill-in-the-blank is incorrect, the terminal would output something like below:
Notice how it tells you which question in which section is incorrect. Go back and check your response!
If an FRQ is incorrect, the terminal would output something like below:
Notice how theres a lot of stars *********************************
Those stars tell you what test cases failed.
For instance, if you look at the
remove_last_digit, question above, when
remove_last_digit(10) is evaluated, the program expects 1, however it got 0 as a result from running your code.
Also notice that at the bottom it tells you how many test cases passed and how many failed.
You’re done! Upload your screenshot here!
If you encounter any issues, please contact me through Wechat, thanks!