Numbers and Operators in Programming: A Deep Dive

Explain numeric data types, math operators, and the conversion between strings and numbers in Python.

Discover how Python handles numbers, mathematical operators, and type conversions, and learn the essential differences between integers, floats, and strings. Master effective techniques to seamlessly convert and manipulate data types in Python for error-free programming.

Key Insights

  • Understand Python's numeric data types: integers (whole numbers such as 8) and floats (decimals like 0.08) and the usage of mathematical operators (+, -, *, /, ** for exponentiation, and % for modulus) which perform arithmetic operations.
  • Recognize the critical distinction between numeric data types and string representations of numbers ("stringy numbers"), which appear as digits in quotes, causing concatenation instead of arithmetic operations and resulting in errors unless explicitly converted.
  • Learn about functions like int(), float(), and str() to convert data types, allowing you to perform math operations (such as adding a 20% tip to a bill) or concatenate numbers into strings effectively (such as creating a message with SAT scores).

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

This is a lesson preview only. For the full lesson, purchase the course here.

There are two kinds of numbers. There are integers and decimals. So let's declare a variable, X. We'll call it 8. We'll print X. And by the way, conventionally, you wouldn't really call a string X, probably.

X sort of implies it's going to be a number. And then we'll say Sales Tax. And we'll call that 8%, which is actually, of course, 0.08.

That would be 8%, right? So let's print X and the type of X. Let's print Sales Tax and the type of Sales Tax. And that will be an int and a float. 8 is an integer, but 0.08 is a float.

Math Operators. You do math with what are called Math Operators. The plus sign—we already looked at that for what is called concatenation, where you're combining strings.

But of course, the plus sign can be used to add numbers together. The minus sign for subtracting. One asterisk is a multiplication sign. Slash for division.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Double asterisk is for exponentiation, raising to the power. So X double star 3 is X to the third power, or X times X times X. If X is 5, we would get 125, for example. X times X times X is X to the third.

Now, if you want to do X squared, you could just say X times X. But if you want to go to the X squared, it's just as easy to write X times X as X double star 2. X double star 2 is X times X. X double star 3 is X times X times X. Put these on their own line. Make it a little easier to read. Let's render that.

There. X squared, X cubed. Now, there's this percent sign that does not mean percent.

It is called the Modulus Operator. And what it does is it goes in between two numbers. It divides the number on the left by the number on the right and returns the remainder.

So n1 mod, as it's called, n2 will give you the remainder of n1 divided by n2. So let's take that for a spin—along with all the other operators.

So let's try this. We'll start with X = 5. And then we'll just work on X. We'll never change X. But we'll just keep changing a value of Y, setting it in terms of X. So an expression is when you have a variable that you don't set to a hard number. You set it in terms of other variables.

So Y = X + 3, that is an expression—setting a variable's value in terms of another variable. And if we print Y, it'll be 8, right? X plus Y equals X plus 3. It is just as good as writing a 5 there.

Let's try another one. We'll say Y now. X is not changing.

It's still 5. Y = X—2. If Y is 5 minus 2, Y should now be 3, which it is. And we'll try Y = X * 7. So X is still 5. 5 times 7 is 35. And we'll say Y = X / 2. X still is 5, holding steady.

So 5 divided by 2 is 2.5. And then Y = X ** 2. 5 to the second power, 5 squared. 5 times 5 is 25. And lastly, Y = X mod 2 returns the remainder of X divided by 2. And 5 divided by 2 gives you a remainder of 1, which is actually a common check used to determine if a value is odd or even, right? If you take any integer and divide it by 2 and you get a remainder of 1, it is odd.

Inversely, if it's a remainder of 0 on that integer divided by 2, it is even. So here we have taken our Math Operators for a spin. Let's go ahead and pause and do that too, please.

OK, now numbers—there's a difference between a number and a so-called stringy number or just a digit. So you could have a number in quotes, and it would actually not be a number, but characters—number characters or digits. So strings like "Apple, " except it's digits.

And the way you might ever have those is any time you have a text input in programming, the value extracted from that—like if it says "enter your tip amount" or something—you type a number in there, 20 or something. That number is actually a string because you're typing it in a text box, and you have to convert it to a real number. When you have so-called stringy numbers—that is, numbers in quotes—you can't really do math with them. They'll just CONCATENATE.

So let's try that. Let's say we have a score of "89." And then you get a bonus of 3. And then you think, oh, great, we're going to update our score.

So let's print the data type of score. Now, is that going to be an integer or a string? It's in quotes, so it's going to be a string. And if we print the bonus data type, that's going to be an int because it's not in quotes.

So there's our string "89" and our int of 3. Now, let's uncomment this. Score = score + bonus. What's that going to be? Is that going to be 92? Or something else? Let's print the score.

Oh, we have an error. The error says can only CONCATENATE string, not int, to string. So it did not work.

It broke. In other words, it took a string score and tried to combine it with an int (3), and it didn't know what to do. Is it supposed to CONCATENATE these? Is it supposed to add them together? So what if we take score? Let's try this.

We'll take score and pass it to int, which will convert the stringy "89" into a real 89. And then when you add 3 to it, you'll get 92. There it is.

Int, stringy number. Actually, let's hold off on that. Let's leave the error for reference.

We'll just turn it off. We don't want the error, but we do want to see it. We want to know that that's an error.

OK, bring you to the next cell. We have int, float, and string. These are functions that take values and convert them.

If you pass a stringy number to int, it'll convert the stringy number into a real integer. So in the case of "89, " what it would do—or what it did before—is strip off the quotes, leaving an actual number. Float will take a stringy number like "89" and convert it—what would have been an integer—into a float. Actually, wait a second.

