Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • TypeError: the first argument must be callable Python
  • Tableau Desktop versus Tableau Server data visualisation
  • how to create an instance of a class class
  • How to delete a key from a Python dictionary Python
  • How To Create An Empty Dictionary In Python python dictionaries
  • How To Pass Data Between Functions Python Functions
  • How Would You Change The Name Of a Key in a Python Dictionary Python
  • How to create a class in Python class

ValueError: invalid literal for int() with base 10

Posted on January 26, 2021May 23, 2021 By admin No Comments on ValueError: invalid literal for int() with base 10

Have you been working with the data type int, and getting the above error?

As a data analytics practitioner, this problem pops up quite frequently, so it is time to get to the bottom of it!

Here we explain the data type, why the error occurs and examples of how to fix it.

So what are INT and what are their properties?

INT or integers are whole numbers with are positive or negative. They cannot hold any decimal places.

Also INT can return an integer object from a number or string.

Any value that has a decimal place in it will be rounded down, and anything after the decimal place will be ignored.

For the below you can see the output is rounded down:

data_error = int(1.75)
print(data_error)
print (type(data_error))
output ===> 1
<class 'int'>

How can string inputs be managed?

Strings are read in, and the program will handle the string as an int, and then perfom its checks.

For example:

data_error = int('1')
print(data_error)
print (type(data_error))
Gives output of: 1
<class 'int'>

BUT

data_error = int('1.75')
print(data_error)
print (type(data_error))
Gives output ====> ValueError: invalid literal for int() with base 10: '1.75'

This is exactly the problem we are looking to resolve. When you read the value as an integer without any inverted commas (i.e. 1.75), it treats it as an integer.

On the other hand, when you include the inverted commas ( i.e. ‘1.75’), the program cannot manage the conversion from string and throws the error above.

For reference Base 10 represents all the integers from 0-9, which are whole numbers, in essence, ten possibilities.

It is used to assign numbers to an integer or string passed to an integer, so for example 3569 is:

3 has a place of 3000

5 has a place of 500

6 has a place of 60

and 9 has a place of 9.

Click here to see a full explanation of Base 10 , the error is used to signify that the output does not align with the Base 10 system.

How can I fix this problem when I encounter it in a program?

Returning to this problem below, the solutions that could be applied are as follows:

data_error = int('1.75')
print(data_error)
print (type(data_error))
Gives output ====> ValueError: invalid literal for int() with base 10: '1.75'

Fix1: Remove the inverted commas, giving you:

data_error = int(1.75)
print(data_error)
print (type(data_error))
Output ====> 1
<class 'int'>

Fix2: Only include integer values when passing to another object.

This will help with ensuring the controls are in place to reduce these errors occurring.

Also if you have to use values inside inverted commas , make sure they are of an integer type only, with no decimal places.

To wrap with conclusions

In summary, the best way to avoid this problem is to use integer values at all times, with no decimal places.

If you have to use a value with a decimal place then do not include it inside inverted commas so the computer program can read it correctly, drop everything after the decimal point and just return an integer.

On our YouTube channel you can subscribe to find out more information about ValueErrors and many more tips and tricks to help you with better data analytics.

We have created a video for this blog post, have a look at it, and it you like the channel please subscribe!

We have also got a fix here for value error pattern contains no capture groups

Value Error Tags:Base 10, int, integers, invalid literal, python invalid literal

Post navigation

Previous Post: how to compare two lists in Python
Next Post: how do I merge two dictionaries in Python?

Related Posts

  • ValueError: cannot convert float NaN to integer Null values
  • ValueError: Columns must be same length as key exception handling
  • ValueError: pattern contains no capture groups Value Error

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • python constructor self and __init__explained Python
  • How to sort a Python Dictionary Python
  • Deleting table records with SQL SQL
  • What are the reserved keywords in Python Definition
  • How to save data frame changes to a file Python Dataframe
  • TypeError: ‘float’ object is not callable Python
  • supervised machine learning vs unsupervised machine learning? machine learning
  • How to Add Formulas to Excel using Python numpy

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT