Methods and Functions

Python int()

The int() python is used to convert the specified string or number into an integer value. 

int() python

Source

The syntax for the int() in Python is as follows:

int(x=0, base=10)

int() Parameters

The int() python has two arguments as parameters. These two arguments are:

x- Convert a number or string to an integer object. The default value of the x parameter is zero. 

Base- The number format is represented by a number. The base is set to 10 by default. This argument is optional.

Return value from int()

The int() in python returns an integer value that is the binary string’s equivalent in the provided base.

Errors:

If any other data is entered besides an integer or a string datatype, the TypeError exception is raised. 

Example 1: How int() works in Python?

                    

# integer

print("int(123) is:", int(123))


# float

print("int(123.23) is:", int(123.23))



# string

print("int('123') is:", int('123'))


Output

int(123) is: 123

int(123.23) is: 123

int('123') is: 123

Example 2: How int() works for decimal, octal, and hexadecimal?

                    

# binary 0b or 0B

print("For 1010, int is:", int('1010', 2))

print("For 0b1010, int is:", int('0b1010', 2))



# octal 0o or 0O

print("For 12, int is:", int('12', 8))

print("For 0o12, int is:", int('0o12', 8))


# hexadecimal

print("For A, int is:", int('A', 16))

print("For 0xA, int is:", int('0xA', 16))

Output

For 1010, int is: 10

For 0b1010, int is: 10

For 12, int is: 10

For 0o12, int is: 10

For A, int is: 10

For 0xA, int is: 10

Example 3: int() for custom objects

                    

class Person:

    age = 23



    def __index__(self):

        return self.age

    

    def __int__(self):

        return self.age



person = Person()

print('int(person) is:', int(person))


Output

int(person) is: 23

What does int () do in Python?

The int() in Python, is used to convert a given number or a string to an integer number. 

Here’s an example to understand the implementation of the int()

Source code

                    

numb = 13

  

String1 = '187'

  

# Stores the result value of

# binary "187" and numb addition 

result1 = int(String1) + numb 

print("int('187') + 13 = ", result1, "\n")

  

# 2nd example



str2 = '100'

  

print("base of 2 for int('100')= ", int(str2, 2))

print("base of 4 for int('100')=", int(str2, 4))

print("base of 8 for int('100')=", int(str2, 8))

print("base of 16 for int('100')=", int(str2, 16))


Output :

int('187') + 13 = 200 



base of 2 for int('100') = 4

base of 4 for int('100') = 16

base of 8 for int('100') = 64

base of 16 for int('100') = 256

Does INT () round up or down python?

The INT() in Python is used to round down an integer. A positive number is round down towards the nearest zero and a negative number in python is round off away from the zero. 

For example:

 

INT(8.1) = 8

INT(-8.1) = -9

How do you use int in Python?

In Python and Python3, the int() method transforms an integer in a given base to decimal.

Here’s an example:

Source Code:

                    

binaryStr = "111"

  

# Stores the equivalent decimal 

# value of binary "111" 

Decimal = int(binaryStr, 2) 

  

print("The binary 111 decimal format is", Decimal) 

  

  

# "101" taken as the octal string

octalStr = "101"

  

# Stores the equivalent decimal 

# value of binary "101" 

Octal = int(octalSting, 8) 

  

print("The octal 101 decimal format is", Octal)



Output :

The binary 111 decimal format is 7

The octal 101 decimal format is 65

If the syntax of the int () is not written properly, a type error is raised.

For example:

                    

binaryStr1 = 111
  

# It throws an error if you use an integer instead of a string.

deci = int(binaryStr1, 2) 

  

print(decimal)



Output :

Traceback (most recent call last):

  File "/home/d87cec4c0c33aad3bb6187858b40b734.py", line 8, in 

    decimal = int(binaryString, 2)  



TypeError: int() cannot convert a non-string to a string with a certain base.

What is integer number in Python?

The python programming language has three numerical types: complex numbers, floating-point numbers, and integers. In Python, integers are classified into zero, positive, or negative whole numbers with no fractional part and infinite precision, such as 0, 100, -10, and so on. If there is a decimal, it is considered as a float variable. 

For example:

                    

The valid integers are:

 

>>> 0

0

>>> 1001

1001

>>> -10

10

>>> 1234

1234

>>> zy=50000000000000

50000000000000

The integers in python are also binary, hexadecimal, and octal values. 

>>>1100 #binary number

12

>>>110 #octal number

6

>>0x12 #hexadecimal number

18

The int class includes all integer literals and variables.

Example: Use the type method to know the class name

                    

>>>type(1001)

<class 'int'> #1001 is of class int



>>> x=156234567890

>>> type(x)

<class 'int'> #x is of class int



>>> y=500000000

>>> type(y) # y is of class int

<class 'int'>

The comma is not allowed as a number delimiter in Python. Instead, use the underscore as a delimiter.

If the numbers include a decimal point, then they would be considered as a float variable

For example:

 

                    

>>> a=7

>>> type(a)

<class 'int'>

>>> a=7.0

>>> type(a)

<class 'float'>

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.