03-01概念知識-常量變量和類型系統

以實例入學

hello world

學語言第一個程序必定爲hello world

(pdf) [root@centos7 PyPDF2.bak]# ipython
Python 3.7.7 (default, Apr  3 2020, 15:51:41)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: print('hello world')                                                                                                                                                      
hello world

常量/變量

常量(僅做了解)

  • 常量:一旦賦值, 就不可以改變, 就是不能重新賦值, python中沒有真正的常量,所以只需要稍微瞭解
  • 字面常量:一個單獨出現的量, 未賦值給任何變量或常量(例如上述"hello world")
  • 關於空格:在python中, 除行首的空格外, 其他空格無意義

變量

變量:是一個名字,在賦值符號的左邊,這個名字可以指代賦值符號右邊的內容
舉例說明,如下i就是一個變量

In [2]: i = 3                                                                                                                                                                     

In [3]: print(i)                                                                                                                                                                  
3

類型系統

強類型

Python是強類型的動態語言
強類型:一般和弱類型做對比,指不同類型之間不能相互計算,運算的時候會做類型檢查。參考如下例子(python是強類型、shell是弱類型)

  • 在python中執行
In [4]: 4 + 4                                                                                                                                                                     
Out[4]: 8

In [5]: 4 + '4'                                                                                                                                                                   
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-ae36418677da> in <module>
----> 1 4 + '4'

TypeError: unsupported operand type(s) for +: 'int' and 'str'

兩個4是不同類型
In [6]: type(4)                                                                                                                                                                   
Out[6]: int

In [7]: type('4')                                                                                                                                                                 
Out[7]: str
  • 在shell中執行
[root@centos7 ~]# expr 4 + 4
8
[root@centos7 ~]# expr 4 + '4'
8

動態類型

動態類型:變量可以重新賦值爲其他類型,請參考如下示例(變量i已經賦值了,但也可以重新賦值)

In [8]: i = 4

In [9]: type(i)
Out[9]: int

In [10]: i = '4'

In [11]: type(i)
Out[11]: str

基本類型

基本類型介紹:總結規律,發現基本類型無法當變量名

  • int:整形
  • float:浮點數
  • bool:布爾類型
  • None

整型和長整型

In [16]: type(1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ** 10)
Out[16]: int

Python 2.7.5 (default, Nov  6 2016, 00:28:07)     # python2中有長整型概念, 在Python3中就沒有
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type(1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ** 10)
<type 'long'>

另外有些語言會有長度限制, 如果超長了就會溢出
python int類型沒有長度限制, 受限物理內存

浮點數

In [17]: type(12.3)
Out[17]: float

In [19]: 12.00000000000000000000000000000000000000001 == 12   浮點數有精度丟失
Out[19]: True

財務、金融、以及科學計算都必須高精度, 可以先轉換爲整型計算然後換算爲小數部分

布爾類型

In [17]: True                                                                                                                                                                     
Out[17]: True

In [18]: False                                                                                                                                                                    
Out[18]: False

None類型

In [19]: None

複合類型

先做簡單介紹,後面都會單獨講解

  • str:字符串
  • list
  • 其他
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章