Prev | Contents | Next

5 Data and Processing Data

5.1 Objective

For this chapter, we want to write a program that reads two numbers from the keyboard and prints out the sum of the two numbers.

5.2 Data, Variables, and Math

Problem-solving step: Understanding the Problem.

Data is the general term we use to describe information stored in the computer. In the case of programming, we’re interested in values that we’ll do things with. Like add together. Or turn into a video game.

Really, data is information. Can you glean information from a symbol? Then it’s data. Sometimes it’s a symbol like “8”, or maybe a string of symbols like “cat”.

Your goal as a software developer is to write programs that manipulate data. You have to manipulate the input data in such a way that the desired output is achieved. And you have to solve the problem of how to do that.

In a program, data is commonly stored in what we call variables. If you’ve taken any algebra, you are familiar with a letter that holds the place of a value, such as the “slope-intercept” form of the equation of a line:

\(y = mx + b\)

or of a parabola:

\(y = x^2\)

But beware! In programming code, variables don’t behave like mathematical equations. Similar, but different.

Enter the following code in a new program in your editor, save it, and give it a run. (This is just like you did with the program in the earlier chapter. You can name this one anything you’d like. If you need inspiration, vartest.py8 seems good to me.)

x = 34      # Variable x is assigned value 34
print(x)
x = 90      # Variable x is assigned value 90
print(x)

In Python, variables refer to values9. We’re saying on line 1 of the code, above, “The variable x refers to the value 34.” Another way to think of this that might be more congruent with other languages is that x is a bucket that you can put a value in.

Then Python moves to the next line of code and runs it, printing 34 to the screen. And then on line 3, we put something different in that bucket. We store 90 in x. The 34 is gone–this type of bucket only holds one thing10.

So the output will be:

34
90

You can see how the variable x can be used and reused to store different values.

We’re using x and y for variable names, but they can be made up of any letter or group of letters, or digits, and can also include underscores (_). The only rule is they can’t start with a digit!

These are all valid variable names (and, of course, you can make up any name you want!):

y
a1b2
foo
Bar
FOOBAZ12
Goats_Rock

You can also do basic math on numeric variables. Add to the code above:

x = 34      # Variable x is assigned value 34
print(x)
x = 90      # Variable x is assigned value 90
print(x)

y = x + 40  # y is assigned x + 40 which is 90 + 40, or 130
print(x)    # Still 90! Nothing changed here
print(y)    # Prints "130"

On line 6, we introduced a new variable, y, and gave it the value of “whatever x’s value is plus 40”.

What are all these # marks in the file? We call those hash marks, and in the case of Python, they mean the rest of the line is a comment and will be ignored by the Python interpreter.

One last important point about variables: when you do an assignment like we did, above, on line 6:

y = x + 40  # y is assigned 130

When you do this, y refers to the value 130 even if x changes later. The assignment happens once, when that line is executed, with the value in x at that snapshot in time, and that’s it.

Let’s expand the example farther to demonstrate:

x = 34      # Variable x is assigned value 34
print(x)
x = 90      # Variable x is assigned value 90
print(x)

y = x + 40  # y is assigned x + 40 which is 90 + 40, or 130
print(x)    # Still 90! Nothing changed here
print(y)    # Prints "130"

x = 1000
print(y)    # Still 130!

Even though we had y = x + 40 higher up in the code, x was 90 at the time that code executed, and y is set to 130 until we assign into it again. Changing x to 1000 did not magically change y to 1040.

Fun Tax Fact: The 104011 is nearly my least-favorite tax form.

For more math fun, you have the following operators at your disposal (there are more, but this is enough to start):

Function Operator
Add +
Subtract -
Multiply *
Divide /
Integer Divide12 //
Exponent **

You can also use parentheses similar to how you do in algebra to force part of an expression to evaluate first. Normal mathematical order of operations rules apply13.

 8 + 4  / 2   #  8 + 4  / 2 ==  8 + 2 == 10
(8 + 4) / 2   # (8 + 4) / 2 == 12 / 2 == 6

And you thought all that algebra wouldn’t be useful… pshaw!

There’s a common pattern in programming where you want to, say, add 5 to a variable. Whatever value it has now, we want to make it 5 more than that.

We can do this like so:

x = 10

x = x + 5   # x = 10 + 5 = 15

This pattern is so common, there’s a piece of shorthand14 that we can use instead.

These two lines are identical:

x = x + 10
x += 10

As are these:

x = x / 10
x /= 10

Here are a few of the arithmetic assignment expressions available in Python:

Operator Meaning Usage
+= Add and assign x += y
-= Subtract and assign x -= y
*= Multiply and assign x *= y
/= Divide and assign x /= y
%= Modulo and assign x %= y

These are very frequently used by devs. If you have x = x + 2, use x += 2, instead!

