int()

int('string','base') returns integer object
string: input String or number
base: (optional) base of the (input) number.
print(int('45'))  # 45
Using float data type as input
print(int(4.5))  # 4 
print(int(4.78)) # 4
print(int(4.99)) # 4
print(int(4.1))  # 4

With base

Base is 2 here
print(int('0101',2)) # 5
octal base
print(int('107',8)) # 71
Base is 16 here
print(int('ca',16)) # 202
print(int(123,16)) # Error
This will generate output More examples with different base value
my_data='1011' # string input
print("With base 2 : ",int(my_data,2)) # 11
my_data='71'
print("with base 8 :",int(my_data,8)) # 57
my_data='cd' 
print("with base 16 :",int(my_data,16)) # 205
Output
With base 2 :  11
with base 8 : 57
with base 16 : 205

None type to int

This will generate error as we can't convert None type variable to Integer.
a=None
b=int(a) #TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
print(b)
Solution , use like this and assign 0 if it is None
a=None
b=int(a or 0) 
print(b) # 0

All Built in Functions in Python bin() complex()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer