create account

Python Programming Tutorials - Number Type Conversion by alucard14

View this thread on: hive.blogpeakd.comecency.com
· @alucard14 ·
$0.04
Python Programming Tutorials - Number Type Conversion
Hello welcome to antother Python tutorial and weโ€™re going to talk about converting number types. You know we got integers and floating point numbers and what happens if we want to convert them well there's a built-in function or actually two building functions for that.

So the first thing I want to do is we discussed this in brief earlier in a previous tutorial how to get the type that a certain object is.

And we do that
```
>>> type(6)
<class 'int'>
>>> 
```

Now we want to know what the type of object numbers are sixes, so get returned is gonna say classes and interger, so it's an integer. Let's try this again.
```
>>> type(6.0)
<class 'float'>
>>> 
```

We'll see what that object is it's a floating-point number. So type gives us the ability to know what type of object it is, so we could also do something like
```
>>> type({})
<class 'dict'>
>>> 
```
and it'll tell us dict for dictionary. 

```
>>> type(True)
<class 'bool'>
>>> 
```

We could do type let's do true return it's a boolean. So this gives us the ability to check the types but it doesn't give us an ability to change them. So to convert that to a say we ask a user for a number say how many times did you go to the store last week and they put in three, well it's gonna come back as a string but we want it as an integer so we can do some math with it. So let's say we want to convert a string to an integer so we go
```
>>> int("3")
3
>>> 
```
and we'll say the user gave us a string of three we close out the parentheses we return it returns an integer, pretty cool right.

So what happens if the user gives us a floating-point number or six point seven or our programming business a floating-point number
```
>>> int(6.7)
6
>>> 
```
we use int to get that number convert it to an integer now it always rounds down so if we put int it's gonna round down to six.

Now say well I wanted to round up
```
>>> round(6.7)
7
>>> 
```
You can use round and do six point seven it close it out and you're gonna get seven alright that's another way to work around it it's gonna give you a seven as an integer. 
```
>>> int(round(6.7))
7
>>> 
```
So that's one way to do it or you could actually do in switch I don't know why you would do it this way but just for knowledge sake you can actually do it this way you know return seven still okay.

Now say let's see how about converting it to a floating point number, so say the user gave us three again 
>>> float("3")
3.0
>>> 

so we do float and it's in a string three and we want it in a floating point number so we get we use float built-in function and it converts it to a three.

What about an integer
```
>>> float(6)
6.0
>>> 
```
Just float six itโ€™s gonna return six point zero, alright. So float converts the number or the string to a floating point number. Int converts a string or a floating point number to an integer.

Now what happens if the user gives us a floating point number in the string and we want to convert it to an integer
```
>>> int("7.5")
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    int("7.5")
ValueError: invalid literal for int() with base 10: '7.5'
>>> 
```

so if we do this is not gonna work. Now we can't convert a string that contains a floating point number, it's not a floating point number but say that wakes it looks like a floating point number and we convert them to a floating point number. This is not this is a string object okay, so if we go int 7.5 we're gonna get an error it says invalid literal for int with base 10. 

So basically saying we need in the string we need it to be an integer and that's the only way you can convert.

Well what happens if I want convert my users input to an integer, so let's say the user gave us the floating-point number 7.5 first thing we're gonna do is
```
>>> int(float("7.5"))
7
>>> 
```
what we're doing here is we're gonna convert it to a floating-point number. Which is gonna give 7.5 then we're gonna convert it to an integer so we hit return there we go alright, or we could have used round to get the floating-point number if we don't give rounded argument
```
>>> round(float("7.5"))
8
>>> 
```
this way it return we get eight. So here we go into all these roles rounds down the 7th and 8th all these rounds up. so here's proof that int gonna always round down and round will round the proper way it's supposed to unless the fraction doesn't meet up like we've talked about a million times before. 

Alright so uh anything else I want to show you oh yeah when we go back to int here I just remember I said this is a string not a floating-point number let's just take a look at that real quick
```
>>> type("7.5")
<class 'str'>
>>> 
```
it's a string alright, it's based of whatever is indicated on the outside. So like I said earlier I said this was a string
![](https://steemitimages.com/DQmcWvYp8VWnKyEyGrcHK12UpsxL7FrbR8xVvTKfEohkEXN/image.png)
it is actually string.

Now what we're doing here 
![](https://steemitimages.com/DQmZGxdrvTwX94KggtKJeLSzSx8W79oi72evZ5VMVHXbNNi/image.png)
We're converting it to a floating-point number because float can handle a string that looks like this all right int cannot. So we have to convert it to a float which is 7.5 and then convert it to an integer which is 7 alright and it always your on sale and it you're always rounds down.

If you guys have any questions leave a comment on below.
๐Ÿ‘  , ,
properties (23)
authoralucard14
permlinkpython-programming-tutorials-number-type-conversion
categorypython
json_metadata{"tags":["python","programming","tutorials","math","blog"],"image":["https://steemitimages.com/DQmcWvYp8VWnKyEyGrcHK12UpsxL7FrbR8xVvTKfEohkEXN/image.png","https://steemitimages.com/DQmZGxdrvTwX94KggtKJeLSzSx8W79oi72evZ5VMVHXbNNi/image.png"],"app":"steemit/0.1","format":"markdown"}
created2018-03-19 16:47:12
last_update2018-03-19 16:47:12
depth0
children1
last_payout2018-03-26 16:47:12
cashout_time1969-12-31 23:59:59
total_payout_value0.036 HBD
curator_payout_value0.003 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,381
author_reputation3,618,561,059
root_title"Python Programming Tutorials - Number Type Conversion"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id45,377,631
net_rshares12,637,655,591
author_curate_reward""
vote details (3)
@trickbrown ·
You should throw in some examples of negative numbers. Depending on the programming language, negative numbers may round differently.

I've never programmed in Python, but I think it rounds everything toward negative infinity, if memory serves, but no doubt, you would know better than I.  :)
๐Ÿ‘  
properties (23)
authortrickbrown
permlinkre-alucard14-python-programming-tutorials-number-type-conversion-20180324t201148070z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-03-24 20:11:48
last_update2018-03-24 20:11:48
depth1
children0
last_payout2018-03-31 20:11:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length292
author_reputation143,590,196,758
root_title"Python Programming Tutorials - Number Type Conversion"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id46,388,171
net_rshares572,844,698
author_curate_reward""
vote details (1)