5.3 Assigning from One Variable to Another

Let’s check out this code:

x = 1000
y = x

Something interesting happens here that I want you to make note of. It’s not going to be super useful right now, but it will be later when we get to more intermediate types of data.

When you do this, both x and y refer to the same 1000.

That’s a weird sentence.

But think of it this way. Somewhere in the computer memory is the value 1000. And both x and y refer to that single value.

If you do this:

x = 1000
y = 1000

Now there are two 1000 values. x points to one, and y points to the other.

Finally, adding on to the original example:

x = 1000
y = x
y = 1000

What happens there is that first there is one 1000, and x refers to it.

Then we assign x into y, and now both x and y refer to the same 1000.

But then we assign a different 1000 to y, so now there are two 1000s, referred to by x and y, respectively.

(The details of this are rather more nuanced than I’ve mentioned here. See Appendix C if you’re crazy enough.)

Takeaway: variables are just names for items in memory. You can assign from one variable to another and have them both point to the same item.

We’re just putting that in your brain early so that we can revive it later.

5.4 Your Mental Model of Computation

Problem-solving step: Understanding the Problem.

This is a biggie, so listen up for it.

When you’re programming, it’s important to keep a mental model of what should happen when this program runs.

Let’s take our example from before. Step through it in your head, one line at a time. Keep track of the state of the system as you go:

x = 34      # Variable x is assigned value 34
print(x)
x = 90      # Variable x is assigned value 90
print(x)

Before we begin, x has no value. So represent that in your head as “x has no value; it’s invalid”.

Then the first line runs.

x is now 34.

Then the second line runs.

x is still 34, and we print it out. (So 34 is printed.)

Then the third line runs.

x is no longer 34. It is now 90. 34 is gone.

Then the fourth line runs.

x is still 90, and 90 gets printed out.

Then we’re out of code, so the program exits. And we have “34” and “90” on the screen from when they were printed.

That’s keeping a mental model of computation.

This is the key to being able to debug. When your mental computing model shows different results than the actual program run, you have a bug somewhere. You have to dig through your code to find the first place your mental model and the actual program run diverge. That’s where your bug is.

5.5 User Input

Problem-solving step: Understanding the Problem.

We want to get input from the user and store it in a variable so that we can do things with it.

Remember that our goal in this chapter is to write a program that inputs two values from the user on the keyboard and prints the sum of those values.

Python comes with a built-in function that allows us to get user input. It’s called, not coincidentally, input().

But wait—what is a function?

A function is a chunk of code that does something for you when you call it (that is when you ask it to). Functions accept arguments that you can pass in to cause the function to modify its behavior. Additionally, functions return a value that you can get back from the function after it’s done its work.

So here we have the input() function15, which reads from the keyboard when you call it. As an argument, you can pass in a prompt to show the user when they are ready to type something in. And it returns whatever the user typed in.

What do we do with that return value? We can store it in a variable! Let’s try!

Here’s another program, inputtest.py16:

# Take whatever `input()` returns and store it in `value`:

value = input("Enter a value: ")
print("You entered", value)

We can run it like this:

$ python3 inputtest.py
Enter a value: 3490
You entered 3490

Check it out! We entered the value 3490, stored it in the variable value, and then printed it back out! We’re getting there!

But you can also call it like this:

$ python3 inputtest.py
Enter a value: Goats rock!
You entered Goats rock!

Hmmm. That’s not a number. But it worked anyway! So are we all good to go?

Yes… and no. We’re about to discover something very important about data.

5.6 Data Types

Problem-solving step: Understanding the Problem.

We started with numbers, earlier. That was pretty straightforward. The variable was assigned a value and then we could do math on it.

But then we saw in the previous section that input() was returning whatever we typed in, including Goats rock! which is certainly not any number I’ve ever heard of.

And, no, it’s not a number, indeed. It’s a sequence of characters, which we call a string. A string is something like a word, or a sentence, for example.

Wait… there’s another type of data besides numbers? Yes! Lots of types of data! We call them data types.

Python associates a type with every variable. This means it keeps track of whether a variable holds an integer, a floating point17 number or a string of characters.

Here are some examples and their associated types. When you store one of these values in a variable, the variable remembers the type of data stored within it.

Example Data Type in English Type name in Python
2 Integer int
3490 Integer int
-45 Integer int
0 Integer int
3.14159 Floating Point float
-13.8 Floating Point float
0.0 Floating Point float
"Hello!" String str
"3490" String str
"" String (empty) str

