Converting NUMBERS

Understanding how to change data types to suit your need

Converting NUMBERS

In Python, there are two number data types: integers and floating-point numbers or floats. Sometimes you are working on someone else’s code and will need to convert an integer to a float or vice versa, or you may find that you have been using an integer when what you really need is a float. Python has built-in methods to allow you to easily convert integers to floats and floats to integers.

Introduction

Python’s method float() will convert integers to floats. To use this function, add an integer inside of the parentheses:

jesse = {'username': 'JOctopus', 'online': False, 'points': 723, 'followers': 481}

jesse.clear()

print(jesse)

In this case, 57 will be converted to 57.0.

You can also use this with a variable. Let’s declare f as equal to 57, and then print out the new float:

f = 57
print(float(f))
Show Output
57.0

By using the float() function, we can convert integers to floats.

Converting Integers to Floats

Python also has a built-in function to convert floats to integers: int().

The int() function works similarly to the float() function: you can add a floating-point number inside of the parentheses to convert it to an integer:

int(390.8)

In this case, 390.8 will be converted to 390.

You can also use this with variables. Let’s declare b as equal to 125.0, and c as equal to 390.8, then print out the new floats:

b = 125.0
c = 390.8

print(int(b))
print(int(c))
Show Output
125
390

When converting floats to integers with the int() function, Python cuts off the decimal and remaining numbers of a float to create an integer. Even though we may want to round 390.8 up to 391, Python will not do this through the int() function.

Converting Floats to Integers

In Python 3, relevant quotients are converted from integers to floats when doing division. That is, when you divide 5 by 2, in Python 3 you will get a float for an answer (2.5):

a = 5 / 2
print(a)
Show Output
2.5
Numbers Converted Through Division

Write a program to demonstrate converting an Integer to a Float

Write a program to demonstrate converting an Integer to a Float and a Float to an Integer.

Write a program to demonstrate converting an Integer to a Float, a Float to an Integer and conversion by division.

7.1 Converting Numbers Task
Submit your completed task...
If you need to get in touch with Mr McG then this is the way….

What's your name?
image/svg+xml
Thank you [name], that's great.

Can I ask where you are from?
image/svg+xml
Hey [name], can I ask where you are from?
image/svg+xml
Hey [name], which class are you in?
image/svg+xml
OK [name], thanks for that.

I will need your email address so I can get back to you!!
image/svg+xml
Thanks [name], nearly there.

What can I help you with?
image/svg+xml

Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.

image/svg+xml
Thank you [name], Mr McG got your message.
Thanks [name], Mr McG will get back to you as soon as he can!!

Have a great day!!

K McGuinness - 2018

No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.