No, it's only going to work on numbers. So you could feed an int in here, and it'll turn into a float. And if you take a number, like an int or a float for that matter, and pass it to string, it'll return to a stringy number.

So let's try these out. We're going to first take score and turn it into an int. We'll say score = int(score).

And then if you print score and the type of score, you'll see that it is now an int. It's 89, but it's not 89 the string anymore—like it was before, right? That's 89 the string.

89 the string—the original with the quotes. But then we take that string and pass it to int, and now you've got 89 the int—with which you can do math—is the point, right? Now we're going to say score = score + bonus. They're both numbers, so now you're good to print your score, which is 92.

And let's take another. We'll say score = "89.4." And we'll say score = score + bonus. And it's going to break, right? You can't do it.

Why? Because we've gone back to a string. But if we say float, now you're taking the "89.4" and floatifying it. Let's change the name.

Doesn't like that. We don't want to use the same name. All right.

That's what you get for a reason. Ah, there we go. OK.

So here's what we have. We have "89.4" for the string. You see what I did there? I had the variable take out in the wrong data type and name.

I wanted to restart the runtime session and run all that move I showed you. OK, so we have 89.4, and then we turned that into an actual number—a float in this case. And then we add the bonus to it.

Any kind of math you might want to do with a stringy number will fail. So let's make another one. We'll call it test_score = "95."

It's a higher score, but it's still got a problem. It is a string. If we reduce the test score by 10, we'd expect a result of 85, right? So let's say test_score = test_score—10.

And if we try to run that, it'll fail because you can't do math with quotes, right? You can't have a stringy number when you're doing math. We could say int(test_score) to ensure that we're dealing with a number. OK, little challenge.

Increase test_score by—let's reset test_score to 95. And I'd like you to increase it by—let's make it 85 here. We'll make it 80.

I'd like you to increase the test score by 20%. So the expected result would be 96, right? 10% of 80 is 8,20% is 16. So increase by 20%. Like, what if—here we'll call this cafe_bill, not test_score.

So you want to increase cafe_bill by 20%, which would be like a tip, you know, if you want a real-world analogy here. So you got your cafe_bill of "80, " but it's a stringy 80. How do you turn it into a real number? And then how do you make it go up by 20%? Pause, give this challenge a try, and we'll look at the solution when you're done.

OK, let's look at the solution to the challenge. We have a cafe_bill of "80, " but it's a stringy 80. We could say cafe_bill—or maybe we could just say total, right? Total_bill or bill_plus_tip, maybe.

Bill including tip, or bill_with_tip, sorry. I'm trying to make it shorter. The bill with the tip is cafe_bill times 1.2, right? That'd be a way to add 20%.

And if you run that, it breaks because that's a stringy number, right? It's not really a number, it's a string. We could say int, or we could just say float for that matter, right? Like, let's not assume a whole number. Floatify that, and there you go—96.

So wrapping stringy numbers in int or float, and then doing math, right? Or you can't perform math if it's still a stringy number—is the idea. OK, so kind of the opposite of taking a stringy number and making a real number out of it that you can do math with is to take a real number and stringify it. So like, you might want—what if you wanted to CONCATENATE numbers into a stringy message? You have to have the whole thing be a string.

So you can't put a number inside concatenation. Let's see what happens if you do. So let's say you have an SAT_math score of 730.

And then we're going to say SAT_report = "Your math score is " + SAT_math. And we want it to say "Your SAT math score is 730." That's the goal here.

That's the string we're going for. But if we run it, it breaks because 730 is a number, and you cannot CONCATENATE with a number. Kind of the flip side of the idea that you can't do math with a stringy number—you can't CONCATENATE with a real number.

We're going to take our SAT_math number and stringify it so that it can be used in string concatenation by passing the number to the str method. And now it does work. All right, another little challenge.

Add a verbal score. We don't call it verbal anymore. But add a verbal SAT score.

And CONCATENATE the full report. We're going to add a verbal score of 690 and CONCATENATE the full report. So the expected output will be "Your SAT math score is 730, and your verbal score is 690."

Or we can just not even say "Your." Maybe make it a little more like that. SAT math, how’s that? SAT math, SAT scores.

Let's try that. Try to make that verbal—math and verbal—just like so. Pause, do the challenge, see what you come up with.

OK, so we know what our expected output is. And we don't have a verbal score, but we know what that's supposed to be too. We're going to say SAT_verb = 690.

And SAT_report—we'll just trim it here. SAT scores: math. And that's where we want the math.

And then we need the verbal, which follows this pipe: verbal, space, verb. It's actually SAT_verb, I should say.

Keep it consistent. Run—boom—it breaks. And it breaks because you cannot CONCATENATE with a number.

We have to stringify SAT_verb, the number, into a stringy "690." And there you go. All right, and if we print str(SAT_verb), the type of that, and then the value itself—

We have SAT_verb missing. OK, there you go. So 690 is a string.

Once you convert it—if you pass 690 to the str method—it’s a string. But if you don’t, it’s an int, right? Check the data type of all versions of SAT_verb—the original num and the string. And indeed, you have the int and the string.

The only difference being you pass the int to the str method, which you need to do when you are concatenating here. So again, pause, get all this written out before you move on, please. And then we’ll keep it going.

Brian McClain

Brian is an experienced instructor, curriculum developer, and professional web developer, who in recent years has served as Director for a coding bootcamp in New York. Brian joined Noble Desktop in 2022 and is a lead instructor for HTML & CSS, JavaScript, and Python for Data Science. He also developed Noble's cutting-edge Python for AI course. Prior to that, he taught Python Data Science and Machine Learning as an Adjunct Professor of Computer Science at Westchester County College.

More articles by Brian McClain

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram