From the course: Python Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Other types of numbers

Other types of numbers - Python Tutorial

From the course: Python Essential Training

Other types of numbers

- [Instructor] All right, we're going to get to the decimal class in a second. But first I just want to show you a couple neat things you can do with the int class in Python. So if I pass in a number as a string, it will convert it. We're casting this string 100 to the integer 100. Now a cool trick with this class is that if I optionally pass as a second argument, I can pass a number, it will take the second argument as the base that the number should be converted from. So 100 in base two is four in base 10. And this first argument has to be a string. If I try to do 100, two like that, it throws an error. So why is that? This is because when you're converting from one base to base 10 you might have things in the string that aren't numbers. So 1ab is perfectly valid in base 16 and that's apparently 427. But obviously, you know, 1ab is not an integer. So we have to have a string as the first argument if we want to convert it…

Contents