B1_ Detailed explanation of common data types in Python

Keywords: Python

Data type classification

1. Classify according to the content of data expression

  1. Number type (number)
  2. String type (str)
  3. Boolean type (bool)
  4. List
  5. Tuple
  6. Dictionary (dict)
  7. Set

2. Classification according to variable and immutable

Immutable data type

  • What is an immutable data type?

    It means that the value in the memory specified by the data type object cannot be changed or modified in place
    In other words, for immutable data types, if you want to change the value of a variable or an object, you need to re create one
    Variable, the computer will reopen a memory space to store new values, and will not modify them in the original memory location

  • Immutable data types in Python

    • Number number
    • bool Boolean
    • String string
    • Tuple tuple

Variable data type

  • What is a variable data type?

The value in the memory space where the data type object is located can be changed, and the value can be modified in place
After the value (variable) of the object is changed, the corresponding value on the memory address it points to is directly changed without copying or opening up
New memory address to store the new value

  • Variable data types in Python
  • List list
  • Set set
  • Dict (Dictionary)

Detailed explanation of data type usage

Number type

1. Description of common Number types

  1. Integer (int)

For integers without decimal point, the integer type of Python 3 has no size limit and can be used as a long type. There is no longer a long type in Python 3

  1. Floating point

Floating point type consists of integer part and decimal part. Floating point type can also be expressed in scientific counting method (2.5e^2 = 2.5 * 10 ^ 2 = 250)

  1. Complex

The complex number is composed of real part and imaginary part, which can be represented by a+bj or complex(a,b). The real part a and imaginary part B of the complex number
All floating point types

  • Sample code
intNumber = 123  # int
print(type(intNumber), intNumber)
floatNumber = 123.456  # float
print(type(floatNumber), floatNumber)
complexNumber = 3 + 5j  # complex
print(type(complexNumber), complexNumber)
  • Operation results
<class 'int'> 123
<class 'float'> 123.456
<class 'complex'> (3+5j)
  • You can also use 0x or 0X, the latter 0o 0O to represent hexadecimal and octal data

  • Sample code

decNumber = 123
hexNumber = 0x123
octNumber = 0o123
print(type(decNumber),decNumber)
print(type(hexNumber),hexNumber)
print(type(octNumber),octNumber)
  • Operation results
<class 'int'> 123
<class 'int'> 291
<class 'int'> 83

2. Binary description and conversion

  1. b: Binary - > bin ()
  2. o: Octal - > OCT ()
  3. d: Decimal - > int ()
  4. h: Hex - > hex ()
  • Code example
numberDec = 100
numberBin = bin(numberDec) # switching sites
numberOct = oct(numberDec) # Convert to octal
numberHex = hex(numberDec) # Convert to hex
print("numberBin: {},type(numberBin): {}".format(numberBin, type(numberBin)))
print("numberOct: {},type(numberOct): {}".format(numberOct, type(numberOct)))
print("numberHex: {},type(numberHex): {}".format(numberHex, type(numberHex)))
  • Operation results
numberBin: 0b1100100,type(numberBin): <class 'str'>
numberOct: 0o144,type(numberOct): <class 'str'>
numberHex: 0x64,type(numberHex): <class 'str'>

Note: the return types of bin() oct() hex() are strings, not direct numbers or other types, but strings

  • Other base numbers are converted to decimal
# Notice the result here and know why str (numberbin)= "0b1100100"
# When calculating str(), it will first convert the string of 0b or 0x, which is 0o, into decimal before str()
number1 = int(str(numberBin),2)
number2 = int(str(numberOct),8)
number3 = int(str(numberHex),16)
print("number1: {},type(number1): {}".format(number1, type(number1)))
print("number2: {},type(number2): {}".format(number2, type(number2)))
print("number3: {},type(number3): {}".format(number3, type(number3)))

# The correct way to write it is
numberBin = "0b1100100"
numberOct = "0o144"
numberHex = "0x64"
number1 = int(str(numberBin),2)
number2 = int(str(numberOct),8)
number3 = int(str(numberHex),16)
print("number1: {},type(number1): {}".format(number1, type(number1)))
print("number2: {},type(number2): {}".format(number2, type(number2)))

Note: the result of str(0x64) is "100" instead of "ox64". Other binary situations are the same, that is, when str() converts a numeric type string, if the numeric type is not decimal, it will be converted to decimal first, and then to decimal string

3. Two ways to solve the bit width (bits) of binary

  • 1. Use len(bin(number))

  • 2. Use the number built-in function bit_length()

  • Example code

number = 256
# Note that the first two 0bs are removed, because when it is 0, 0b0, and if it is negative, the expression is - 0b, so it should be intercepted from the position of 3
bitLength1 = len(bin(number)[2:]) if number > 0 else len(bin(number)[3:])
bitLength2 = number.bit_length()
print("bitLength1: {},bitLength2: {}".format(bitLength1,bitLength2))
  • Operation results
