Python Part 14a - Working with numbers Office

Python Part 14a - Working with numbers Office So welcome to this tutorial on working with numbers in python it's the first of four i'm working with different data types so here's what you'll learn during this.

Tutorial i want to begin very briefly by explaining what's different about this tutorial against all the others i've done in this series.

And then we'll look at numerical operators in python so things like plus minus divide and times and a few other ones as well we'll look at some of the mathematical.

And numerical functions built into python and we'll look at how you can check and convert numbers i think that's enough of me my picture is going to vanish but my alter ego spam.

Will guide you through the rest of this tutorial so let's get started so before we begin i just want to quickly explain why this tutorial is.

Different from all the others in this python series all the others emphasize learning how to do tasks i hope they do anyway i've tried to make them as practical and.

Python Part 14a - Working with numbers

Interesting as i possibly can this one's slightly different it's more of a reference guide so what i'll do is i'll be typing code in just to show you how you can do different things with.

Numbers dates and times strings and billions but remember you can click on the link at the top right of the screen at any time to download any files for this.

Tutorial and they'll include any code that i type in during them and if you can't see the link at the top right as some people i gather can't you can always go to the link at the start.

Of the youtube page for this tutorial which will take you to exactly the same page and you'll be able to download any files i've created so let's get started with this.

So to start with i've created a file called basicoperations.py and the program prints out a value of a variable which doesn't yet exist and that's why it's underlined so let's.

Create it what i'm going to do firstly is to create some basic operations so the main operators you can use are plus minus times and divided by and python as every.

    Other language firstly uses something

    Called bottomless to determine the order of operations and what that means is the multiplication and division take.

    Precedence at precedence over addition and subtraction so for example if i do 2 times 3 plus 4 times 5 what python will do is do the 2 times 3.

    First which is 6 then do the 4 times 5 which is 20 add them together and you'll get the answer 26. if i want to change that i need to use.

    Brackets to do so so by putting these brackets in it ensures it will do the 3 plus 4 first and it will give me 2 times 7 times 5 which is 70. so it changes the answer.

    You can also use the remainder symbol as an example of how to do this let's take 22.

    And divide it by seven the remainder of that is one which is why you get that a slightly different one you can use is.

    To do 22 and then do two divided by signs 1 after the other and that gives you the counterpoint to the previous one that will give me 3 because when you divide 22 by 7 it gives you 3 with 1.

    Left over so those are two quite useful symbols you can use one other symbol you can use is raising to the power.

    So to do this i could put result equals and what i'll do is 2 raised to the power of 10 which i think is 1024 and see if i'm right i am in addition to all of those you can.

    If you want to have an operation working

    On a number itself or variable itself you can use this syntax uh we've seen it quite a few times already so i could say result.

    Plus equals one and what i'll do is take result equals result plus one is just a shorthand for that so it will give me 1025 because my.

    Last answer was 1024 and you can do that with absolutely anything so for example if i set result equals three and then the result.

    With any of the operators i'm going to choose the power operator so if i do result equals raised to the power of two what it will do is take result as 3.

    And then raise out to the power of 2 and give me 9. and those are the main operators that you can use in python so to illustrate functions in on numbers.

    I've created another file called functions.py and again i'm printing out the value of a variable which doesn't yet exist but will soon so you can use built-in functions i'll.

    Just show you a couple of examples you can find the absolute value of a number which is its positive part so if i were to print out for sorry if i would set result equals.

    Absolute minus three then what i'll do when i run that is i'll get three because that's the positive part you can round numbers so let's round pi.

    Four decimal places so if i type in three point one four one five nine and round that to four decimal places then i'll get three point one four one six it's just a way of displaying.

    Significant figures and perhaps another useful function is to get the minimum and the maximum of a number or a set of numbers.

    So for example i could take the maximum of 2 3 8 and four and that will give me eight because that's the highest of the values in the.

    List and if i change max to min you won't be surprised to know it will give me two so those are the well i don't know about the main some of the useful functions.

    But the most useful ones you can get at are from importing the math module so what this allows you to do for example is to show the value of pi or any other useful constant i think is.

    In there to a large number of decimal places or what i could do is show for example the square root of a number done this quite a few times previously.

    In the earlier part of these tutorial series so i could show the square root of 2 for example which is 1.414 if you want to know what's available the.

    Easiest thing to do is to have a look at the list shown in alphabetical order so most of these are familiar from people who did maths or maybe further.

    Maths a level in england i don't know what the equivalent is in other countries i'm afraid so i can see things like um hyperbolic tangent what a function that.

    Is and most of these you'll only ever need when you're doing specific mathematical things and there's a few exceptions i guess one other one you might want to do.

    Is show the factorial of a number factorials get very big very quickly so i'll choose a fairly small number there and that will show me uh the factorial of 10 which is often written i believe.

    As 10 shriek so those are the main mathematical functions that you can use in the final part of this very short tutorial we'll look at checking and.

    Converting number types let's start with converting them so there's two main functions you can use to convert numbers which are int and float because.

    They're the two main types of number so for example i could set result equal to and i could take a pi which is 3.14159 etc.

    And i could convert that to an integer and to do that because i'm using something in the math module i need to import it first so if i run that you'll see it gives me.

    Three it rounds down there's actually a floor function in python which does the same thing or i could do it the other way around so i could take.

    Result and set it equal to the floating point equivalent of the number 42 and that would give me 42.0 so you can always turn a floating point.

    Number into an integer or an integer into a floating point number although you may well lose precision as a result one other thing useful thing you can often do is to check the data type.

    So for example if i want to know whether pi is a floating point number which i reckon it is i could use the is instance now this is a general function i'm just showing it here to do with numbers.

    So i could type pi in as a first argument and i could test whether pi is a float or an int in this case i'm going to say is it an int or not and i reckon this should return.

    A yes or true because pi is definitely a floating point number if on the other hand i said is it an integer.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continue to make videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=MYhjwBBcF-I
Previous Post Next Post