In the examples, above, strings are declared using double quotes ("), but they can also be done with single quotes, as long as the quotes match on both ends:

"Hello!"  # is the same as 'Hello!'
'Hello!'  # is the same as "Hello!"

Okay, that’s all fine. But is input() returning a string or a number? We saw both happen when we tried it out, right?

Actually, turns out, input() always returns a string. Period. Even if that’s a string of numbers. Note that these things are not the same:

3490     # int, a numeric value we can do math with
"3490"   # string, a sequence of characters

Sure, they look kinda the same, but they aren’t the same because they have different types. You can do arithmetic on an int, but not on a string.

Well, that’s just great. The task for this chapter is to get two numbers from the keyboard and add them together, but the input() function only returns strings, and we can’t add strings together numerically!

How can we solve this problem?

5.7 Converting Between Data Types

Problem-solving step: Understanding the Problem.

If we can’t add strings mathematically, can we convert the string "3490" into the integer 3490 and then do math on that?

Yes!

In fact, we can convert back and forth between all kinds of data types! Watch us convert a string to a number and store it in a variable:

a = "3490"    # a is a string "3490"
b = int(a)    # b is an integer 3490!

print(b + 5)  # 3495

How did that work? We called the built-in int() function and passed it a string "3490". int() did all the hard work and converted that string to an integer and returned it. We then stored the returned value in y. And finally, we printed the value of b+5 just to show that we could do math on it.

Perfect!

Here are some of the conversion functions available to you in Python:

Function Effect
int() Convert argument to an integer and return it
float() Convert argument to a floating-point number and return it
str() Convert argument to a string and return it

So… given all that we know so far, how can we solve this chapter’s problem: input two numbers from the keyboard and print the sum?

5.8 Input Two Numbers and Print the Sum

Problem-solving step: Devising a Plan.

We know:

Now—how do we put all that together to write a program that inputs two numbers from the keyboard and prints their sum?

This is the Devising a Plan portion of problem-solving. We’re not going to write code to make this happen. We’re just going to write an outline of the individual steps the program must describe in a language called pseudocode (which is English that looks kinda like code).

Then when we’re satisfied it’ll work, we can code it up for realsies.

So stop here and take a moment to consider what the step by step might be to get this done.

Really, take a moment, because I’m about to give spoilers. Thinking about how to solve problems is 80% of what a software developer gets paid to do, so you might as well practice right now.

What do we know? What tools do we have at our disposal? What resources? How do I put all those together to solve this problem, like solving a puzzle?

Here’s some pseudocode that would get the job done, it looks like:

read string from keyboard into variable x
convert x to int and store it back in x again
read string from keyboard into variable y
convert y to int and store it back in y again
print the sum of x + y

If we’re satisfied that our plan is solid, it’s time to move to the next phase.

Problem-solving step: Carrying out the Plan.

Now let’s convert each of those lines to real Python. I’ll throw in the pseudocode as comments so we can see how they compare. (Source code link18.)

# Read string from keyboard into variable x
x = input("Enter a number: ")

# Convert x to int and store it back in x again
x = int(x)

# Read string from keyboard into variable y
y = input("Enter another number: ")

# Convert y to int and store it back in y again
y = int(y)

# Print the sum of x + y
print("The sum of the two numbers is:", x + y)

Save that file as twosum.py and run it:

$ python3 twosum.py
Enter a number: 9
Enter another number: 8
The sum of the two numbers is: 17

Too easy! Let’s challenge it:

$ python3 twosum.py
Enter a number: 235896423496865928659832536289
Enter another number: 94673984675289643982463929238
The sum of the two numbers is: 330570408172155572642296465527

Not even breaking a sweat!

Nice. Now, I want you to think like a villain. What would a villain pass into our program for input that would cause it to break?

Try all those things with your program. What happens when you try it? Which ones work and which ones don’t?

Notice that a big, spewing error message is really the worst case scenario here. And it’s not really that painful. Don’t be afraid to try to break your code. The computer can handle it. Just run it again.

Later, we’ll learn techniques to catch errors like this so that the program doesn’t bomb out, and prints a nice message to the user to please try again with valid input, thank you very much.

Notice that when the program crashes, buried in all that output, is the line number the program crashed on! Very, very useful! And the last line tells you exactly what Python thinks went wrong.

The point is, if you’re not sure how something will work, just try it. Experiment! Break things! Fix things! Again, the computer can absolutely handle it. It’s just a big sandbox for you to play in.

5.9 Wrapping it Up

Problem-solving step: Looking Back.

This grimly-named step is where we take a look at our code and decide if there was a better way to attack this problem. It’s important to remember that coding is a creative endeavor. There are many ways to solve the same problem.

Admittedly, right now, you don’t have many tools in the toolkit, so your creativity is limited. But eventually, in the not-too-distant future, you’ll know several ways to solve a problem, and you’ll have to weigh the pros and cons of each, and be creative and choose one!

What could be better?

What else could we do?

5.10 Exercises

“You know how to get to Carnegie Hall?”

“Practice!”

Zeus says, “This book assumes you complete all of the exercises!” and when Zeus speaks, people really should listen.

I know, I know. You get to the exercises part of a book and you just skip ahead. I mean, it’s not like I’m grading you or anything.

But there’s only one way to get to be a better dev: practice and repetition. Going through this book without doing the exercises is like training for a marathon by reading about how to run. It’s simply not going to get you there on its own.

Resist the urge to look at the solution until you’ve solved it! Give yourself a time limit. “If I haven’t solved this in 20 minutes, I can look at the solution.” That 20 minute isn’t wasted—it’s invaluable problem-solving practice time. During that time, you’re building a scaffold in your brain that can hold the solution once you see it.

If you just skip straight to the solution, look at it, and say, “Yup, makes sense, I got it,” you’re missing out on all that benefit.

Don’t shortchange yourself! Do the exercises! The more you do, the better dev you’ll be! I’m getting off my soapbox now!

Remember to use the four problem-solving steps to solve these problems: understand the problem, devise a plan, carry it out, look back to see what you could have done better.

Here they are:

  1. Make a version of the two number sum code that works with floats instead of ints. Do the numbers always add correctly, or are they sometimes just a little bit off? Lesson: floating point math isn’t always exact. Sometimes it’s off by a tiny fraction. (Solution19.)

  2. Have the program print out the sum and the difference between two numbers. (Solution20.)

  3. Allow the user to enter 3 numbers and perform the math on those. (Solution21.)

  4. Write a program that allows the user to enter a value for \(x\), and then computes and prints \(x^2\). Remember ** is the exponentiation operator in Python. 3**2 is 9. (Solution22.)

  5. Write a program that allows the user to enter a, b, and c, and the solves the quadratic formula23 for those values.

    A refresher: with equations of the form:

    \(ax^2+bx+c=0\)

    you can solve for \(x\) with the quadratic formula:

    \(x=\cfrac{-b\pm\sqrt{b^2-4ac}}{2a}\)

    This all looks terrifying! Can you feel your brain seizing up over it? Deer in the headlights? That’s OK. This is how developers feel when confronted with a new problem. Really! All of us! But what we know is that we have a problem solving framework we can use to attack this problem regardless of how difficult it seems initially.

    Remember: Understand, Plan, then Code It Up.

    Take a deep breath. Shake off the fear!

    You can absolutely do this. It’s not any harder than anything so far! Let’s go!

    Your program should plug a, b, and c into the above formula and print out the result value in x.

    Make sure \(b^2\ge4ac\) or there won’t be a solution and you’ll get a “domain error” when you try to take the square root of a negative number. Some test values for \(a\), \(b\), and \(c\) that work: 5, 9, 3, or 20, 140, 60.

    What is that \(\pm\) symbol after \(-b\) in the equation? That’s “plus or minus”. It means there are actually two equations, one with \(+\) and one with \(-\):

    \(x_{plus}=\cfrac{-b+\sqrt{b^2-4ac}}{2a}\)      \(x_{minus}=\cfrac{-b-\sqrt{b^2-4ac}}{2a}\)

    Solve them both and print out both answers for a given \(a\), \(b\), and \(c\).

    What about that square root of \(b^2-4ac\)? How do you compute that? Here’s a demo program for computing the square root of 2—use it to learn how to use the math.sqrt() function, and then apply it to this problem.

    import math      # You need this for access to the sqrt() function
    
    x = math.sqrt(2) # Compute sqrt(2)
    print(x)         # 1.4142135623730951

    Code that up and, hey! You’ve written a program that solves quadratic equations! Take that, homework! (Solution24.)

  6. Followup to the previous question: after computing x, go ahead and compute the value of

    \(ax^2+bx+c\)

    and print it out. (You can use either the plus solution or the minus solution—doesn’t matter since they’re both solutions.) The result should be exactly 0. Is it? Or is it just something really close to zero? Lesson: floating point math isn’t always exact. Sometimes it’s off by a tiny fraction.

    Sometimes you might get a number back that looks like this, with a funky e-16 at the end (or e-something):

    8.881784197001252e-16

    That’s a floating point number, but in scientific notation25. That e-16 is the same as \(\times10^{-16}\). So the math equivalent is:

    \(8.881784197001252\times10^{-16}\)

    Now, \(10^{-16}\) is actually a really, really small number. So if you see something like e-15 or e-18 at the end of a float, think “that’s a really small number, like close to zero.”

    (Solution26.)

  7. Make up two more exercises and code them up.

And don’t worry–we’ll get away from the math examples soon enough. It’s just, for now, that’s about all we know. More soon!

5.11 Summary

This chapter we covered:

It’s a great start, but there’s plenty more to come!


Prev | Contents | Next