這節(jié)復(fù)習(xí)函數(shù)的定義和調(diào)用嵌器,以及模塊導(dǎo)入
詳細(xì)內(nèi)容見笨辦法筆記17
教材代碼
#ex25.py
def break_words(stuff):
??? words = stuff.split(',')
??? return words
def sort_words(words):
??? return sorted(words)
def print_first_word(words):
??? word = words.pop(0)
??? return word
def print_last_word(words):
??? word = words.pop(-1)
??? print word
def sort_sentence(sentence):
??? words = break_words(sentence)
??? return sort_words(words)
def print_first_and_last(sentence):
??? words = break_words(sentence)
??? print_first_word(words)
??? print_last_word(words)
def print_first_and_last_sorted(sentence):
??? words = sort_sentence(sentence)
??? print_first_word(words)
??? print_last_word(words)
1.函數(shù)的定義
以關(guān)鍵字def開始
函數(shù)名能體現(xiàn)出函數(shù)的功能
括號(hào)中可使用參數(shù)肛真,括號(hào)后面緊跟冒號(hào)
函數(shù)體以相同縮進(jìn)為標(biāo)志
2.函數(shù)的調(diào)用
函數(shù)需先定義才可對(duì)其進(jìn)行調(diào)用
3.模塊的導(dǎo)入
將ex25.py導(dǎo)入到另一個(gè)腳本中,以便能使用ex25.py中定義的函數(shù)爽航。
import ex25
或者
from ex25 import *
4.代碼中涉及到幾個(gè)新的函數(shù)
split
str.split([sep[, maxsplit]])
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).
If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].
For example, ' 1? 2? 3? '.split() returns ['1', '2', '3'], and '? 1? 2? 3? '.split(None, 1) returns ['1', '2? 3? '].
split()函數(shù)根據(jù)分隔符對(duì)字符串進(jìn)行分隔成段蚓让。返回一個(gè)列表。
參數(shù)中可設(shè)置分隔符和最大分隔次數(shù)
sorted
sorted(iterable[, cmp[, key[, reverse]]])
Return a new sorted list from the items in iterable.
The optional arguments cmp, key, and reverse have the same meaning as those for the list.sort() method (described in section Mutable Sequence Types).
cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.
key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly).
reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.
In general, the key and reverse conversion processes are much faster than specifying an equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once. Use functools.cmp_to_key() to convert an old-style cmp function to a key function.
The built-in sorted() function is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).
sorted()對(duì)對(duì)象進(jìn)行排序讥珍,最后一個(gè)參數(shù)可用于設(shè)置升序或降序排列
pop
? pop(...)
???? L.pop([index]) -> item -- remove and return item at index (default last).
???? Raises IndexError if list is empty or index is out of range.
pop() 函數(shù)用于移除列表中的一個(gè)元素(默認(rèn)最后一個(gè)元素)历极,并且返回該元素的值。
如果列表為空或者索引超出范圍衷佃,會(huì)引發(fā)一個(gè)IndexError異常趟卸。