Python快速入門 1

2022-07-04 17:27:10 字數 2681 閱讀 2823

#

用#號字元開頭注釋單行

"""三個引號可以注釋多行

三個引號可以注釋多行

三個引號可以注釋多行

"""

#

整數3 #

=>3

#

加法1+1 #

=>2#減法

8-1 #

=>7#乘法

10*2 #

=>20

#除法 !!!結果自動轉換成浮點數

35/5 #

=>7.0

5/3 #

=>1.6666666666666667

#整數除法 !!!結果向下取整

5//3 #

=>1

5.0 #

=>1.0

-5//3 #

=>-2

5//(-3) #

=>-2

-5.0 #

=>-2.0

#浮點數的運算結果也是浮點數

3*2.0 #

=>6.0#取模

7%3 #

=>1

#x的y次方

2**4 #

=>16

#用括號決定優先順序

(1+3)*2 #

=>8

#

布林值true #

=>true

false #

=>false

#用not取非

not true #

=>false

not false #

=>true

#邏輯運算子,注意and和or都是小寫

true and false #

=>false

true or false #

=>true

#整數也可以當做布林值

0== false #

=>true

2==true #

=>false

1==true #

=>true

#用==判斷相等

1==1 #

=>true

2==1 #

=>false

#用!=判斷不等

1!=1 #

=>false

1!=2 #

=>true

#比較大小

1<10 #

=>true

2<=2 #

=>true

2>=2 #

=>true

#

字串用單引號雙引號都可以

'這個是字串'"

這個也是字串"#

用加號連線字串

'hello

'+'world'#

=>'hello world'

#字串可以被當做字元列表

'this is a string

'[0] #

=>'t'

#用format來格式化字串

"{} can be {}

".format("

string

",'interpolated

') #

=>'string can be interpolated'

#可以重複引數以節省時間

" be nimble, be quick, jump over the

".format("

jack

","candle stick

") #

=>'jack be nimble,jack be quick,jack jump over the candle stick'#"

wants to eat

".format(name='

bob',food='

lasagna

') #

=>'bob wants to eat lasagna'

#如果你的python3程式也要執行在python2.5以下環境執行,也可以用老式的格式化語法

"%s can be %s the %s way

"%('

strings

','interpolater

','old

') #

=>'strings can be interpolater the old way'

#

none是乙個物件

none

#當與none進行比較時不要用==,要用is,is是用來比較兩個變數是否指向同乙個物件

'etc

'is none #

=>false

none is none #

=>true

#none,0,空字串,空列表,空字典都算是false,其他都是true

bool(none) #

=>false

bool(0) #

=>false

bool("") #

=>false

bool({}) #

=>false

bool() #

=>false

Python快速入門 1

用 號字元開頭注釋單行 三個引號可以注釋多行 三個引號可以注釋多行 三個引號可以注釋多行 整數3 3 加法1 1 2 減法 8 1 7 乘法 10 2 20 除法 結果自動轉換成浮點數 35 5 7.0 5 3 1.6666666666666667 整數除法 結果向下取整 5 3 1 5.0 1.0...

Python快速入門(1)

from 實驗樓 python pep8 官方 風格指導 google python 風格指導 linux下對python源 格式檢查 sudo apt get update sudo apt get install pep8 pep8檢查檔案源 格式錯誤 pep8 file name.py 源程式...

python快速入門(1)變數

一.變數 1.變數賦值 不需要 不需要寫出變數的型別 days 365 2.連續建立變數 用 day 1 1 day 2 2 3.用print列印 python2中不需要 python3裡面print需要 print day 4.列印字元 print day 5.列印變數 6.列印變數型別 一般常用...