Resources mentioned in this lesson:
Creating Numeric Data Types
00:00 In the previous lesson, I showed you some math-related built-in functions. This lesson is the beginning of a new section on data types, starting with the built-in functions that create numeric data.
00:11 Many of the built-in functions are for creating and manipulating data types. This is the first of several lessons that cover the different data type creators, starting with the numeric functions covering integers, binary, octal, hexadecimal, float, complex numbers, and Booleans.
00:31 Some of these functions are creators and some of them are converters. Potato, potato. You’ll understand what I mean when you see the examples. Off to the REPL to show you just what I mean.
00:43 Since you can just type an integer in, you might ask why there’s a function for integers. Its primary use is conversion. For example, if you got some input from the user on the command line, that has to be a string.
00:57
That’s how input works. But what if you want to convert that to an integer? Calling int() converts the argument into an integer if it’s possible.
01:04
It also works if you give it a float. But it won’t do both at the same time, though. You have to choose one or the other. If you call int() without an argument, you get back the default value of 0.
01:19
The int() function optionally takes an argument that specifies the base of the number being converted.
01:27
Here, I’ve passed in a string that contains a binary value, that’s the 0b prefix, and then I call the function with base=2. Binary 11 then gets converted to the decimal integer equivalent, which is 3.
01:45 There’s the same without the leading prefix.
01:50 Works for octal as well.
01:55
And hexadecimal, which is base 16. There are other built-in functions that help you go the other way. bin() converts its argument to a string representation of a binary number.
02:07
Notice the 0b prefix here. Of course, I can use the base=2 concept from before to convert it back.
02:17
To go along with bin(), there is also oct() for octal. And hex() for hexadecimal. Given these four functions, you can go back and forth between the most common representations of whole numbers in coding.
02:32
If you’re tired of whole numbers and want to get a little more real, see what I did there? Dad joke for the win. The float() function helps you create floating-point numbers.
02:42 If you pass it an integer, it converts it.
02:46
Same goes with strings containing integers. And strings containing floats. Like with int(), passing no argument in returns 0, but this time floating-point style.
02:59
When I demonstrated the absolute value function, I showed you that Python supports complex numbers. In that lesson, I used a string to create one. So now you see that complex() isn’t really special. It’s consistent with int(), hex(), float(), and the others.
03:16 With complex numbers, you can also pass in two arguments specifying the real and imaginary parts if you prefer. Here I’ve created the same result, but using those two integers instead of a string.
03:29 The components of complex numbers don’t have to be integers. They can be floating-point as well. You might argue that Booleans aren’t really numbers, but they are math-y, so I’m sticking with them here. In fact, I’ll tell you a secret.
03:45 Booleans in Python actually inherit from integers, but let’s just pass right over that. Like with other functions you’ve seen so far, you can convert things to Boolean data types using a built-in.
03:56
Python includes a Boolean type, but lots of programming languages just use 1 and 0, so you might need to convert. Calling with 0 gives False, while calling with 1 gives True.
04:08 Python has a concept of truthiness and will convert many different things to Boolean values. For the most part, empty things and zeros are false, while everything else is true.
04:22
42, is also True. An empty string is False, while a nonempty string
04:31
is True. Same goes for iterables.
04:39
And like with int() and float(), an argumentless call is 0-like, which in this case means False.
04:48 Take a look at this tutorial if you’d like to learn more about math with complex numbers and how to do that in Python. Or see these quick pieces for examples on Booleans and truthiness.
05:00 Next up, I’ll move from numbers to strings with even more data type creation functions.
Become a Member to join the conversation.