活動地址:CSDN21天學習挑戰賽
第一節,我們學習【Python開發環境的搭建】,知道什麼了是程序設計語言,了解了Python語言的發展及特點,並進行Python開發環境的搭建以及Python集成開發環境PyCharm的安裝及模板設置.
第二節,我們學習【Python語言的基本語法元素】.包括Python程序的格式框架(縮進、注釋),語法元素名稱,數據類型和語句元素以及基本的輸入輸出函數.
本節,我們將學習【Python的基本數據類型】.數字類型:整數類型、浮點數類型、復數類型.String type and data type conversion between.
# 常見數據類型
""" 整型:int 98 浮點型:float 3.292992 布爾型:False 0; True 1 字符串:str 'hello' '你好' "world" """
Python中提供3種數字類型:整數類型、浮點數類型和復數類型.
PythonThere is no limit to the scope of the integer,In theory as long as memory to store,PythonYou can use any size of integer.
整數類型有4Kind of hexadecimal representation:十進制、二進制、八進制、十六進制
.默認情況下,采用十進制,If you need other base methods need leading symbols added.
二進制是一套計數方法,每個位置上的數有 2 種可能(0 - 1);二進制是計算機的執行語言,但是早在計算機出現前就存在這套計數方法,最早可追溯到古埃及.在日常生活中,我們使用的是十進制,每個位置上的數有 10 種可能(0 - 9).
1.8位(bit) = 1 字節(byte)
1024字節(byte)= 1 千字節(KB)
024千字節(KB)= 1 兆 (MB)
1024MB = 1GB
整數類型的4種進制表示
# -*- coding: utf-8 -*-
# @File : demo.py
# @author: Flyme awei
# @email : [email protected]
# @Time : 2022/8/3 23:28
""" 十進制 默認的進制 二進制 0b開頭 八進制 0o開頭 十六進制 0x開頭 """
print('二進制,0b開頭', 0b1010010)
print('八進制,0o開頭', 0o7432647)
print('十六進制,0x開頭', 0xA36540984)
Different between hexadecimal integer can direct operation or more,The program either hexadecimal expression data,Internal computer are stored in the same format of numerical,進制之間的運算結果默認以十進制方式顯示.
Floating-point said with a decimal value
,在Python語言中的浮點數類型必須帶有小數部分,小數部分可以是0.比如說98
是整數,98.0
就是浮點數.
浮點數有2種表示方法:Decimal representation and scientific notation representation.Floating point there is no other hexadecimal representation.
科學計數法使用字母e
或者E
作為冪的符號,以10
為基數.舉例<a>e<b>
表示a*10**b
比如說:1.01e3
表示1.01*103
,就是1010.0
,-1.01e-3
表示-1.01*10-3
,就是-0.00101
.
# -*- coding: utf-8 -*-
# @File : demo.py
# @author: Flyme awei
# @email : [email protected]
# @Time : 2022/8/3 23:28
# 浮點型 float()
a = 3.114152
print(a, type(a))
# The computer USES the binary storage,浮點數存儲不精確
n1 = 1.1
n2 = 2.2
print(n1+n2) # 3.300000000003
# 浮點數計算 導入decimal模塊 decimal 十進制
from decimal import Decimal # 直接導入decimal模塊的Decimal方法
print(Decimal('1.1')+Decimal('2.2'))
import decimal # 導入decimal模塊
print(decimal.Decimal(1.1)+decimal.Decimal(2.2)) # .Decimal 調用decimal模塊的Decimal方法
Floating-point value range and the decimal precision limited by different computer systems.浮點數的取值范圍[-10308,10308]
,Distinguish between floating point precision is about2.22*10-16
.For most of the operation,浮點數類型的數值范圍和小數精度足夠“可靠”.It is generally believed floating-point type no limit.
>>> 1.01 + 2.02
3.0300000000000002
>>> 123455.346788*0978.456
120795624.79679933
>>> 98765.37854/3456.54677
28.573424608977593
Which the same value of integer and floating point arithmetic precision higher?整數1010
和浮點1010.0
它們的值是相等的,But in the power operation result was different.
>>> pow(1010,32)
1374940678531097054162291350571104044956417832049380936096496320100000000000000000000000000000000
>>> pow(1010.0,32)
1.3749406785310972e+96
>>>
PythonThe floating-point operation there is a language“不確定尾數”問題,The two floating-point arithmetic,May add some“不確定”的尾數.
>>> 0.1 + 0.2
0.30000000000000004
This is not a computer running error,But normal,因為在計算機內部,Using a binary representation floating-point number,Restricted by computer says floating point Numbers using storage width,The binary number is not completely equal to0.1But the tap into0.1的二進制數.So in the computer inside the calculation is the most close to0.1和最接近0.2The two Numbers add operation,The resulting number is close to0.3,But is not necessarily the most close to,Reflected in the decimal is created on the tail.How much is the tail,Internal computer will be determined based on the binary arithmetic to produce
.
Uncertain problem of tail will be the result of the floating-point arithmetic caused certain problems.由於0.1+0.2
Operation results in uncertainty tail,所以與0.3
Judgment is equal to the result ofFalse
.
>>> 0.1 + 0.2 == 0.3
False
在PythonThere is a built-in function calledround()
,Used for rounding operation,所以可以使用round()
This function limit operation result retain digits,Remove the uncertainty tail.
>>> round(0.1 + 0.2, 3)
0.3
>>> round(0.1 + 0.2, 3) == 0.3
True
復數類型表示數學中的復數.復數使用a+bj
的形式表示,Known as the real and imaginary part.其中j稱為“虛部單位”,它被定義為j=√(-1)
.
The plural for example:11.3+4j -5.6+7j 1.23e-4+5.67e+89j
在Python中,The plural USES(a,b)
表示a+bj
,a
表示實部,b
表示虛部,Imaginary part through the suffix“J
”或“j
”來表示.當b
等於1
時,1
不能省略,采用 1j
表示復數,而j則表示Python程序中的一個變量.
復數類型中實部和虛部都是浮點類型,對於復數z,可以用z.rea
l和z.imag
Get real part and imaginary part.
print((1.23e4 + 5.67e4j).real)
print((1.23e4 + 5.67e4j).imag)
print(1.23e4 + 5.67e4j.imag)
output:
12300.0
56700.0
69000.0
布爾類型 用來表示真或假
True
表示真False
表示假True:1
False:0
# -*- coding: utf-8 -*-
# @File : demo.py
# @author: Flyme awei
# @email : [email protected]
# @Time : 2022/8/3 23:28
f1 = True
f2 = False
print(f1, type(f1))
print(f2, type(f2))
# True <class 'bool'>
# False <class 'bool'>
字符串又被稱為不可變的字符序列
可以使用單引號:' '
雙引號:" "
三引號:''' '''
或:""" """
來定義,單引號和雙引號定義的字符串必須在一行,三引號定義的字符串可以分布在連續的多行
# -*- coding: utf-8 -*-
# @File : demo.py
# @author: Flyme awei
# @email : [email protected]
# @Time : 2022/8/3 23:28
print('我用python')
print("我用python")
print('''我用python''')
print(type('我用python'))
print(type("我用python"))
print(type('''我用python'''))
output:
''' 我用python 我用python 我用python <class 'str'> <class 'str'> <class 'str'> '''
數據類型轉換:將不同類型的數據拼接在一起
str()
將其它類型轉換為字符串也可用引號轉換str(123)
'123'
int()
Transfer other types is converted to the integer文字類和The decimal class string,無法轉換為整數; 浮點數轉為整數:抹零取整int('123')
float()
Will other types into a floating point number整數轉為浮點數,末尾為.0
float(9)
# -*- coding: utf-8 -*-
# @File : demo.py
# @author: Flyme awei
# @email : [email protected]
# @Time : 2022/8/3 23:28
""" str() Converts other types to string type """
print(str(123))
print(type(str(123)))
""" int() Other types into integer 1.文字類 和 The decimal class string 無法轉換為整型 2.Floating point Numbers to integers MaLing integer """
print(int(3.22244))
""" float() Will other types into a floating point number 1.文字無法轉換 2.整數轉換為浮點數.末尾為 .0 """
print(type(5))
print(float(5), type(float(5)))
常見數據類型:
- 整數類型
int
- 浮點類型
float
- 布爾類型
True/false
- 字符串類型
str
數據類型轉換:
str()
Converts other types to string typeint()
Other types into integerfloat()
Will other types into a floating point number