Python int() Method with Examples

In the previous article, we have discussed Python id() Method with Examples
int() Method in Python:

The int() function converts the given value to an integer number.

Syntax:

int(value, base)

Parameter Values:

value: It is a number or string that can be converted into an integer number.

base: The number format is represented by a number. 10 is the default value.

Return Value:

The int() method gives:

  • An integer object created from a given number or string uses the default base of 10.
  • (No parameters) returns 0
  • (If base is specified) treats the string in the specified base (0, 2, 8, 10, 16)

Examples:

Example1:

Input:

Given Number = 35.5

Output:

The given number's{ 35.5 } Integer number =  35

Example2:

Input:

Given Value = 'D'
Given base = 16

Output:

The given value's { D } Integer number with given base{ 16 } = 13

int() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the int() function to get the given number’s Integer number.
  • Store it in another variable.
  • Print the given number’s Integer Number.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 35.5
# Pass the given number as an argument to the int() function to get the
# the given numbers's Integer number.
# Store it in another variable.
rslt = int(gvn_numbr)
# Print the given number's Integer Number.
print("The given number's{", gvn_numbr,
      "} Integer number = ", rslt)

Output:

The given number's{ 35.5 } Integer number =  35
How does int() work with decimal, octal, and hexadecimal values?

Approach:

  • Give the value as static input and store it in a variable.
  • Give the base as static input and store it in another variable.
  • Pass the given value, base as the arguments to the int() function to get the Integer number of a given value.
  • Store it in another variable.
  • Print the Integer number of a given value.
  • The Exit of the program.

Below is the implementation:

# Give the value as static input and store it in a variable.
gvn_valu = '16'
# Give the base as static input and store it in another variable.
gvn_base = 8
# Pass the given value, base as the arguments to the int() function to get the
# Integer number of a given value.
# Store it in another variable.
rslt = int(gvn_valu, gvn_base)
# Print the Integer number of a given value.
print("The given value's {", gvn_valu,
      "} Integer number = ", rslt)

Output:

The given value's { 16 } Integer number =  14

Similarly, check it out for other values

gvn_valu = 'D'
gvn_base = 16
rslt = int(gvn_valu, gvn_base)
print("The given value's {", gvn_valu,
      "} Integer number = ", rslt)

Output:

The given value's { D } Integer number =  13

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the float(input()) function and store it in a variable.
  • Pass the given number as an argument to the int() function to get the given number’s Integer number.
  • Store it in another variable.
  • Print the given number’s Integer Number.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using the float(input()) function and store it in a variable.
gvn_numbr = float(input("Enter some random number = "))
# Pass the given number as an argument to the int() function to get the
# the given numbers's Integer number.
# Store it in another variable.
rslt = int(gvn_numbr)
# Print the given number's Integer Number.
print("The given number's{", gvn_numbr,
      "} Integer number = ", rslt)

Output:

Enter some random number = 250
The given number's{ 250.0 } Integer number = 250
How does int() work with decimal, octal, and hexadecimal values?

Approach:

  • Give the value as user input using the input() function and store it in a variable.
  • Give the base as user input using the int(input()) function and store it in another variable.
  • Pass the given value, base as the arguments to the int() function to get the Integer number of a given value.
  • Store it in another variable.
  • Print the Integer number of a given value.
  • The Exit of the program.

Below is the implementation:

# Give the value as user input using the input() function and store it in a variable.
gvn_valu = input("Enter some random value = ")
# Give the base as user input using the int(input()) function and store it in another variable.
gvn_base = int(input("Enter some random number = "))
# Pass the given value, base as the arguments to the int() function to get the
# Integer number of a given value.
# Store it in another variable.
rslt = int(gvn_valu, gvn_base)
# Print the Integer number of a given value.
print("The given value's {", gvn_valu,
      "} Integer number = ", rslt)

Output:

Enter some random value = B
Enter some random number = 16
The given value's { B } Integer number = 11

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.