1. 程式人生 > >面試題講解

面試題講解

1.        a=(1,)b=(1),c=("1") 分別是什麼型別的資料?

 

2.      字串轉化大小寫

str = "www.runoob.com"
print(str.upper()) # 把所有字元中的小寫字母轉換成大寫字母 print(str.lower()) # 把所有字元中的大寫字母轉換成小寫字母 print(str.capitalize()) # 把第一個字母轉化為大寫字母,其餘小寫 print(str.title()) # 把每個單詞的第一個字母轉化為大寫,其餘小寫 

執行以上程式碼輸出結果為:

WWW.RUNOOB.COm

www.runoob.com

Www.runoob.com

Www.Runoob.Com

3.    統計字串中某字元出現次數

python統計字串中指定字元出現的次數,例如想統計字串中空格的數量

s = "Count, the number of spaces."
print s.count(" ")
x = "I like to program in Python"
print x.count("i")


4.保留兩位小數

>>> a=13.949999999999999

上網查了資料,有網友提供了一種方法

>>> print "%.2f" % a 
13.95
5.  list=[2,3,5,4,9,6],從小到大排序,不許用sort,輸出[2,3,4,5,6,9]

ll=[] 

if len(list)>0:

  m=min(list)

  list.remove(m)

  ll.append(m)

  return ll

 1:Python 有哪些特點和優點?

作為一門程式設計入門語言,Python 主要有以下特點和優點:

 

可解釋

具有動態特性

面向物件

簡明簡單

開源

具有強大的社群支援

 

 3. 列表和元組之間的區別是?

答:二者的主要區別是列表是可變的,而元組是不可變的。舉個例子,如下所示:

in s True if an item of s is equal to x, else False (1)
not in s False if an item of s is equal to x, else True (1)
t the concatenation of s and t (6)(7)
n or s equivalent to adding s to itself n times (2)(7)
s[i] ith item of s, origin 0 (3)
s[i:j] slice of s from i to j (3)(4)
s[i:j:k] slice of s from i to j with step k (3)(5)
len(s) length of s  
min(s) smallest item of s  
max(s) largest item of s  
s.index(x[, i[, j]]) index of the first occurrence of x in s (at or after index i and before index j) (8)
s.count(x) total number of occurrences of x in s

 

s[i] x item i of s is replaced by x  
s[i:j] t slice of s from i to j is replaced by the contents of the iterable t  
del s[i:j] same as s[i:j] []  
s[i:j:k] t the elements of s[i:j:k] are replaced by those of t (1)
del s[i:j:k] removes the elements of s[i:j:k] from the list  
s.append(x) appends x to the end of the sequence (same as s[len(s):len(s)] [x])  
s.clear() removes all items from s (same as dels[:]) (5)
s.copy() creates a shallow copy of s (same as s[:]) (5)
s.extend(t) or += t extends s with the contents of t (for the most part the same as s[len(s):len(s)]t)  
*= n updates s with its contents repeated ntimes (6)
s.insert(i, x) inserts x into s at the index given by i(same as s[i:i] [x])  
s.pop([i]) retrieves the item at i and also removes it from s (2)
s.remove(x) remove the first item from s where s[i] ==x (3)
s.reverse() reverses the items of s in place (4)

 

13. 請解釋使用 *args 和 **kwargs 的含義

當我們不知道向函式傳遞多少引數時,比如我們向傳遞一個列表或元組,我們就使用 * args。

  • 1>>> def func(*args):
  • 2    for i in args:
  • 3        print(i)  
  • 4>>> func(3,2,1,4,7)

執行結果為:

在我們不知道該傳遞多少關鍵字引數時,使用 **kwargs 來收集關鍵字引數。

  • >>> def func(**kwargs):
  • 2    for i in kwargs:
  • 3        print(i,kwargs[i])
  • 4>>> func(a=1,b=2,c=7)

位置引數(不要是可變型別),可變引數(*args),預設引數,命名關鍵字引數(必須按名傳參,引數位置可以改變*,名字1,名字2),關鍵字引數(在最後,以字典的形式列印)

 

命名關鍵字引數前如果有可變引數的話,他的*是可以省略額的==