bitLength1: 9,bitLength2: 9

4. Common operations of floating point type float

  1. as_integer_ratio()

    Return tuple (x, y), number = k, number.as_ integer_ ratio() ==> (x,y) x / y = k

  2. hex()

    Floating point number in hexadecimal

  3. fromhex()

    Enter a hexadecimal decimal as a string and return a decimal decimal

  4. is_integer()

    Judge whether the decimal is an integer. For example, 3.0 is an integer and 3.000001 is not. Return a Boolean value

  • Code example
numberFloat = 0.25
(x, y) = numberFloat.as_integer_ratio()
numberFloatHex = numberFloat.hex()
numberFloat = float.fromhex("0x1.0000000000000p-2")

numberFloat1 = 3.00001
numberFloat2 = 3.0
isInteger1 = numberFloat1.is_integer()
isInteger2 = numberFloat2.is_integer()

print("{} / {} = {}".format(x, y, numberFloat))
print("0.25_hex(): {}".format(numberFloatHex))
print("numberFloatFromHex: {}".format(numberFloat))
print("{} is_integer: {}, {} is_integer: {}".format(
    numberFloat1, isInteger1, numberFloat2, isInteger2))
  • Operation results
1 / 4 = 0.25
0.25_hex(): 0x1.0000000000000p-2
numberFloatFromHex: 0.25
3.00001 is_integer: False, 3.0 is_integer: True

5. Common mathematical functions of number types

  1. abs(x)

    Returns the absolute value of a number, for example, abs(-3) = 3

  2. ceil(x)

    Returns the upward rounding result of a floating-point number, for example: math.ceil(1.0003) = 2. Note that it is a negative number
    math.ceil(-2.3334) = -2, not - 3

  3. floor(x)

    Returns the downward rounding result of a floating-point number, for example: math.floor(1.988) = 1. Negative numbers are the same,
    floor takes a small integer, math.floor(-2.33334)=-3, not - 2 here

  4. round(x,n)

    Round to n decimal places. For example, round(2.35763,2) = 2.36

  5. exp(x)

    e^x power, the x power of E, e.g. exp(10) = e ^ 10

  6. fabs(x)

    Find the integer part of float data. For example: math.fabs(2.345) = 2

  7. log(x,n)

    logx value based on n. for example: log(2,3) = log2^3

  8. modf(x)

    Return the decimal part and integer part of float. Note that the decimal part comes first and the integer part comes last
    For example: math.modf(2.34567) = (0.34567,2)

  9. pow(x,y)

    Calculate the result of xy and return it, which is equivalent to xy, that is, the y-power of x, for example, pow(2,3) = 2 ** 3 = 8

  10. sqrt(x)

    Calculate the arithmetic square root of x, for example sqrt(9) = 3

  • Code example
x = -2.435123
y = 1.2345
print("abs(x): {}".format(abs(x)))
print("ceil(x): {}".format(math.ceil(x))) # Take the integer up. Note that the maximum value is also taken when the number is negative. For example, ceil() of - 3. Many is - 3
print("ceil(y): {}".format(math.ceil(y)))
print("floor(x): {}".format(math.floor(x))) # Integer down
print("round(x,n): {}".format(round(x,2))) # Round to several decimal places
print("exp(x): {}".format(math.exp(x))) # e^x
print("fabs(x): {}".format(math.fabs(x))) # Find the absolute value of the integer part
print("log(y): {}".format(math.log(y,10)))
print("log10(y): {}".format(math.log10(y)))
print("modf(x): {}".format(math.modf(x))) # Returns the decimal part and the integer part. The integer part comes after and the decimal part comes before. It returns a tuple
print("pow(2,6): {}".format(pow(2,6))) # Value after x ** y operation

  • Operation results
abs(x): 2.435123
ceil(x): -2
ceil(y): 2
floor(x): -3
round(x,n): -2.44
exp(x): 0.08758697318777672
fabs(x): 2.435123
log(y): 0.09149109426795106
log10(y): 0.09149109426795106
modf(x): (-0.4351229999999999, -2.0)
pow(2,6): 64
sqrt(9): 3.0

Bool Boolean data type

  • Code example
boolValF = False
boolValT = True
print("boolValF: {},type(boolValF): {}".format(boolValF, type(boolValF)))
print("boolValT: {},type(boolValT: {})".format(boolValT, type(boolValT)))

# bool type essentially saves a number int,False = 0,True = 1
print("int(False): {},int(True): {}".format(int(False),int(True)))

print("0 == False: {},1 == True: {}".format(False == 0,True == 1))
  • Operation results
boolValF: False,type(boolValF): <class 'bool'>
boolValT: True,type(boolValT: <class 'bool'>)
int(False): 0,int(True): 1
0 == False: True,1 == True: True

Posted by baennaeck on Sun, 19 Sep 2021 04:39:17 -0700