強制轉換

  1. int 函式強制轉換為整數。
    pyuser@deyu:~/zzz$ python
    Python 3.12.9 (main, Aug 14 2025, 00:00:00) [GCC 14.2.1 20250110 (Red Hat 14.2.1-7)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> x=5.0
    >>> y=int(x)+2
    >>> print(y,type(y))
    7 <class 'int'>
    
  2. float 函式強制轉換為浮點數。
    >>> x=5
    >>> y=float(x)+2
    >>> print(y,type(y))
    7.0 <class 'float'>
    >>> quit()