Lab: Exploring Types And Operations
Preparation
a. Make sure you are booted into the Mac OSX side of the computer.
b. Once you are logged in, double click the icon named courses.ads.carleton.edu
on your desktop. If you encounter any errors, have me or the prefect contact Mike Tie to set up your account.
c. Open up a Terminal
, navigate to your StuWork
directory, create a folder named lab-04-05
, and change your directory to the newly created folder. (Remember that you should replace USERNAME with your actual username!)
The Terminal
application should remember your previous directory from last class period, but below are the commands for changing into the StuWork
directory, making the directory, and changing into it.
$ cd /Volumes/COURSES/cs111-03-s19/StuWork/USERNAME/
$ mkdir lab-04-05
$ cd lab-04-05
d. Open Brackets
and make sure you have your StuWork/USERNAME
directory opened. (You will see all of its subfolders in the left panel if so.) If the folder is not opened, you can open it by selecting File -> Open Folder
and navigating to courses
, then cs111-03-s19
, then StuWork
, then USERNAME
.
Exercise 1
In this exercise, we will experiment with using variable names.
a. In your Terminal
, type python3
to open up the REPL environment and execute the following commands and replace the word NAME
with your actual name.
>>> name = "NAME"
>>> num = 128
>>> pi = 3.1415
b. Verify that the variable names have been properly defined by executing the following commands to see what they print.
>>> print(name)
>>> print(num)
>>> print(pi)
c. What happens if you try to print a variable that hasn’t been defined? Try doing so in the REPL to see what happens.
d. The built-in print
function is used to actually output text to the user. However, the REPL environment will show you the result of evaluating expressions even though they normally wouldn’t be displayed. Try typing out the following commands and note the results that are displayed by the REPL.
>>> num / 2
>>> 2 * pi
>>> name
Since the print
function is not used, these expressions do NOT print to the display if you executed them outside of the REPL environment. For example, if you had the above expressions in a file named test.py
and ran python3 test.py
, only text that is explicitly printed via the print
function will be displayed.
The REPL actually showing you the result of the expression without needing to call print
every time is a nice feature. It allows us to easily test expressions and quickly see what the result is.
e. Note that print
is also a variable. It’s default value is the location of the built-in function that prints to the display. What do you think will happen if you type the following into the REPL? Test your guess by actually executing it.
>>> print
f. The basic values stored inside a variable name can be changed by executing another assignment statement. Verify this by typing
>>> num = 17
>>> num
g. Do you think you can write assignment statements backwards? Try typing
>>> 20 = num
Remember that assignment statements are always of the form <IDENTIFIER> = <EXPRESSION>
and creates (or updates) the variable associated with <IDENTIFIER>
with the result of evaluating the expression <EXPRESSION>
.
h. What do you think will happen if you change the value of the print
variable? Execute the following to test your guess.
>>> print = "abc"
>>> print
i. What happens if you try to call the print
function after you’ve changed its value now?
>>> print("Hello world!")
Keep in mind that if you change the value of print
in your program, you cannot change it back without restarting your program or the REPL! Thus, it is a good idea to never change the built-in functions in your program.
j. Exit the REPL by typing exit()
and reopen it by typing python3
so that the print
function is reinitialized.
k. What happens if you try to name a variable that starts with a number or special character? Try it out in the REPL.
(In Python, all variables must start with a alphabetic character or underscore _
. Any later character in the name can consist of characters, numbers, or underscores.)
Exercise 2
In this exercise, we will experiment with how expressions are evaluated into their basic values.
a. For each of the following expressions, evaluate the expressions by hand and write down what you expect the result to be. Then verify your results by typing them into the REPL.
>>> 5 * 4 + 3
>>> 5 * (4 + 3)
>>> 2 ** 4 - 1
>>> ((15 // 2) + 2) * 3
b. If you and your partner aren’t sure what the **
and the //
operators do, play around with them in the REPL until you have a good idea what they are doing. Then ask the instructor or prefect to confirm your thoughts.
c. We can also have expressions that evaluate to a string literal. Try the following in the REPL to see what they evaluate to.
>>> "Hello world!"
>>> "Fall" + "Winter" + "Spring"
>>> "Hello, " + name + ". It is nice to meet you!"
The +
operator on strings means “concatenate”. It will join two strings literals together into one.
d. What do you think will happen if we use the +
on two different basic value types? Guess what the following command will do and then test your prediction.
>>> num = 4
>>> "The number I am thinking of is: " + num
e. You cannot concatenate num
onto the previous string without first turning it into a string. We can do this by using the built-in str
function which turns basic values into their textual representations. When applied to numbers, it behaves as you might expect. Try each of the following in your REPL.
>>> str(23)
>>> str(num)
>>> "The number I am thinking of is: " + str(num)
>>> 123 + 456
>>> str(123) + str(456)
Exercise 3
In this exercise, we will explore the differences between the int
and float
types of numbers.
a. For each of the following, predict whether you think the expression will evaluate to a value of type int
or a float
.
-
a + b
wherea
andb
are bothint
s -
a + b
wherea
andb
are bothfloat
s -
a + b
wherea
is anint
andb
is afloat
b. Test your predictions in the REPL.
>>> 5 + 4
>>> 3.14 + 2.7
>>> 4 + 2.7
c. For each of the following, predict whether you think the expression will evaluate to a value of type int
or a float
.
-
a / b
wherea
andb
are bothint
s -
a / b
wherea
andb
are bothfloat
s -
a / b
wherea
is anint
andb
is afloat
-
a // b
wherea
andb
are bothint
s -
a // b
wherea
andb
are bothfloat
s -
a // b
wherea
is anint
andb
is afloat
d. Test your predictions in the REPL.
e. Try running 15.0 // 3
in the REPL. Why do you think it returns a float
here? Discuss this with your partner.
f. Integers in Python are perfectly precise and can have an arbitrarily large number of digits. Notice that even the following expression will evaluate correctly:
>>> 11111111111111111111111111111111111 + 2
g. Try to generate really large integers to see if you can break the computer. (It is easy to generate large numbers with the **
operator. For example, 2 ** 100
. If the terminal freezes up, you can force it to quit by pressing Ctrl + C
on the keyboard.)
h. Floats in Python are only approximate. Try typing the following into the REPL.
>>> 0.1 + 0.2
i. Discuss with your partner what you think happened above. What problems should we watch out for when working with imprecise numbers like floats?
j. Try experimenting with operations on various floats to see what sort of errors accumulate over time.
Exercise 4
In this exercise, we will experiment with the print
function.
a. Create a new file named print_test.py
in your lab directory with the following text.
# print_test.py
# Titus Klinge, YOUR NAME(S) GO HERE
# 2019-04-05
#
# A simple program to test the print function
print("Hello world!")
print("This", "print", "has", "multiple", "parameters")
print("This print line has a special end character", end=";")
print("12345")
print("This", "is", "a", "combination", end="THE END")
b. If you are still in the REPL, exit out of it by typing exit()
.
c. Make sure you are in the lab-04-05
directory by typing pwd
.
d. Run your python program by running python3 print_test.py
.
e. Answer the following questions with your partner.
- What happens when you separate strings with commas in the
print
function? - What is the role of the
end
named parameter? - Since the
end
parameter is optional, does it have a default value? If so, what is it?
Exercise 5
In this exercise we will experiment with input.
a. Create a new file named input_test.py
with the following text.
# input_test.py
# Titus Klinge, YOUR NAME(S) GO HERE
# 2019-04-05
#
# A simple program to test the input function
x = input("Please enter a number to be stored in x: ")
y = input("Please enter a number to be stored in y: ")
print("The sum of the numbers is ", x + y)
b. Run the program by typing python3 input_test.py
into the terminal
c. Was the output what you expected? Experiment with what is going on by running the program a few more times with different numbers. Also, try typing in something OTHER than numbers to see what it is doing.
Note that the input
function always returns what the user types as a string. If you have a number in a string form, such as "123"
, you can turn it into an integer by using the int
function: int("123")
.
d. Fix the input_test
program by changing x
and y
into integers before adding them.
Finished Early?
If you have extra time, start working on Assignment 2 with your partner!
Before You Leave
Get in the habit of emailing the code written during class to your partner. Since only one account is being used at a time, the files created will only be accessible from the user that is logged in. Later in the course, having these in-class files will be essential for reviewing and studying!
You should also consider exchanging contact information with your partner so that you can set up a meeting to work on the assignment.