Python int() Function

Python int() function is used to get the integer value. It returns an expression converted into an integer number. If the argument is a floating point, the conversion truncates the number. If the argument is outside the integer range, It converts the number into long type.

If the number is not a number or if a base is given, the number must be a string.

Signature

Parameters

x : A number which is to be converted into integer type.

base: It is an Optional argument if used number must be a string.

Return

It returns an integer value.

Let's see some examples of int() function to understand it's functionality.

Python int() Function Example 1

It is a simple python example which converts float and string values into an integer type. The float value is truncated by the function and returned an integer instead.

Output:

integer values : 10 10 10

Python int() Function Example 2

To verify the type of returned value, we can use type function. The type function returns the type of value. See an example below.

Output:

<class 'int'> <class 'float'> <class 'str'>
values after conversion  10 10 10
and types are: 
  <class 'int'> <class 'int'> <class 'int'>

Python int() Function Example 3

Output:

Values after conversion: 2 175 8
and types are: 
  <class 'int'> <class 'int'> <class 'int'>





Latest Courses