天天看點

python【1】-基礎知識

python是一種解釋性的、面向對象的、帶有動态語義的進階程式設計語言。

互動式python解釋器:IDLE

python是通過縮進來組織代碼段的,而不像c#中的大括号。一般推薦使用四個空格作為縮進。

除法:

預設情況下,一個整數除以另一個整數,結果仍然為整數:1/2 =0

如果其中一個數是浮點數,那麼結果就是浮點數。

1.0/2 =>0.5 1/2.0=>0.5 1/2. =>0.5

雙斜線:使用雙斜線即使參數是浮點數,結果也會是整除。

1.0//2=》0.0

幂運算:

2**3=>8 -2**3=>-8

擷取使用者輸入:input(str)方法。

>>> x=input('x=') x=5 >>> y=input('y=') y=6 >>> print x*y 30

幂函數:pow(2,3)=>8

絕對值:abs(-10)=>10

四舍五入:round(0.4)=>0.0      round(0.6)=>1.0

可以使用import指令導入特殊的子產品來增強功能。

例如:

import math math.floor(1.9) =>1.0

cmath和複數:

>>> import cmath >>> cmath.sqrt(-1) 1j

python本身是支援複數計算的:

>>> (1+2j)+(3+4j) (4+6j) >>> (1+2j)-(3+4j) (-2-2j) >>> (1+2j)*(3+4j) (-5+10j)

python儲存為.py為字尾的檔案

用#井号表示注釋,井号右側的内容不會被解釋。

字元串可以使用單引号或者雙引号,如果中間字元串内容也包括引号,那麼可以使用轉義字元。

>>> "let's say \"hello world\"" 'let\'s say "hello world"'

拼接字元串使用加号:

>>> x='a' >>> y='b' >>> x+y 'ab'

字元串表示:str和repr

str:把值轉換為合理的字元串形式,便于使用者了解;

repr:把值轉換為合理的python表達式的形式。

>>> x="hello world"; >>> print str(x) hello world >>> print repr(x) 'hello world' >>> y=10000L >>> print str(y) 10000 >>> print repr(y) 10000L

input 與raw_input

input 會假設使用者輸入的是合法的python表達式,在下面的例子中隻有輸入字元串時才會執行成功。

raw_input會把輸入作為原始資料,然後将其放入到字元串中。

一般情況下,建議使用raw_input.

#使用Input >>> name=input('your name?') your name?chenjing Traceback (most recent call last):   File "<pyshell#19>", line 1, in <module>     name=input('your name?')   File "<string>", line 1, in <module> NameError: name 'chenjing' is not defined your name?'chenjing' >>> print 'hello'+name hellochenjing #使用raw_input >>> name=raw_input('your name?') >>> print 'hello '+name hello chenjing 注意raw_input是以字元串形式傳回,如果想要整型需要使用int()進行類型轉換。例如: birth=int(raw_input('your birth')) if(birth<2000):   print 'before 00' else:   print 'after 00'

長字元串

如果比較長的字元串需要跨行,那麼可以使用三個引号,而且其中的引号也不必再使用轉義字元了。

>>> str='''hello world hello 'cathy' hello chenjing''' >>> print str

原始字元串

原始字元串以r開頭,原始字元串不會把反斜線作為特殊字元處理,在原始字元串輸入的每個字元都會直接保持一緻輸出,不過原始字元串不能以反斜線結尾。

c:\program files &gt;&gt;&gt; print r'c:\newfile' <a>c:\newfile</a>

字元串編碼

ASCII編碼是1個位元組,而Unicode編碼通常是2個位元組。

UTF-8編碼:可變長編碼。把一個Unicode字元根據不同的數字大小編碼成1-6個位元組,常用的英文字母被編碼成1個位元組,漢字通常是3個位元組,隻有很生僻的字元才會被編碼成4-6個位元組

在計算機記憶體中,統一使用Unicode編碼,當需要儲存到硬碟或者需要傳輸的時候,就轉換為UTF-8編碼。

Python支援ASCII編碼,通過ord()和chr()函數,可以把字母和對應的數字互相轉換。

print ord('A') &gt;&gt;&gt;65 print chr(65) &gt;&gt;&gt;A

Python在後來添加了對Unicode的支援,以Unicode表示的字元串用<code>u'...'</code>表示

u'測試'

想轉換為utf-8需要encode('utf-8')方法

u'測試'.encode('utf-8')
python【1】-基礎知識

字元串格式化

python【1】-基礎知識

還可以指定是否補位0,以及小數位數

python【1】-基礎知識

如果字元串中包含%,則使用兩個百分号代替%%

python【1】-基礎知識

%s和%r:%s等同于調用str()函數,%r等同于調用repr()。例如:

x="There are %d books."%10

print x

print "I said %r"%x

print "I said %s"%x

print str(x)

print repr(x)

運作結果:

There are 10 books.

I said 'There are 10 books.'

I said There are 10 books.

'There are 10 books.'

Python内置的datetime子產品提供了datetime,date,time等常用時間類型。

date()和time()方法可以分别提取日期和時間;

strftime()方法用于将時間格式化為字元串;

strptime()方法用于将字元串轉換為日期格式;

兩個時間相減可以獲得delta類型的時間差。

# -*- coding: cp936 -*- from datetime import datetime,date,time dt=datetime(2016,5,23,14,40,0) print(dt.day)#23 print(dt.hour)#14 print(dt.date())#2016-05-23 print(dt.time())#14:40:00 #格式化字元串 print(dt.strftime("%Y-%m-%d %H:%M"))#2016-05-23 14:40 #字元串轉換為時間 dt=datetime.strptime('201605231440','%Y%m%d%H%M') print(dt) #2016-05-23 14:40:00 #時間差 dt1=datetime(2016,5,23,14,40) dt2=datetime(2016,5,1) delta=dt1-dt2 print(type(delta))#&lt;type 'datetime.timedelta'&gt; print(delta)#22 days, 14:40:00 print(dt+delta)#2016-06-15 05:20:00     本文轉自 陳敬(Cathy) 部落格園部落格,原文連結: http://www.cnblogs.com/janes/p/5520177.html ,如需轉載請自行聯系原作者