cant multiply sequence by non-int of type float
This is a topic that many people are looking for. https://granthamandira.org/ is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, https://granthamandira.org/ would like to introduce to you Python Tutorial for Beginners 3: Integers and Floats – Working with Numeric Data. Following along are instructions in the video below:
hey there hows it going everybody in this video well be learning how to work with numeric data in Python and numbers are most commonly represented with integers and floats and the difference between an integer and a float is that an integer is a whole number and a float as a decimal so to see an example of this lets create a variable called num and lets just set this equal to 3 now python has a built-in function called type where we can see the data type of an object so if we print out the type of num and run this and we can see that it returns that that is of the class integer now if we were to set this number instead to 3.14 and now rerun this and we can see that now the type of number is a float so thats the main difference between an integer and a float now when working with numeric data its common that youll need to use some basic arithmetic so let me grab some comments from my snippets here just so that we have a reference for everything that we can do so Ill copy these arithmetic operators and paste these in and now lets go ahead and just run through each of these so the first four weve likely already seen a lot and are familiar with so for example addition we if we print it out 3 plus 2 and we can expect that equal to 5 if we print out 3 minus 2 that should be 1/3 times 2 should be equal to 6 and 3 divided by 2 should be equal to 1.5 now for division to behave this way this is actually new in Python 3 if youre running Python 2 then this will actually equal 1 because it drops the decimal but in Python 3 that gives us the right answer of 1.5 now if we dont want to drop that decimal then we do have a floor division and for division can be performed by adding 2 division signs so if I run this and now we can see it drops that decimal and its equal to 1 so if you ever see these 2 division signs then that is this floor division now if you wanted to work with exponents and powers and we can use these 2 multiplication signs so if we wanted to print 3 to the second power then we could just say 3 with these two multiplication signs if I print that then we can see that that equals nine because three squared is equal to nine now this last operator here is called a modulo operator and it gives us the remainder after a division so three mod two will have a remainder of 1 because 2 goes into 3 once with one left over so if we say 3 mod 2 and run that we can see that that is equal to 1 now a common use case for this is to tell if a number is even or odd now the reason for this is because every time you divide a number by 2 there are only two possible remainders its either going to be 0 or 1 so for example if we look at a few more examples here so let me just print out a few more module operators and
Ill do 2 mod 2 3 mod 2 4 mod 2 and 5 mod 2 so if we run this then we can see that 2 goes into 2 once with no remainders thats why we get a 0 2 goes into 3 once with 1 as a remainder so 2 goes into 4 twice with no remainder and 2 goes into 5 twice with 1 as a remainder so we can see from this pattern that if you do a mod 2 on any number and there is no remainder and that number is even if you do a mod 2 on any number and the remainder is 1 and that number is odd and thats a pretty common check that youll use a lot throughout Python programming okay so now lets look at the order of operations just like we would expect we can also use parentheses to change the order of operations just like with normal arithmetic so for example if we were to say lets see 3 times 2 plus 1 and with the normal order of operations we would multiply 3 & 2 which would give us 6 and then we would add 1 which would give us 7 so if we run that then we can see that we got 7 as our answer but if I put a parentheses here around this 2 plus 1 and now with normal arithmetic the way that this would work is that it should first add up these numbers in the parentheses which should give us 3 and then 3 times 3 should give 9 so now if we run this then we can see that we got 9 so the order of operations does work correctly within Python like we would expect okay so now lets look at another common operation that youll see a lot and that is incrementing a variable so if I make a variable here called num and I set this equal to 1 then what are some ways that we can increment that value by one well one way that we could do this is to say that num is equal to num plus 1 and if we print out that num that we can see that it did increment it by 1 and now its equal to 2 but incrementing values is such a common operation that there is a shorthand for this so instead we can just say num plus equals 1 so if we run that that we can see that its still incremented at value up to 2 and you can use this syntax with the other operations as well so instead if we were to say num x equals 10 and ran this then we can see that we got 10 because it took our original number and multiplied 1 by 10 okay so a couple more things here we also have some built-in functions available to us to work with numbers and one of these is abs for absolute value and basically this will just remove the sign for many negative numbers so if I took the absolute value of negative 3 and Ill just clean up a couple lines there okay so if we were to print out the absolute value of negative 3 and run that and we can see down here that we just got the absolute value which is 3 now another
built-in function that we have is round and by default this will round our values to the nearest integer value so if we said print the round of 3 point 7 5 and run that and we can see that 3 point 7 5 rounded up to 4 and we can also pass a second argument into our round function that tells it how many digits that we want to round to so if I put in a comma here and pass in a 1 as a second argument and now run this and what were saying is that we want to round to the first digit after the decimal we can see that that rounded 23.8 okay so another common thing that you need to do when working with numbers is to use comparisons now we want to know if two values are equal greater than less than and all of that so to test this we can use comparison operators and I have some comments over here in my snippets with the comparison operators as well and Im just going to paste over the arithmetic operators that weve already gone over and paste those in now these comparisons are going to return boolean switch our true/false values well be learning more about boolean in a future video when we go over conditionals but well see them here for the first time so lets say I have two variables here and well just call these two variables num1 and set that equal to three and well do num2 is equal to two so now lets run through all of these comparisons so first lets say that we wanted to check if these two variables were equal so I could say num1 and you can see up here that the equals comparison is double equals now you dont want to use the single equals because the single equals is this assignment here so the double equals is comparison a single equals as assignment so we want to compare his number 1 equal to num2 and if we run that and we can see that it returns false because those two values are not equal now if I wanted to check if they werent equal then I could use the exclamation point before the equal sign here exclamation point equals and if we run that that we can see that we got true because these two values are not equal now I can check if num 1 is greater than num2 by using the greater than sign so and save that and run it and we can see that we got true because num1 is 3 number 2 is 2 so 3 is greater than 2 and if you wanted to check less than then you can just use the less than sign so if we run that you can see we got false and you can also use the equal signs with these as well so if I want to check if this was greater than or equal to and we could run that we can see that its true and if we want to check less than or equal to then we can use those as well and if we print that we can see that we got false ok so now were going to look at one more thing Im just going to delete these in order to get
room now I will have these comments up on my github page if you want to reference to the arithmetic operators and the comparisons that we just looked at okay so in the last video we learned about strings now its possible that you have something that looks like a number but it could actually be a string so lets look at a problem that we can run into if thats the case and then well see how to solve it so lets say that you have some variables that look like numbers so maybe we read these in from a text file or got them from a website or something like that so just to give an example lets recreate our number one but this time were going to set these equal and single quotes here well set this to 100 and num2 well set equal to inside single quotes 200 so I explicitly set these equal to strings so its obvious to us that theyre strings but it might not be so obvious to us if we got these values from somewhere else so now lets say that we want to add these values together so if I was to say print num1 plus num2 and if we run this then we can see that this isnt the result that we thought it would be now if you remember from our string video when we add strings together it just concatenates those together so this is what we would expect with strings but with numbers we would expect this to be 300 so in order to turn these into integers were going to have to do something called casting and casting is super easy and Python so to cast these values from string to integers then well just add a couple lines here and I will copy these and well just say that num1 equals int num1 so we just casted that to an integer and we can do the same thing here with num2 so now if we save that and run it and we can see that we got three hundred so if you have an integer thats actually a string and you want to cast that to an integer then you can wrap that string in this int function or this int class here to create an integer okay so I think that is going to do it for this video I hope that now you feel comfortable working with integers and floating point values and in the next video well be learning about lists sets and tuples which basically allow us to hold sequences of data and is extremely useful to learn how to use properly but if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and Ill do my best to answer those if you enjoy these tutorials and would like to support them there are several ways you can do that the easiest ways to simply like the video and give it a thumbs up and also its a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and theres a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching you
Python, Python Ints, Python Floats, Integers, Python Integers, Python math, Python arithmetic, Python for Beginners, Absolute Beginners, Python for Absolute …
Thank you for watching all the articles on the topic Python Tutorial for Beginners 3: Integers and Floats – Working with Numeric Data. All shares of https://granthamandira.org/ are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.