1、 python中的注釋:
什麼是注釋:通過用自己熟悉的語言,在程式中對某些代碼進行标注說明。
注釋的作用:提高程式的可讀性,注釋在程式中不會運作
python 中的注釋主要有兩種:單行注釋,多行注釋
- 單行注釋:# 被注釋的内容
- 多行注釋:三個引号(可也以為單引号,或雙引号)
# print('hello word') 我被注釋掉了 單行注釋
'''
我是一個多行注釋
print('hello')
print('word')
'''
2、python 中的基本資料類型:
基本資料類型主要有六類:
-
數字類型(Number):
(1)int 整型 例如: 10, 43
(2)float 浮點型 例如:23.24
(3)bool 布爾類型 True 和 False (在python中 False 對應數字0 ,True 對應非0的數字,通常為1)
(4)complex 複數 (使用較少,可以自己了解下)
使用type()方法可以檢視資料類型
# int定義: a = 3 print(a) print(type(a)) # <class 'int'> # float定義: b = 23.24 print(b) print(type(b)) # <class 'float'> # 布爾類型 c = True print(c) print(type(c)) # <class 'bool'>
-
混合數字類型自動更新:
python中劃分數字類型的複雜度:bool < int <float <complex。
在混合類型表達式中,python首先将備操作的對象(數字)轉換成其中最複雜的操作對象資料類型,然後再對相同資料類型的操作對象進行數學運算。
例如:當一個整數和浮點數進行混合時,整數會先更新為浮點數的值之後通過浮點數的運算法則得到結果。
a = 12 b = 10.23 print(a + b) # 22.23 print(type(a + b)) # <class 'float'>
-
數字類型強制轉換:
可以通過内置函數對數字類型進行強制轉換
(1)将int 強制轉換成 bool:0 轉換成False,非0轉換成True
(2)将bool 強制轉為 int:False轉換成0,True轉換成1
(3)将 int 強制轉為float: 直接添加一個0的小數位
(4)将float 強制轉為 int:直接去掉小數部位
float 和 bool 進行轉換同 int 和 bool 進行轉換
# 将 int強制轉換為bool print(bool(10)) # True print(bool(0)) # False # 将 bool轉為 int print(int(False)) # 0 print(int(True)) # 1 # 将 float轉為 int print(int(18.88)) # 18 # 将 int轉為float print(float(18)) # 18.0 # 将 bool轉為float print(float(True)) # 1.0 # 将 float轉為bool print(bool(1.03)) # True
本篇内容主要介紹python中注釋的使用,以及python中的基本資料類型有哪些。主要介紹數字類型。
随後會繼續更新python基礎内容