Go to this site to download a zip file and the instructions to play the game. It is a text-based game to help you get comfortable navigating around the command line. Yes, you could easily cheat! But the point is to use the game to make learning these commands more fun. Could you give boring exercises if you prefer.
Can work on your own or in pairs.
This week is about getting comfortable with the building blocks of Python and how to start constructing a new program.
We'll start with working in the interactive python shell.
- Open the terminal within VS Code and type:
python
-
You have now started an interactive session in Python. Go through the below exercises inside the interactive shell.
-
When you want to end a session, either type the following line or type together the CTRL button with D:
exit()
-
Type in the code to compute
10 + 5
,3 - 99
, and5.5 * 3.2
-
Notice that the answer is printed immediately. Now save each answer in a variable:
a = 10+5
b = 3-99
c = 5.5*3.2
- Nothing prints immediately. Now type:
a
The value held by a
is now printed.
The value held by a
is now printed.
- Try typing:
print(b)
This is an alternative way to print and the way you print when writing code in a saved file (which you'll do later).
Numbers are either represented as whole numbers (integers or int
) or as numbers with a fractional part or decimal point (float
).
Most of the operators are what you would expect - add with +
and multiply with *
. Note that division with /
will always give an answer as a float
and //
will give an int
which is the mathematical floor of the result of the division. You can use the type( )
function to see the datatype of a variable.
- Type in and run the code below to see some examples.
type(100 * 8)
type(0.6 + 4)
You can find more information in the Python docs.
- Compare the output from
9/3
9/3.
9//3
and9//2
and check their data types.
Like in other programming languages, you can create a variable that has a name and holds a value. What is different about Python is that you don't need to say what kind of data the variable is holding. Python will just note the data type you've used. For example, in Arduino or another C-language you would write:
int number = 47;
But in Python you would write:
number = 47
Note a couple differences. First there isn't int
in front of the Python variable, but there also isn't a ;
at the end.
# A longer example (this is a comment, not code)
apples = 3
oranges = 8
fruit = apples + oranges
print(fruit)
- Create two variables
width
andheight
with two different values, then calculate the area (width * height
) and store it in a new variable calledarea
. Finally print the value held inarea
.
Strings are text and need to be within pairs of ' '
or " "
. More details can be found in the Python Docs. It's just best to stick with a single style and not mix both ' '
and " "
in the same code. For Computing-1 we'll choose to use ' '
for all strings.
You can concatenate strings with the +
. Try it out.
- Create two variables that each store a string -
'hello'
and'world'
, then combine them into a new variable calledsentence
that printshello world
.
HINT: You'll need to add a space somewhere.
Longer strings that span multiple lines can be contained by a pair of three double quotes.
- Add another line of text to the below variable
long_string
and print the result.
long_string = """
This is my very long string.
It can even contain line breaks.
"""
A Boolean datatype can only hold one of two possible values: True
and False
.
# Two different Boolean variables
# Also, lines that start with # are comments
# These aren't interpreted by the Python interpreter
truth = True
lies = False
A Boolean comparison like >
or <=
generates a Boolean value.
- Try typing in the following code into the interactive shell.
4>6
5==5
10<=20
- Change the above code so that they each print
False
.
It's helpful to read the error messages that Python outputs when it comes across an error. You can read more about error messages in the Python Docs
- Run the below code and read what the error message says is going wrong, then fix the code so that it no longer causes an error.
a = 123
print(A)
4 + '32'
answer = 42
print answer
There are a large number of modules included in Python that can give you additional functionality. The math
module is one of them.
import math
print(math.e)
- Go to the module documentation and find out how to finish the code below.
import math
radius = 5
# Calculate the area of the circle using the variable radius
# Hint: you don't need to type out pi, it is included in the math module
area =
-
Using functions from the
math
module, use code to solve the following equations:y = 2 + sin(x) when x = 3
y = log(x-3) + 2(x+4) when x = 5
-
You can 'tab complete' inside of the interacive Python shell. Type
math.c
and hit 'tab' twice quickly to see all the functions withinmath
that start with the letter c. -
You can view the help documentation for a function directly in the terminal. For example
help(math.floor)
shows the documentation for the floor function. The 'Q' key quits the help interface and returns you to the interactive Python terminal. Find and view the help documentation for taking the square root of a number.
Note that importing modules only lasts for a single session of the interactive shell. If you leave the session by typing exit()
and then start a new session with python
, you will have to re-import any modules you want to use. All the previous variables you had created in earlier sessions will also be erased. This is a good feature - it means you know you are starting from a clean slate whenever you start a new interactive shell session.
However, when you want to start building up more complicated code, you don't want to type it all out each time. Instead you can save your code in a text file ending with .py
. This indicates is Python code.
- Inside of VS Code create a new file (you can do that from the File menu). Type the following code:
message = 'If this has printed, you have run the code successfully.'
print(message)
-
Save the file as
firstProgram.py
. You can save it wherever you'd like on your computer, but keep track of where that is as you'll need to use the command line to get there. -
In the terminal in VS Code, use
cd
to move to the same folder as where you savedfirstProgram.py
. -
Run the code by typing
python firstProgram.py
-
Instead of starting the interactive Python shell, the Python interpreter will run the Python code in your file and then return to the command line when all the code has been run.
HINT: If at any point the code seems to be stuck and isn't ending, you can type CTRL
+ c
to quit the running code.
-
Modify your Python code in
firstProgram.py
so that it asks the user to enter a message and then prints the message again for the user. The interaction would look like this:
$ python firstProgram.py
$ What is your message? This is my message.
$ This is my message.
Building on the Python code asking for a message and then printing it again, write a program that asks the user to guess a number between 1 and 10. The program then prints whether the number is equal to the random number generated by the code. So it then prints True
and ends the program if the input number is equal to the random number. If it is not equal to the number generated by the code, it prints False
and ends the program.
As a starting point, try writing code to match this pseudocode (a mix between natural language and generic technical language):
1. Prompt user for input
2. Generate a random number
3. Compare generated number to input
4. Print result
As data read in by input()
is always saved as a string
, here is some code to help you convert the input number into an int
(this is called casting something as an integer).
guess = input()
guess = int(guess)
HINT 1: Look through the random
module for useful ways to generate a random number.
HINT 2: You don't need to use if
statements - we haven't learned how to use them in Python yet. Think about how you can use a Boolean variable in a clever way with the ==
comparator.