Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Functions: Type Conversion

Give Feedback

00:00 Type Conversion.

00:05 This section will cover converting between different types of data, the first of which is text and relies on the American Standard Code for Information Interchange, or ASCII for short. In recent years, Unicode has become more common as it represents many more characters and is the standard in Python 3, but if you need to use the ascii() function, you can pass it some text and it will tell you what can be represented in ASCII.

00:36 In this case, the text can be represented in ASCII because all the characters are there, but if we pass it some Unicode text with a different character set as you can see onscreen here, it escapes them in a similar way as we’ve seen earlier and gives us character codes that represent those.

00:53 This can be useful if you have a system which can only represent character codes that are in ASCII, but it’s still possible to store these characters in an ASCII format that would then translate back to Unicode.

01:07 The next one is chr().

01:12 And as we can see, that returns a Unicode string of one character. So, the ordinal is put in. In this case, 97 equates to a lowercase 'a', 98 equates to a lowercase 'b', 65 equates to a capital 'A'.

01:33 You can look that up on a table for ASCII, such as the one which is onscreen right now. Here you can see the 128 defined characters in the ASCII alphabet.

01:45 This table has been presented in groups of 8, and the reason for that is there’s some repetition in this table which isn’t immediately apparent if it’s presented in columns of 10, et cetera.

01:57 So for instance, capital A is 64 + 1, which is 65 and lowercase a is 96 + 1, which is 97. You can probably see how the two alphabets mirror each others’ layouts, and how from 32 up to 63 there are numbers and punctuation characters, and from 0 to 31 there are a set of control characters which aren’t commonly used anymore but are implemented in some systems. Now, there isn’t a table for the Unicode character set because it’s much larger—it’s got millions of characters in, so that would take many screens.

02:34 But you can look them up online and print them out using the chr() function. They’re often defined with hex entries. So in this case, we’re going to enter a hex number of 0x and then 06a4.

02:53 And we can see that equates to the character you see on the ASCII line. So chr() works for both ASCII and Unicode characters, as you would expect.

03:05 chr() has a complimentary function ord(), where you can pass it a single character—such as the 'a' that we’ll see here—and it will tell you what the ASCII or Unicode point for that is, remembering that ASCII codes are the same as Unicode below 128.

03:25 So here, you can see capital 'A' is 65. And if we use the character we’ve seen there,

03:33 paste that in,

03:38 you can see we’ve got 1489.

03:46 Another function you’ve already seen is type(). type() can be fed any object, whether it’s an explicit declaration or a variable, and it will tell you what type that is, such as str (string), int, or float,

04:04 or bool, or even a list, which we’ll see later on in this series.

04:13 Generally, this is useful for when exploring variables and objects, but it’s probably not something you should use typically in your code. There are a number of conversion functions that we’ve already seen earlier on in this series, but we’re just going to take a quick recap on those as they’re so useful.

04:32 If you want to make something into an int, you can use int(). In this case, the string '1234' becomes the int 1234.

04:44 And it can be done with different bases. So in this case, int('1234'), where base=16so this is a hexadecimal '1234'—equates to the decimal 4660. So this is useful for base conversion.

05:03 If we do int('1010', base=2), we’ve put in the binary number '1010', which equates to 10 because we’ve got an 8 there and a 2 there, and those two add up to 10.

05:19 We’ve also seen bin(), oct(), and hex(). If we do this, taking bin(10), we get the binary representation of 10. If we do oct(10), we get the octal representation of 10.

05:34 So we’ve got 1 eight and 2 units. And if we do the hex(10), we get '0x' to show it’s hexadecimal, and 'a'. And 'a' is a single digit which represents 10 in hexadecimal.

05:47 We can also convert to float using the float() function, here passing it an integer which becomes converted into a float. We can convert to complex.

05:58 In a complex number, if we pass it just a single int, we get 4 real numbers and 0 on the imaginary plane, but you can also define it in the normal way, using 4+3j in this case, and there you can see your complex number.

06:16 We can convert other data types to strings if needed for printing, et cetera. str() of this int 1234 is the string '1234'.

06:26 In Booleans, anything which isn’t 0 will become True, so bool(0) is False, but bool(1) is True, as we’ve seen, but a bool(1234) is also True.

06:42 It’s important to remember that anything that isn’t either 0 or False will become True. And that’s a summary of the type conversions which are available in Python.

kiran on July 19, 2020

can you provide your slides in PDF format?

Darren Jones RP Team on July 21, 2020

I’ll get them uploaded for you!

kiran on July 21, 2020

Thank you! @Darren Jones. once done let me know.

Become a Member to join the conversation.