码迷,mamicode.com
首页 > 其他好文 > 详细

面试题讲解

时间:2018-12-24 16:34:54      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:运行   not   def   func   body   小数   列表   order   *args   

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),关键字参数(在最后,以字典的形式打印)

 

命名关键字参数前如果有可变参数的话,他的*是可以省略额的==

 

面试题讲解

标签:运行   not   def   func   body   小数   列表   order   *args   

原文地址:https://www.cnblogs.com/song-119/p/10168485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!