Python基礎教程 學習筆記 第一章 基礎

2021-09-11 20:55:51 字數 4471 閱讀 9557

加法:+

減法:-

乘法:*

除法:單斜槓(/)結果為浮點數。

>>

>1/

20.5

>>

>1/

11.0

雙斜槓(//)為整除,直接丟棄小數部分。

>>

>1//

20>>

>1//

11>>

>

5.0//

2.42.0

>>

>

10//-3

-4>>

>-10

//-33

(

2.x版本中,整數用單斜槓(/)除法也是預設整除,若要使結果同樣為浮點數,需要先執行:

>>>from __future__ import division #future前後分別是兩個下劃線
)

求餘(求模):% (還可用於浮點數)

>>

>10%

31>>

>

2.75

%0.5

0.25

>>

>10%

-3-2

>>

>-10

%32>>

>-10

%-3-

1

=>!!!注意!!!

無論是整除(//)還是求餘(%)中,對於正數,負數都是向下圓整

乘方(求冪):**

>>

>2**

38>>

>-3

**2-9

>>

>(-

3)**2

9

(都以0打頭)

>>

>

0xaf

#16進製制

175>>

>

010#8進製

8>>

>

0b1011010010

#2進製

722

變數是表示(或指向)特定值的名稱

>>

> x =

3

這稱為賦值(assignment),我們將值 3 賦給了變數 x 。換而言之,就是將變數 x 與值(或物件)3 關聯起來。

使用python變數前必須給它賦值,因為python變數沒有預設值.

>>

> x =

input

("x: ")x:

34>>

> y =

input

("y: ")y:

42>>

>

print

(int

(x)*

int(y)

)1428

pow 求乘方(與 ** 一樣)

>>

>

pow(2,

3)8

abs 計算絕對值

>>

>

abs(-10

)10

round 將浮點數圓整為與之最接近的整數

>>

>

round(2

/3)1.0

5.1.匯入模組:(以及 floor 函式與 ceil 函式呼叫)

>>

>

import math

>>

> math.floor(

32.9

)#math模組中的 floor 函式,向下圓整

32>>

> math.ceil(

32.3

)#math模組中的 ceil函式,向上圓整,與 floor 相反

33

5.2.若不想每次呼叫函式時指定模組名,可用 from module import function.

(前提是確定不會從不同模組匯入同名函式)

>>

>

from math import sqrt

>>

> sqrt(9)

3.0

5.3.可使用變數來引用函式(以及其他大部分python元素)。執行賦值語句 foo =math.sqrt 後,就可使用 foo 來計算平方根。例如, foo(4) 的結果為 2.0

5.4.處理複數的模塊cmath

>>

>

import cmath

>>

> cmath.sqrt(-1

)1j>>

>(1

+3j)*

(9+4j

)(-3

+31j

)

! ! ! 建議使用常規 import 匯入模組

6.1.

單引號,雙引號 都可

用 \』 , \』』 轉義(乙個反斜槓)

6.2.拼接字串

6.2.1.僅當同時依次輸入兩個字串時

>>

>

"let's say "

'"hello, world!"'

'let\'s say "hello, world!"

6.2.2.+

>>

>

"hello, "

+"world!"

'hello, world!'

>>

> x =

"hello, "

>>

> y =

"world!"

>>

> x + y

'hello, world!

6.3 str 與 repr

str :使用 str 能以合理的方式將值轉換為使用者能夠看懂的字串。例如,盡可能將特殊字元編碼轉換為相應的字元.

>>

>

print

(str

("hello,\nworld!"))

hello,

world

repr : 使用 repr 時,通常會獲得值的合法python表示式表示

>>

>

print

(str

("hello,\nword!"))

hello,

world!

6.4長字串,原始字串,位元組

6.4.1.長字串(跨越多行的字串,可使用三引號)

print

('''this is a very long string. it continues here.

and it's not over yet. "hello, world!"

still here.'''

)

三引號可以是三個單引號或者三個雙引號,此時無需用轉義符表示原單雙引號。

常規字串也可跨越多行,可在行尾加上反斜槓。

(反斜槓和換行符將被轉義,即被忽略)

這種方法也適用於表示式和語句。

>>

>1+

2+ \4+

512>>

>

print \

('hello, world'

)hello, world

6.4.2原始字串

原始字串不以特殊方式處理反斜槓

r"…"

>>

>

print

(r'c:\program files\fnord\foo\bar\baz\frozz\bozz'

)c:\program files\fnord\foo\bar\baz\frozz\boz

例外是,引號需要像通常那樣進行轉義,但這意味著用於執行轉義的反斜槓也將包含在最終的字串中

>>

>

print

(r'let\'s go!'

)let\'s go

!!! 原始字串不能以單個反斜槓結尾,除非你對其進行轉義(但進行轉義時,用於轉義的反斜槓也將是字串的一部分)(否則python將無法判斷是否字串結束)

但可以這樣:(再拼接乙個常規字串)

>>

>

print

(r'c:\program files\foo\bar'

'\\'

)c:\program files\foo\bar\

python字串使用unicode、編碼表示文字

—後面還有有關文字編碼內容,待以後學習

Python基礎教程學習筆記

第一章 快速改造 基礎知識 函式描述 abs number 返回數字的絕對值 cmath.sqtr number 返回平方根,也可以應用於負數 float object 將字串和數字裝換為浮點數 help 提供互動式幫助 input prompt 獲取使用者輸入 int object 將字串和數字轉...

python基礎教程學習筆記一

第一章 基礎知識 1.1 直譯器的安裝 一路下一步,完成安裝 修改path,新增安裝路徑 命令列執行顯示如下結果 1.2 互動式直譯器 示例 helloworld.py print hello world 中國 執行示例程式 python helloworld.py 1.3 演算法 1.4 數字和表...

python基礎教程學習筆記二

第一章 列表和元組 2.1 python的六種內建序列 列表,元組,字串,unicode字串,buffer對像,xrange物件 注 列表可以修改,元組不可以 retacn retacn 30 yue yue 32 database retacn,yue database retacn 30 yue...