Python學習筆記(一)

2021-07-10 16:31:06 字數 4468 閱讀 7489

《python學習手冊》學習筆記

我的python版本是2.7,由於其它環境不相容3.x。

「>>>」後面的是使用者輸入的命令,機器返回值前面則沒有標誌。

數字

分為整數、浮點數、以及更為少見的型別(有虛部的複數、固定精度的十進位制數等)

>>> 

123+111

234>>>

1.5*4

6.0>>>

2**4

#次方運算子,python中#是注釋符號

16>>> len(str(2**100)) #計算數字的長度,需要先轉換為string型別

31

也可以使用數學模組,模組只不過是我們匯入以供使用的一些額外工具包。

>>> 

import math

>>> math.pi

3.141592653589793

>>> math.sqrt(81) #開方運算子

9.0>>> math.pow(2,4) #乘方運算子

16.0

>>> import random

>>> random.random() #0到1之間的浮點數

0.8460230849617111

>>> random.choice([1,2,3]) #在幾個選項中隨機選取某個值

3>>> random.randint (3,6) #生成3到6之間的整數

5

字串

>>> a = 'ellen'

>>> len(a) #輸入字串的長度

5>>> a[4] #輸出某個位置的字元,字元順序從『0』開始

'n'>>> a[-2] #可以使用反向索引,最後乙個字元索引是『-1』,以此類推

'e'>>> a[-1-2] #可以在中括號內進行數值運算

'l'

>>> a[1:4]  #可以進行「分片」操作,從『1』到『4』,但不包括『4』位置的字元

'lle'

>>> a[1:] #冒號後面預設為字串尾

'llen'

>>> a[:3] #冒號前面預設字串首

'ell'

>>> a[:-1]

'elle'

>>>

>>> a[:]

'ellen'

字串可以進行合併操作:

>>> a + 'haha'

'ellenhaha'

>>> a * 3

'ellenellenellen'

不可變性:以上的例子中我們並沒有通過任何操作最原始的字串進行改變。字串在python中具有不可變性——在建立後不能就地改變。例如,不能通過對其某一位置進行賦值而改變字串。

例如,如果我們要改變某一字元,下面的命令會報錯:

>>> a[2] ='b'

traceback (most recent call last):

file "", line

1, in

a[2] ='b'

typeerror: 'str' object does not support item assignment

字串還有一些獨有的操作方法,比如find()和replace()方法:

>>> a.find('e')

3>>> a.replace('en','abc')

'ellabc'

>>> a

#注意a字串依然沒有任何變化

'ellen'

還有很多實用的方法:

>>> b.split('.') #按照某個字元分隔字串

>>> a.upper() #全部轉為大寫字母

'ellen'

>>> a.isalpha () #判斷值是否是字元

true

>>> c='111'

>>> c.isalpha ()

false

>>> c.isdigit() #判斷值是否是數字

true

>>> d=' sdfsd '

>>>> d.rstrip() #去除字串右面的空白字元

' sdfsd'字串還支援乙個叫格式化的高階替代操作,可以易表示式的形式和乙個字串方法呼叫形式使用,類似c語言格式化輸出:printf("result is %d", num);

>>> 

'%s, my name is %s.'%('hello', 'ellen') #第一種方式

'hello, my name is ellen.'

>>>

", what's your ".format('ok', 'name') #第二種方式

"ok, what's your name"

我們也可以檢視某個型別的屬性:

>>> a

'ellen'

>>> dir(a) #使用dir()方法

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

dir()方法列出了方法的名稱,如果我們要知道它們是做什麼的,可以使用help()函式:

>>> help(a.upper)

help on

built-in

function

upper:

upper(...)

s.upper() -> string

return a copy of

thestring s converted to uppercase.

其它編寫字串的方法:

>>> a='a\nb\tc'

>>> a

'a\nb\tc'

>>> len(a)

5>>> ord('a') #返回字元的ascii碼

97

還可以使用三引號來更方便的輸出字串:

>>> msg = 'hello\nworld\nim python'

>>> msg

'hello\nworld\nim python'

>>> print(msg)

hello

world

im python

使用三引號就變為了:

>>> msg = """hello

world

im python"""

>>> msg

'hello\nworld\nim python'

>>> print(msg)

hello

world

im python

這是乙個微妙的語法上的便捷方式,但是在做拼接html**段這樣的工作時則會方便不少!

Python學習 學習筆記(一)

python是什麼?人們為和使用python python的缺點 如今誰在使用python 流行的p2p檔案分享系統bitjorrent是乙個python程式。eve online這款大型多人網路遊戲 massively multiplayer online game,mmog 廣泛地使用pytho...

python學習學習筆記一

1,python 是完全物件導向的語言。在python中一切都是物件,函式 模組 字串等都是物件。2,資料型別 數字,字串,列表,元組,字典 數字型 整型 浮點型 布林型 非零即真 複數型 int x float x 型別轉換 非數字型 字串 列表 元祖 字典 list 元祖 元祖轉列表 tuple...

Python學習筆記 一

python學習筆記 一 關鍵知識點 1 程式列印輸出使用print語句 2 使用print輸出字串時,字串內容不帶引號。而使用字串變數名輸出時,字串內容由引號括起來 3 在python 解析器中下劃線 表示最後乙個表示式的值 4 重定向輸出符合為 5 程式中需要輸入時,實用raw input 內建...