Datatypes in Pythons: ===================== 1) Primitive Datatypes ====================== fundamental datatypes in python are primitive datatypes. Numerical type Integer Floating-point Complex Non-numerical type Boolean Note: ===== Python ==> Object Oriented Programming Language That every datatype in python is a built-in datatype because for each datatype we have a pre-defined class in python library. Integer ==> int float-point ==> float complex ==> complex Boolean ==> bool x = 10 y = 12.23 type(x) ==> type(y) ==> High-level programming languages 1) Procedural Programming languages : C printf(), scanf(), putchar(), getchar(), puts(), gets(), malloc(), calloc() etc. 2) Object Based : VB Script 3) Object Oriented: C++, Python, Java etc. OOPs ==> Object Oriented Programming System: =========================================== Class Object Method Encapsulation Inheritance Polymorphism Abstraction etc. ========================================= Integer datatype: ================= 0, 1, 2, .. int ==> class for integer can allow to define in four ways: 1) Decimal form 2) binary form 3) octal form 4) hexadecimal form 1) Decimal form: ================ base-10 number system value can allow to define with 10 literals ==> 0 to 9 ==> can be either the +eve or -eve value ex: x = 9876, y = -123 2) binary form: =============== base-2 number system value can allow to define with only two literals ==> 0 and 1 ex: a = 110011001 ==> binary number can always prefix with ==> 0B/0b ex: a = 0b110001001001 3) octal form: ============== base-8 number system value can allow to define with 8-literals ==> 0 to 7 ex: a = 1276 # decimal ==> The Octal number can prefix with: 0o/0O ex: a = 0O1276 # octal 4) hexadecimal form: ==================== base-16 number system can allow to define the value with 16-literals 0 to 9 ==> 10-literals A/a to F/f ==> 6-literals ==> hexadecimal can always prefix with: 0X/0x ex: a = 0x12 a = 9876 b = -123 c = 110011001 d = 0B110011001 e = 12 f = 0O12 g = 0x12 print(a,type(a)) print(b,type(b)) print(c,type(c)) print(d,type(d)) print(e,type(e)) print(f,type(f)) print(g,type(g)) ===================================== bin(): ===== ==> a built-in method used to convert any integer number into binary. Syntax: bin(any integer value) a = 10 b = 0O21 c = 0Xaf print(a,bin(a)) print(b,bin(b)) print(c,bin(c)) ======================================== oct(): ===== ==> can use to convert any integer to octal Syntax: oct(any integer number) hex() ==== ==> can use to convert any integer to hexadecimal Syntax: hex(any integer) a = 100 b = 0b11001 c = 0o123 d = 0x321 # any number to decimal print(a) print(b) print(c) print(d) # any number to binary: print(bin(a)) print(bin(b)) print(bin(c)) print(bin(d)) # any number to octal print(oct(a)) print(oct(b)) print(oct(c)) print(oct(d)) # any number to hexadecimal print(hex(a)) print(hex(b)) print(hex(c)) print(hex(d)) ===================================== 1) decimal to binary ==================== decimal should divide with '2' until the quotient is reached to '0' ==> after that, arrange all the remainders from bottom to top. 2) decimal to octal =================== decimal should divide with '8' until the quotient is reached to '0' ==> after that, arrange all the remainders from bottom to top. 3) decimal to hexadecimal ========================== decimal should divide with '16' until the quotient is reached to '0' ==> after that, arrange all the remainders from bottom to top. 4) binary to decimal ==================== follow the number system from right to left each binary literal of given sequence multiplied with its coefficients (2 powers) and sum together. 5) octal to decimal =================== follow the number system from right to left each binary literal of given sequence multiplied with its coefficients (8 powers) and sum together. 6) hexadecimal to decimal ========================= follow the number system from right to left each binary literal of given sequence multiplied with its coefficients (16 powers) and sum together. 7) Octal to hexadecimal ======================= octal ===> binary in octal, that each octal literal can be defined with 3-bit binary in binary, the total sequence from right to left with 4-bit pair should divide and convert each pair into hexadecimal ================================================ Assignment: =========== decimal to binary, octal, hexadecimal ====================================== 1122, 109, 97, 79 binary to decimal ================= 0b11100011001 octal to hexadecimal ===================== 0o112233