🥳 GOSKILLS TURNS 10: Get 10 days of free access with code 10YEARS

GoSkills
Help Sign up Share
Back to course

Math Operators

Quiz me Quiz Compact player layout Large player layout
Focus video player for keyboard shortcuts
Auto
  • 720p
  • 540p
  • 360p
1.00x
  • 0.50x
  • 0.75x
  • 1.00x
  • 1.25x
  • 1.50x
  • 1.75x
  • 2.00x
cc

We hope you enjoyed this lesson.

Get the Introduction to Python course for more great video tutorials.

Start free trial

Cool lesson, huh? Share it with your friends

Facebook Twitter LinkedIn WhatsApp Email

  • Lesson resourcesResources
  • Quick referenceReference
  • Transcript
  • Notes

About this lesson

How to do basic math in Python.

Exercise files

Download this lesson’s related exercise files.

Math Operators
58.7 KB
Math Operators - Solution
58.8 KB

Quick reference

Math Operators

Python makes math very easy to do.

When to use

Any time you need to do math in Python, use these math operators.

Instructions

Math operators in Python are pretty straight forward:

  • +  Add
  • -  Subtract
  • *  Multiply
  • /  Divide
  • **  Exponents
  • %  Modulus

Pay attention to the order of operations.

Multiplication and division get executed before addition and subtraction; but anything inside parenthesis get's executed first.

Hints & tips

  • Math operators in Python work exactly as you think they should
  • Remember your math order of operations! 
  • You can do math on numbers or on variables too
Login to download
  • 00:04 In the next video, I want to talk about number data types.
  • 00:08 But before that, in this video, I want to just talk about basic math and
  • 00:11 the math operators.
  • 00:13 So to do math in Python, it is super, super easy.
  • 00:16 We just use the same math symbols that you learned in grade school math for
  • 00:21 basic math operations, addition, subtraction,
  • 00:24 multiplication, division, exponents, and modulus.
  • 00:27 And you're probably familiar with exponents we'll talk about it in a second,
  • 00:30 but the modulus here may be something you haven't seen before,
  • 00:33 we'll get into that in just a minute as well.
  • 00:35 So to do math in Python super, super easy, could not be easier.
  • 00:40 You just go print, just like whenever we want to print something to the screen.
  • 00:43 Now you don't have to use print to use math, but if you want to print the outcome
  • 00:46 of your math onto the screen, obviously you need to use print.
  • 00:49 So we can just go 2 + 3, if we save this and run it,
  • 00:53 it's going to be five obviously.
  • 00:56 So math is really just this easy in Python.
  • 01:01 So 10- 3, obviously is going to be 7.
  • 01:03 To multiply you just use this star.
  • 01:06 Save that, 30, and to divide, let's make this a nice and round number.
  • 01:12 We just use the forward slash.
  • 01:14 Remember we did the backslash to escape something out.
  • 01:18 This is the forward slash, that means divide.
  • 01:20 So if we save this it's going to be 5.
  • 01:22 Super easy.
  • 01:23 So exponents, you're taking something to the power of something.
  • 01:27 So if we took, for instance, 2 to the power of 3,
  • 01:31 we would use this double multiplication sign, which means exponents.
  • 01:36 So 2 to the 3rd power would be basically the same as 2 * 2 * 2.
  • 01:42 Now if it was 2 to the 4th power, it would be * 2, 4 times, 1, 2, 3, 4.
  • 01:49 Here we have 3 *, so it's 1, 2, 3 quick exponent primer.
  • 01:54 If we save this 2 * 2 * 2, 2, 4, so it's going to be 8, right, yep, 8.
  • 01:59 So that just leaves the modulus.
  • 02:01 And the modulus kind of can confuse people sometimes.
  • 02:04 And it's actually very useful for a lot of different things and
  • 02:07 we'll see you later on in the course.
  • 02:09 But if we were to take 10 divided by 3, well, 3 goes into 10 three times.
  • 02:15 So 3 times 3 is 9, right?
  • 02:16 Well, 10 minus 9 is 1.
  • 02:19 So 3 with 1 remainder with 1 leftover.
  • 02:23 Well, the modulus returns that remainder, that leftover thing.
  • 02:26 So if we save this and run it, we'll get one because that's the remainder.
  • 02:31 The modulus just returns the remainder.
  • 02:33 So super useful if we had 2, 10 divided by 2 is 5 with no remainder,
  • 02:38 there's no leftover because 5 times 2 is 10.
  • 02:41 So if we run this, we're going to get 0.
  • 02:44 So that's the modulus, super easy.
  • 02:46 The only kind of weird thing about math is something called order of operations.
  • 02:49 And you probably remember this from grade school math.
  • 02:51 But if we have 2 + 3 * 2, what is the answer going to be here?
  • 02:57 Is it going to be 2 + 3 equals 5 times 2 equals 10?
  • 03:03 Or is it going to be 3 times 2 is 6 plus 2 equals 8.
  • 03:09 Care to make a guess?
  • 03:10 Well, the answer is going to be eight and that's because of the order of operations.
  • 03:14 In math, in math world there's a hierarchy of which things get done first and
  • 03:20 multiplication and division are more important than addition and subtraction.
  • 03:24 So they get one first and then the addition or
  • 03:26 subtraction gets done afterward.
  • 03:28 So in this case we're taking 3 * 2, which is 6, and then adding 2 and we get 8.
  • 03:33 You can override this, and you do that with more parentheses.
  • 03:38 So, for instance, we could go wrap these guys in parentheses.
  • 03:42 And in the world of order of operations parentheses are more important than
  • 03:46 multiplication and division.
  • 03:48 And therefore also more important than addition and subtraction.
  • 03:52 Basically what it means is anything inside of these parentheses
  • 03:54 is going to be done first and then outside your normal order of operations,
  • 03:58 your normal precedence takes over.
  • 04:01 So we have 2 + 3 equals 5, times 2 equals 10.
  • 04:04 If we run this, boom, we get 10.
  • 04:06 So that's math.
  • 04:07 That's order of operations, which is very, very basic.
  • 04:10 And in this video, we took these and we just wrote the numbers right in here.
  • 04:15 You could also put num 1 = 1, num 2 = 2.
  • 04:23 And then with variables you go num 1 + num 2,
  • 04:27 save this we should get 3, and we have 3.
  • 04:31 So we can use the numbers themselves.
  • 04:33 You can use variables.
  • 04:34 You can use all these different operators and these signs +,
  • 04:38 -, *, /, these are called operators.
  • 04:41 They're the math operators.
  • 04:42 So if you hear the word math operators, it just means math sign.
  • 04:45 So that's all for this video.
  • 04:46 In the next video we're going to look at the numbers data
  • 04:51 types specifically floats and integers.

Lesson notes are only available for subscribers.

String Manipulation
04m:42s
Numbers
03m:51s
Share this lesson and earn rewards

Facebook Twitter LinkedIn WhatsApp Email

Gift this course
Give feedback

How is your GoSkills experience?

I need help

Your feedback has been sent

Thank you

Back to the top

© 2023 GoSkills Ltd. Skills for career advancement