Math in Python is easy + exercises 📐

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody in this video I'm going to show you all of the different math that we'll need throughout the rest of the series I have a lot to cover and I'll split this video into different sections we'll cover some basic arithmetic operators built-in math functions a few functions from the math module and then a few exercises be sure to look at the timestamps if you would like to skip ahead to another section let's begin with some really easy stuff we're going to cover some basic arithmetic operators let's say we have a variable friends currently you have zero friends if you need to increment a variable by one you could say friends the name of the variable equals the name of the variable again plus one so the plus sign is the addition operator and I think we do have a little bit of experience with that already so if I were to print my variable friends guess what you now have one friend we could also shorten this line of code you could say friends plus equals one that would do the same thing this is known as an augmented assignment operator that will give you the same result I prefer to use augmented assignment operators just because they take less text and I think they're easier to read now let's use subtraction friends equals friends minus two so of course minus is the subtraction operator you have negative two friends I guess if you were to use the augmented assignment operator that would be friends minus equals two there you still have negative two friends okay multiplication let's change friends to how about five friends equals friends times three you now have 15 friends then the augmented assignment operator version of this would be friends times equals three so again you have 15 friends let's cover division friends equals friends divided by two so we have 2.5 friends somebody was cut in half we have half a friend maybe it's just their legs or torso or something then the augmented assignment operator would be friends divided by equals two and the result is still the same now exponents friends equals friends to the power of two so friends is currently five friends to the power of two would be five times five which is 25. the augmented assignment operator version of this equation would be friends exponent equals two and again friends is 25. then we have modulus modulus gives you the remainder of any division suppose we have 10 friends instead of five I will assign a new variable remainder remainder equals friends the percent signed is known as the modulus operator it will give us the remainder of any Division if I were to divide my group of friends by three we will have one remaining I'll store the remainder within a separate variable we would have a remainder of one it's kind of like in class when the teacher says for everybody in the class to go into groups of three then there's always that one kid that's by themselves that's kind of the same concept we're dividing our friends into groups of three then the modulus will give you the remainder if we divided our group of friends into groups of two well ten divides by two evenly so there is no remainder so that is the modulus operator it's fairly popular to use this operator to find if a number is even or odd because it will divide by two evenly if that number is even if the remainder is 1 that means that the original number is odd okay so yeah those are some basic arithmetic operators addition subtraction multiplication division exponentiation then modulus now what we're going to do is cover some built-in math related functions suppose we have three variables x equals 3.14 y equals four Z equals five it doesn't matter if these are floating Point numbers or whole integers the first is the round function we have a variable named result I'm going to round X so there is a built-in round function after the set of parentheses we can add some value or variable to be rounded so we will round x to the nearest whole integer then print the result so our result is three so that's the round function with the absolute value function we can find the absolute value of a number let's change y to be negative four instead of four we'll take result equals ABS which means absolute value of y the absolute value is the distance away from zero as a whole number the absolute value of negative 4 is 4. let's change y back to four there's a built-in power function result equals pow then we'll need a base and an exponent what's y to the power of three that would be four times four times four which is 64. that's the power function you can raise the base to a given power the next two are really useful using the max function we can find the maximum value of various values what's the maximum value between X Y and Z then I'll just need to store this value result equals the max between X Y and Z well the maximum value is five otherwise there's min what's the minimum value between x y and z that would be 3.14 now in this next section we do have some very useful constants and functions from the math class but we'll need to import the math module at the top of our text editor so import math if you need the value of pi you'll type the name of the math module dot pi and I'm just going to print this print math dot pi the value of pi is 3.14159 and a bunch of digits that come after if you're working with physics I do know that people use the constant e a lot we won't be using e in this video series but if you ever need access to it just type math dot e and that will give you e which is 2.71 something something something I believe e is known as the exponential constant if you need the square root of a number let's say result equals math dot s q r t we can place a variable or a value within the square root function let's say we have X again x equals 9. what is the square root of x then I will print whatever the result is the square root of 9 is 3. that is the square root function there's a ceiling function result equals math dot seal seal will always round a floating Point number up suppose X is 9.1 so 9.1 rounded up is 10. otherwise there's floor which will always round a number down result equals math dot floor let's change X to 9.9 .9 rounded down is 9. those are some useful math functions let's go over some exercises okay this first exercise we are going to calculate the circumference of a circle we'll need the help of the math module because there's some good functions in there to calculate the circumference of a circle the formula is 2 times pi times R let's ask a user for a radius because that's what R is we'll accept some user input enter the radius of a circle we will Typecast the input as a floating Point number to calculate the circumference again the equation is 2 times pi we can get that from the math module times whatever the radius is and the user is going to type that in then we will print whatever the circumference is print we'll use an upstring the circumference is our variable circumference enter the radius of a circle I'll enter 10. actually 10.5 their circumference is 65.97 if you want to round and truncate some of these numbers we can use the round function around circumference then round to a given decimal place I'll round to two digits again 10.5 rounded is 65.97 you could add a unit of measurement to let's say centimeters 10.5 is 65.97 centimeters all right that is the first exercise for this next exercise let's calculate the area of a circle we'll import the math module we'll ask for a radius much like before radius equals input enter the radius of a circle we'll cast our input as a floating Point number the equation for the area of a circle is pi times radius squared we could easily use the built-in power function to raise our radius to the power of 2. then we will display the area print I'm using an F string the area of the circle is our area to some unit of measurement let's say centimeters squared enter the radius of a circle 10.5 the area of the circle is 346.36 but I would like to round this number to two decimal places I'll use that round function and I'll Place area and the number of digits to round two within this function let's try that again 10.5 the area of the circle is 346.36 centimeters squared that is the second exercise for this last program we're going to find the hypotenuse of a right triangle the formula to calculate the hypotenuse of a right angled triangle is C equals the square root of a squared plus b squared we'll Begin by importing the math module we'll ask the user for the lengths of side A and B a equals input enter side a will cast the input as a floating Point number we'll do the same thing with side B equals enter side B now this part's going to be confusing we'll calculate C we'll need a squared plus b squared we'll take a to the power of 2 Plus B to the power of 2. then we'll need the square root of all of this whatever the result is I will surround this equation with math dot square root and that should give us our answer let's print using an F string side C equals whatever C is so enter side a three side B will be four side C is five all right everybody so that was everything related to some arithmetic operators and math related functions in Python and in the next video we're going to cover a few things involving string formatting
Info
Channel: Bro Code
Views: 90,888
Rating: undefined out of 5
Keywords: python, math, arithmetic
Id: jc7TBgMS_kw
Channel Id: undefined
Length: 13min 50sec (830 seconds)
Published: Sat Oct 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.