Back to Browse

#8 Python Playground: Type Conversion | Computer Programming & Coding for Kids & Beginners

4.6K views
Oct 22, 2020
3:56

Magic8 8-0 Liam Type Conversion in Python 8-1 Liam Hi, Emma! Emma Hi, Liam! Liam Do you remember the data types? Emma Sure! There are strings, integers, floating point and so on in data types. Figure Liam That’s right! What happens when you add the string "Hello" and the number 100? Figure Emma Do you mean adding strings and numbers? I can't calculate it in Python because the data types are different. Liam That’s right! We can't add up strings and numbers. So, can you add the number 15 to the string 100? Emma Because of the different data types, I can't actually add strings and numbers together. How can I calculate it? Liam You can calculate it by converting the character string 100 to the number 100. Emma How do I change the data type? 8-2 Liam Data Type Conversion in Python Liam When you want to convert the data, use the command to convert the data. Emma That's useful. What types of commands are available to convert data? Liam int function converts string to integer. float function converts string to floating point number. str function converts integer and floating point number to string. Emma We can use these commands to convert the data. Liam Exactly! First, let’s try adding the string "100" to the integer 15. Create a new file in IDLE and write the code like this. Save and then run it. Then an error is displayed. Emma It is not possible to calculate with string and integer. We need to convert the string to the integer before we can calculate it. Liam That’s right! So, we use int function to convert the string to an integer and then calculate it. Create a new file in IDLE and write the code like this. Save and then run it. test8-1.py a="100" print(int(a)+15) Output 115 Emma Now we've done the math. We were able to calculate it by converting the string type 100 to a number 100. Liam Exactly! 8-3 Liam How to check if it can be converted Emma Can we check if we can convert in advance to avoid errors? Liam We can prevent such errors by checking in advance when they are likely to occur. We can use the isdigit function to check if the variable can be converted to a number correctly. Figure Liam True is displayed when conversion is possible, and False is displayed when conversion is not possible. Emma If we check beforehand and convert only when True is displayed, there will be no error. Liam That’s right! Let's try it out and see if we can convert the strings "100" and “Hello!" Create a new file in IDLE and write the code like this. Save and then run it. test8-2.py a="100" b="Hello!" print(a.isdigit()) print(b.isdigit()) Output True False Emma It turns out that the string 100 can be converted to an integer and the string Hello! cannot be converted. Liam That’s right. 8-0 Emma In this lesson, we learned about data type conversions. Liam Well done!

Download

0 formats

No download links available.

#8 Python Playground: Type Conversion | Computer Programming & Coding for Kids & Beginners | NatokHD