Type Conversion: ================ Explicit Type Conversion: ========================= 1) int(): ========= -> it is used to convert a binary number into decimal. -> it is also used to convert an octal number into decimal. x = 0O123 print("Decimal for given Octal = ",int(x)) -> it is also used to convert an hexadecimal number into decimal. x = 0x123 y = 0XA1 print("Decimal for given Hexadecimal = ",int(x)) print("Decimal for given Hexadecimal = ",int(y)) -> it can be also used to convert a floating-point value into decimal/integer. -> int() can be used to convert Boolean value into decimal. -> if a string with number (decimal only), int() can convert into decimal. Note: ===== int() cannot convert the complex number into decimal int() cannot convert the string with alphabets into integer. x = 0.00001 y = 123.00123 z = True p = False q = "1234" # r = "123.234" # r = 12-23j # Float value conversion into integer print("Decimal = ",int(x)) print("Decimal = ",int(y)) # Boolean value conversion into an integer print("Decimal = ",int(z)) print("Decimal = ",int(p)) print(type(q)) print("Decimal = ",type(int(q))) # print(int(r)) =============================================== 2) float(): =========== Syntax: float(value) -> can use to convert any integer value into float. -> Boolean value also possible to convert into float. -> when the string is defined with decimal number or floating-point number, then float() can convert into float. Note: ===== The complex number and string with alphabets are not possible to convert into float using float(). a = 0b11001 # binary b = 0o12 # octal c = 123 # decimal d = 0x21 # hexadecimal e = True f = False g = '123' h = '123.2345' # i = 12-23j print(float(a)) print(float(b)) print(float(c)) print(float(d)) print(float(e)) print(float(f)) print(float(g)) print(type(float(h))) # print(float(i)) ============================================================= 3) complex(): ============= Syntax: complex(value) -> any integer value can convert into complex number. -> floating-point number can convert into complex. -> Boolean value also possible to convert into complex. -> Any string without alphabets and special characters can possible to convert into complex. a = 0b1101 b = 120 c = 0o12 d = 0x21 e = 1.2e5 f = True g = False h = '1234' i = '12.234' j = '12-24j' # k = '0b11001' print(complex(a)) print(complex(b)) print(complex(c)) print(complex(d)) print(complex(e)) print(complex(f)) print(complex(g)) print(complex(h)) print(complex(i)) print(complex(j)) # print(complex(k)) ==================================================== 4) bool(): ========= Syntax: bool(value) -> Any value other than the zero, can be consider as "True" bool(10), bool(-10) ==> True bool(0),bool(0.0),bool(0-0j) ==> False bool('') ==> False bool('a') ==> True print(bool(10)) print(bool(-10)) print(bool(0)) print(bool(0.0)) print(bool(0-1j)) # True print(bool('')) print(bool('a')) =========================================== 5) str(): ========= Syntax: str(value) -> any value can convert into string print(type(str(10))) print(type(str(0b11001))) print(type(str(12.234))) ================================================== input(): ======== Syntax: variable = input("some text") ==> input() can read any value in string format. # taking an integer value a = int(input("Enter some value:")) # taking a float value b = float(input("Enter some value:")) # reading of complex value c = complex(input("Enter some value:")) # reading of boolean value d = bool(input("Enter some value:")) print(type(a)) print(type(b)) print(type(c)) print(type(d)) ============================================ Taking of binary, octal and hexadecimal in run time: ==================================================== Syntax: identifier = int(input(),base) here: input() function by default can read a value in string base => differ from one base to another binary ==> base-2 octal ==> base-8 hexadecimal ==> base-16 var1 = int(input("text"),2) # binary var2 = int(input("text"),8) # octal var3 = int(input("text"),16) # hexadecimal a = int(input("Enter a binary value:"),2) b = int(input("Enter an octal value:"),8) c = int(input("Enter an hexadecimal value:"),16) # any base value want to convert int decimal, int() can be used. # print() can print any base value in decimal by default. print(a) print(b) print(c) Small Test Case Scenario for understanding of binary data usage =============================================================== # ASSUME WE ARE TESTING A SOFTWARE SYSTEM LIKE TELE COMMUNICATION SYSTEM THAT DEALS WITH CONTRL # FLAGS STORED AS BINARY NUMBERS (LIKE: 101010101). IN THIS CASE, WE NEED TO VERIFY IF A SPECIFIC # BIT IS SET OR CLEARED. def testFlag(binaryValue,bitPosition): return (binaryValue >> bitPosition) & 1 == 1 binaryValue = 1010101 bitPosition = 3 result = testFlag(binaryValue,bitPosition) if(result == True): print("Test Case Passed.") # given bit position is set else: print("Test Case Failed.") # given bit position is clear