 # int() function in Python

## int() function in Python

Python int() function returns an integer from a given object or converts a number in a given base to decimal.

### Python int() Syntax :

int(string, base)

### Python int() Parameter :

• string : consists of 1’s and 0’s
• base : (integer value) base of the number.

### Python int() Returns :

Returns an integer value, which is equivalent of binary string in the given base.

### Python int() Exception and Errors :

TypeError : Returns TypeError when any data type other than string or integer is passed in its equivalent position.

## Python int() method example

### Example 1: Working with int() function in Python

 `# Python3 program for implementation` `# of int() function` `num ``=` `13` `String ``=` `'187'` `# Stores the result value of` `# binary "187" and num addition` `result_1 ``=` `int``(String) ``+` `num` `print``(``"int('187') + 13 = "``, result_1, ``"\n"``)` `# Example_2` `str` `=` `'100'` `print``(``"int('100') with base 2 = "``, ``int``(``str``, ``2``))` `print``(``"int('100') with base 4 = "``, ``int``(``str``, ``4``))` `print``(``"int('100') with base 8 = "``, ``int``(``str``, ``8``))` `print``(``"int('100') with base 16 = "``, ``int``(``str``, ``16``))`

Output :

```int('187') + 13 =  200

int('100') with base 2 =  4
int('100') with base 4 =  16
int('100') with base 8 =  64
int('100') with base 16 =  256```

### Example 2: Convert binary string to Python int base

 `# Python3 program for implementation` `# of int() function` `# "111" taken as the binary string` `binaryString ``=` `"111"` `# Stores the equivalent decimal` `# value of binary "111"` `Decimal ``=` `int``(binaryString, ``2``)` `print``(``"Decimal equivalent of binary 111 is"``, Decimal)` `# "101" taken as the octal string` `octalString ``=` `"101"` `# Stores the equivalent decimal` `# value of binary "101"` `Octal ``=` `int``(octalString, ``8``)` `print``(``"Decimal equivalent of octal 101 is"``, Octal)`

Output :

```Decimal equivalent of binary 111 is 7
Decimal equivalent of octal 101 is 65```

### Example 3: Program to demonstrate the TypeError

 `# Python3 program to demonstrate` `# error of int() function` `# when the binary number is not` `# stored in as string` `binaryString ``=` `111` `# it returns an error for passing an` `# integer in place of string` `decimal ``=` `int``(binaryString, ``2``)` `print``(decimal)`

Output :

`TypeError: int() can't convert non-string with explicit base`

### Example 4: Python int() Exception

 `try``:` `    ``var ``=` `"Geeks"` `    ``print``(``int``(var))` `except` `ValueError as e:` `    ``print``(e)`

Output:

invalid literal for int() with base 10: ‘Geeks’

### Application :

It is used in all the standard conversions. For example conversion of binary to decimal, octal to decimal, hexadecimal to decimal.

Last Updated on October 29, 2021 by admin

## Create a new column in Pandas DataFrame based on the existing columnsCreate a new column in Pandas DataFrame based on the existing columns

Create a new column in Pandas DataFrame based on the existing columns While working with data in Pandas, we perform a vast array of operations on the data to get the data in the desired form. One of these operations

## Python – Itertools.Permutations()Python – Itertools.Permutations()

Python – Itertools.Permutations() Itertool is a module provided by Python for creating iterators for efficient looping. It also provides various features or functions that work with iterators to produce complex iterators and help us to solve problems easily and efficiently in

## DataFrame.to_excel() method in PandasDataFrame.to_excel() method in Pandas

DataFrame.to_excel() method in Pandas The to_excel() method is used to export the DataFrame to the excel file.  To write a single object to the excel file, we have to specify the target file name. If we want to write to

## Python – Itertools Combinations() functionPython – Itertools Combinations() function

Python – Itertools Combinations() function Itertool is a module of Python which is used to creation of iterators which helps us in efficient looping in terms of space as well as time. This module helps us to solve complex problems easily

## str() vs repr() in Pythonstr() vs repr() in Python

str() vs repr() in Python str() and repr() both are used to get a string representation of object. Example of str(): s = 'Hello, Geeks.' print (str(s)) print (str(2.0/11.0)) Output: Hello, Geeks. 0.181818181818 . Example of repr(): s = 'Hello,

## Python filter() methodPython filter() method

filter() in python The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not. syntax: filter(function, sequence) Parameters: function: function that tests if each element of

## How to create DataFrame from dictionary in Python-Pandas?How to create DataFrame from dictionary in Python-Pandas?

Let’s discuss how to create DataFrame from dictionary in Pandas. There are multiple ways to do this task. Method 1: Create DataFrame from Dictionary using default Constructor of pandas.Dataframe class. Code: # import pandas library import pandas as pd