Module 3: Integers | CMSC 105 Elementary Programming - Fall 2024

Module 3: Integers

Note: Create a text file called module3.txt where you will store you answers to exercise questions. The questions that are not related to changing code. You will submit this file on Blackboard along with your code.

Objectives

By the end of this module you will be able to:

First, an analogy

Suppose we have boxes. Consider the following rules about “boxes”:

Box 1

Box 2

There is a cloning process that works like this:

Box 3

x = y

Integer Variables

We’ll now start working with “boxes” (variables) that hold integers (whole numbers like 3, 17, 2097, but not numbers like 3.141).

Consider this program:

i = 5
print(i)

Exercise 1: Type up the program in my_variable_example.py. Also save the file so that it can be submitted on Blackboard. What does it print? Report what you see in module3.txt.


Now let’s examine key parts of this program:


Exercise 2: Type up the above in my_variable_example2.py and confirm that 3 is what gets printed.



By way of explanation:

i = 5
i = 3
print(i) # The most recent value of i (in this case, 3) gets printed



Exercise 3: Is it possible to not have a value in a variable? Consider this program:

i
print(i)

Type up the program in my_variable_example3.py What is the error? Answer in module3.txt. (Remember, non-coding questions are to be answered in your module text file, in this case: module3.txt.)


Thus: when you make a variable, you need to put something in it.

Next, let’s look at assignment between variables:

We say, in short, “i is assigned to j”.


Exercise 4: Consider this program:

i = 5
j = i
print(j)
print(i)   # Did i lose its value?

Type up the program in my_variable_example4.py and report what gets printed in module3.txt.



The above example illustrates that the value in i gets copied into the variable j, which means that the value 5 is still in the variable i.



Exercise 5: Consider this program:

i = 5
j = i
k = j
print(k)

Try to identify the output of this program just by mental execution. Next, type up the program in my_variable_example5.py and confirm.


Exercise 6: Consider this program:

i = 5
j = i
i = 0
k = j
j = 0
print(k)

Try to identify the output of this program just by mental execution. Then type up the program in my_variable_example6.py and confirm.


Note that a copied value does not change if the original is changed:


Exercise 7: Type up the following lines of code in my_variable_example7.py:

i = 5
j = 6      

# Add code between here

# and here.

print(i)   # should print 6
print(j)   # should print 5

Add some lines of code with the objective of swapping the values in variables i and j. You will need a third variable to be used as a holding place. Thus, without directly assigning the number 5 to j or the number 6 to i, write code using a third variable to achieve the desired swapping of values.


Integer Operators

Let’s examine the familiar arithmetic operators +, -, *, /


Integer division:

Expressions and operator-precedence

Consider the following program:

i = 5
j = 6
k = i * j - (i + 1) * (j - 1)
print(k)

Exercise 9: Type up the above program in my_expression_example.py What does it print? Answer in module3.txt.


About expressions:

Let’s dive a bit deeper into precedence and do some examples:


Exercise 10: What does the expression i*j - i+1*j-1 evaluate to when i = 7 and j = 3? Answer in module3.txt.


More expressions

The remainder operator %:

Consider this example:

i = 14
j = -6
k = i % (-j)
print(k)

Exercise 11: Can you mentally execute and identify what’s printed? Type up the above in my_expression_example2.py to confirm. Report the value in module3.txt.