Python學習筆記 6

2021-08-23 11:53:49 字數 3092 閱讀 6118

python學習筆記(6)

1)sequence

sequence是一物件,乙個接乙個地儲存多種資料項。python中sequence有幾種不同型別。

下面先看兩種sequence基本型別:字串和列表

在字串中訪問單個字元:

用for迴圈迭代字串,語法如下:

for variable in string:

statement

statement

etc.

例子:>>> name = 'juliet'

>>> for ch in name:

print chju

lie例2:

# this program counts the number times

def main():

count = 0

my_string = raw_input('enter a sentence: ')

for ch in my_string:

if ch == 't' or ch == 't':

count +=1

main()

使用索引訪問字串中的單個字元

字串的每個字元都有乙個序號,表示它在字串中的位置。

例:>>> my_string = 'roses are red'

>>> ch = my_string[6]

>>> ch

'a'>>> print my_string[0],my_string[6],my_string[10]

r a r

還可以用負數做序號,-1表示字串最後乙個字元,-2表示倒數第2個字元,依次類推。

>>> my_string[-1]

'd'>>> my_string[-2]

'e'序號錯誤

序號有範圍,如『boston』字串的序號為0~5以及-1~-6。超出此範圍則indexerror。

例:>>> city='boston'

>>> print city[6]

traceback (most recent call last):

file "", line 1, in

print city[6]

indexerror: string index out of range

字串分割

格式如下:

string[start:end]

例:>>> full_name = 'patty lynn smith'

>>> middle_name = full_name[6:10]

>>> print middle_name

lynn

例:login.py

# the get_login_name function accepts a first name,

# last name, and id number as arguments. it returns

# a system login name.

def get_login_name(first,last,idnumber):

# get the first three letters of the first name.

# if the name is less than 3 characters, the

# slice will return the entire first name.

set1 = first[0:3]

# get the first three letters of the last name.

# if the name is less than 3 characters, the

# slice will return the entire last name.

set2 = last[0:3]

# get the last three characters of the student id.

# if the id number is less than 3 characters, the

# slice will return the entire id number.

set3 = idnumber[-3:]

# put the sets of characters together.

login_name = set1+set2+set3

return login_name

p14.py

import login

def main():

first = raw_input('enter your first name: ')

last = raw_input('enter your last name: ')

idnumber = raw_input('enter your student id number: ')

# get the login name.

print 'your system login name is:'

print login.get_login_name(first, last, idnumber)

main()

測試子串是否在字串中

用in 或 not in

列表的方法

index(item) 返回序號指定的元素

insert(index, item) 在指定序號後插入item

sort() 列表按從小到大的順序排序

remove(item) 刪除列表中第乙個出現item的項

reverse() 列表反序

例:>>> my_list = [1,2,3,4,5]

>>> del my_list[2]

>>> print my_list

[1, 2, 4, 5]

>>> my_list=[5,4,3,123,50,40,30]

>>> print 'the lowest value is',min(my_list)

the lowest value is 3

>>> print 'the highest value is', max(my_list)

the highest value is 123

Python 學習筆記 6

6.1 字典 字典就是乙個關聯陣列 或者稱為雜湊表 它是通過關鍵字索引的物件的集合。使用大括號 來建立乙個字典。print 字典 dic print dic uu dic username dd dic home print uu print dd dic username pxl dic home...

python 學習筆記(6)

我們要借助python的 語法,把decorator置於函式的定義處 log def now print 2015 3 25 答案 call now 2015 3 25 偏函式 python的functools模組提供了很多有用的功能,其中乙個就是偏函式 partial function funct...

python學習筆記6

字典與集合 詳細解釋鏈結 字典是python語言中唯一的對映型別,用花括號 表示,乙個字典條目就是乙個鍵值對,乙個鍵對應乙個值,是一對一的關係。乙個字典物件是可變的,它是乙個容器型別,能儲存任意個數的python物件,其中也包括其它容器型別。方法keys 返回字典的鍵列表,values 返回字典的值...