Unit 1

Programming Basics

[2.1] For each of the data values on the left, write on the right what data type it is:

0
False
"CMU"
14
True
-7
3.14

[2.2] What will the code on the right evaluate to?

3**3
(16-7)/3
"Love" + "You"
"Yay" * 3
"yay" == "Yay"
3 <= 3.14

[2.3] For each of the expressions below, what type will it evaluate to?

15-110
"Hello" == "World"
"13" + "24"
6.0 * 1.0
4/2

[2.4] Using the Python interpreter, display some text using the function print.

[2.5] Using the Python interpreter, store a string value in a variable and then print the contents of the variable using the print function.

[2.6] Repeat the above two exercises using the editor window.

Variables and Data Types

[2.7] What is the output from the following?

#1.
a = 3
b = 4
x = a * b
print(x)
#2.
a = 4
b = 2
x = a / b
a - 2
x + 3
print(x)
#3.
name = "Fran"
name = "Kelly"
print(name)
#4.
a = 3
b = -5
a = a * b
print(a)
#5.
a = 4
b = 2
b = b - a
b + 10
print(b)
#6.
s = "Welcome"
s = s + " to 15110"
print(s)

[2.8] Write Python code to do the following:

  • Create a variable named num and store the value 34 to it.
  • Add to num the value 10 and store the new value in num.
  • Change the data value of num to be “15-110”.

[2.9] Use the print function to print on the same line the string “The value is: ”, the boolean value True, and the numbers 34 and 0.7.

Errors

[2.10] You try to print your age in the Python interpreter as shown below, however you get the following error. What type of error is this error? What does the error mean, namely what is the issue with the code given?

[2.11] Write a program (one or more lines of code) in Python that won’t run because it contains a syntax error.

[2.12] Write a program (one or more lines of code) in Python that crashes once it is running because it has a runtime error.

[2.13] Try to run each of the following lines of code in Python (copy-paste them as they are given below). On the right, write the type of the error as well as what the error means, namely what is the issue with the code given?

Print("I am a student")
print("Hello World"
print "Hello World"
print("Hello World)
print(Hello World)
        print("hi!!!")
p r i n t ( "Try me" )

[2.14] We’ve seen that n = 42 is legal. What about 42 = n?