智一面的面试题提供python的测试题
使用地址:http://www.gtalent.cn/exam/interview?token=99ef9b1b81c34b4e0514325e9bd3be54

print()
def print(self, *args, sep=' ', end='\n', file=None)
1
print 实例。print 的 file 参数可以指向一个文件句柄(文件描述符),而后直接输出的文件。
 
>>> print(11,22,33) # 输出任意类型, ','默认为空格
11 22 33
>>> print("hello"+"world") # ➕ 可以连接两个字符串
helloworld
>>> print(10,20,sep="#") # 设置 ‘,’ 分割符的值,默认是一个空格
10#20
>>> print(10,20,end="。") # 设置 print 打印结束后的输出内容,默认为换行('\n')
10 20。
 
除此之外,print(r"string")... 还支持 r'', b'', u'', f'' 等特殊参数
r'string\n' # 非转义原生字符串 ==》 输出:string\n
b'bytes' # bytes字节符,打印以b开头,通过 encode() 与 decode() 转换
u/U:        # 表示unicode字符串,通过 encoding() 设置编码
f/format() # 格式化操作
 
input()
def input(“提示信息”)
1
input实例,接受输入,返回一个字符串。
 
>>> a=input("输入") # 在 “xxx” 的内容为提示信息
输入>? 12
>>> type(a)
<class 'str'>
 
max()
def max(*args, key=None)
1
max()实例。根据参数的 Unicode(ascii)码比较,返回其中最大的一个。
 
>>> max(11,22,33)
33
# 对于复合型的列表,如[(),()]或[[],[]]等可以通过以下方式指定比较方式
>>> lst=[[12,34,56],[22,22,22],[10,30,80]]
>>> max(lst) # 默认以第一维的数值进行比较,即每个小列表的第一个数。
[22, 22, 22]
>>> max(lst,key=lambda x:x[1]) # 以小列表的第二维进行比较,输出其中较大的小列表
[12, 34, 56]
>>> max(lst,key=lambda x:x[1]) # 以小列表的第三维进行比较,...
[12, 34, 56]
 
min()
def min(*args, key=None)
1
与 max() 用法相同。
 
len()
def len(*args, **kwargs)
1
len()实例。返回对象(字符、列表、元组等)长度或项目个数
 
>>> str="hello" # 字符串
>>> len(str)
5
>>> t=(1,2,3,4) # 元组
>>> len(t)
4
>>> st={1,2,3,4} # 集合
>>> len(st)
4
>>> dt = {"key1" : 1, "key2" : 2 } # 字典
>>> len(dt)
2
...
# 如果是自定义类型,在类中重写 len() 函数即可。
 
type()
def __init__(cls, what, bases=None, dict=None)
1
type()实例。返回对象类型名称。
 
>>> type(1)
<class 'int'>
>>> a=[1,2,3,4]
>>> type(a)
<class 'list'>
 
isinstance()
def isinstance(x, A_tuple)
1
isinstance()实例。返回一个 bool 类型。
 
>>> isinstance(1,int)
True
>>> isinstance(1,str)
False
>>> isinstance(1,(str,list,int))           
True
 
id()
def id(*args, **kwargs)
1
id()实例。返回数据在内存中的地址。
 
>>> id(1)
140732958299168
>>> a=1
>>> id(a)
140732958299168
 
int()
def __init__(self, x, base=10)
1
int()实例。将数据类型转换为整型。
 
>>> int(1.2) # 浮点数 ==》 整型
1
>>> int("2") # 字符串 ==》 整型
2
>>> int("0b1010",2) # 二进制 ==》 整型
10
>>> int("0o12",8) # 八进制 ==》 整型
10
>>> int("0xa",16) # 十六进制 =》 整型
10
 
除此之外还有类似的函数,如:
 
bin()
oct()
hex()
“”“
def bin(*args, **kwargs)
def oct(*args, **kwargs)
def hex(*args, **kwargs)
”“”
>>> bin(10) '0b1010'
>>> bin(0o12) '0b1010'
>>> bin(0xa) '0b1010'
>>> oct(0b1010) '0o12'
>>> oct(10) '0o12'
>>> oct(0xa) '0o12'
>>> hex(0b1010) '0xa'
>>> hex(0o12) '0xa'
>>> hex(10) '0xa'
 
float()
def __init__(self, *args, **kwargs)
1
float()实例。将数据类型转换为浮点型。
 
>>> float(1)
1.0
>>> float("2")
2.0
 
str()
def __init__(self, value='', encoding=None, errors='strict')
1
str()实例。将数据类型转换为 str 类型
 
>>> str(1) '1'
>>> str([11,22]) '[11, 22]'
>>> str((1,2,3,4)) '(1, 2, 3, 4)'
>>> str(({1,2,3,4})) '{1, 2, 3, 4}'
 
bool()
def __init__(self, x)
1
bool()实例。将参数转换为布尔类型,如果没有参数,返回 False
 
>>> bool(0) False
>>> bool([]) False
>>> bool((0)) False
>>> bool({}) False
>>> bool(None) False
 
list()
def __init__(self, seq=())
1
list()实例。将给定数据类型转换为列表。
 
>>> list((1,2,3,4)) [1, 2, 3, 4]
>>> list("hello") ['h', 'e', 'l', 'l', 'o']
>>> list({"hello":1,"python":2}) # 默认使用字典的 keys 转换为列表
['hello', 'python']
>>> list({"hello":1,"python":2}.values()) # 使用字典的 values 转换为列表
[1, 2]
 
tuple()
def __init__(self, seq=())
1
tuple() 与 list() 用法相同。
 
dict()
def __init__(self, seq=None, **kwargs)
1
dict()实例。使用关键字参数生成字典。
 
>>> dict(name="张三",age="18")
{'name': '张三', 'age': '18'}
 
set()
def __init__(self, seq=())
1
set()实例。set 集合中无重复元素,常用来进行去重操作。
 
>>> lst=[1,2,1,3,4]
>>> list(set(lst))
[1, 2, 3, 4]
>>> tup=(1,2,3,2,1)
>>> tuple(set(tup))
(1, 2, 3)
 
round()
def round(*args, **kwargs)
1
round()实例。数字四舍五入到给定的十进制精度。
 
>>> round(3.14)
3
 
abs()
def abs(*args, **kwargs)
1
abs()实例。返回参数的绝对值。
 
>>> abs(-1)
1
>>> abs(-3.14)
3.14
————————————————
我们的python技术交流群:941108876
智一面的面试题提供python的测试题
http://www.gtalent.cn/exam/interview?token=99ef9b1b81c34b4e0514325e9bd3be54