当前位置:网站首页 > Python编程 > 正文

python函数没有return,返回什么(python 没有return返回none)



1.python字符串格式化中,%s和.format的主要区别是什么

print('ma name is {} ,age is {} year old'.format("小明",17))print('my name is {1},age is {0}'.format(15,'小明')) #:位置攒参的方式li=['hollo',18]print('may name is {0},my year old is {1}'.format(*li))# 按照关键字传参hash={'name':'hoho','age':18}print ('my name is {name},age is {age}'.format(hash))

2.现有两个元组,(('a'),('b')),(('c'),('d'))请用匿名函数把他变成[{'a':'c'},{'b':'d'}]

t1=(('a'),('b')) t2=(('c'),('d')) res=lambda t1,t2:[{i:j}for i,j in zip(t1,t2)] print(res(t1,t2))

3.如何给列表去重并保持原来的顺序不变

l=[11,2,3,4,7,6,4,3,54,3,] now_l=[]for i in l:if i not in now_l: now_l.append(i)print(now_l) 也可以用set直接去重和排序print(set(l)

4.解释什么是匿名函数,它有什么好处

为什么匿名函数没有语句?

是被用于在代码被执行的时候构建新的函数对象并且返回

5.python如何书写可变参数和关键字参数

在python中定义函数,可也用必选函数,默认参数,可变参数,和关键字参数,这四种参数都可以一块使用,或者只用其中的一些但是请注意的是参数定义的顺序必须是必选参数,默认参数,可变参数,关键字参数

6.python模块中match和search的区别

7. 1 and 2和1 or2输出结果是什么

python中的and是从左往右计算的,若所有的值都是真的,则返回最后一个,若存在假的,返第一个假的,1 or 2因为是1是非零,所以返回的值是1

8.,示例说明args和kwargs

9.写出打印结果

print((i%2 for i in range(10)))   #:<generator object <genexpr> at 0X000001c577DDE258>print([i%2 for i in range(10)])    [0,,1,0,1,0,1,0,1]

10.python2和python3的区别

11.请描述unIcode,utf-8,gbk之间的关系

Unicode--->encode----->utf-8utf-------->decode------>unicode

12.迭代器,生成器,装饰器

13.def func(a,b=[])这样写有什么陷阱?

image.png

所以默认参数要牢记一点:默认参数必须指向不变对象

14.python实现9乘9乘法两种方法?

for i in range(1,10):        for j in range(1,i+1):             print('%s * %s= %s'%(i,j,i*j),end="")        print('')
     print( ' '.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)])for x in range(1,10)]))

15.如何在python中拷贝一个对象,并说出他们的区别

image.png

16.谈谈你对python装饰器的理解

def outher(func):        def good(*args,kwargs):                ret=func(*args,kwargs)                return ret       return good@outherdef bar():       print('狗') bar()

17.python基础:

18.简述python中垃圾回收机制

19.如何判断一个变量是否是字符串

a='sadsadada'print(isinstasnce(a,str))

20.Differences between list and tuple in python link:

http://www.hacksparrow.com/python-difference-between-list-and-tuple.html

21.range和xrange的区别

22.'1','2','3'如何变成['1','2','3']?

x='1','2','3'li=[]for i in x:      li .append(i)print(li)'1','2','3' 如何变成[1,2,3] x='1','2','3'li=[]for i in x:      li .append(int(i))print(li)

23.一行实现[1,4,9,16,25,36,49,81,100]

target=list(map(lambda x:  x*x,[1,2,3,4,5,6,7,8,9,10]

24.print(map(str,[1,2,3,4])))输出的是什么

map obj

25.请解释Python中的statimethod(静态方法)和classmethod(类方法),

并补全下面的代码

image.png

26.如何用python删除一个文件?

import os os.remove(r'D:gggfileobj) os.remove(‘路径’)

27.面向对象

](https://upload-images.jianshu.io/upload_images/-d872b16188d4ba64.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

28.python如何生成随机数

import random print(random.randint(1,10))

29.如何在function里面设置一个全局变量

image.png

30.请用python写一个获取用户输入数字,并根据数字大小输出不同脚本信息

image.png

31.请解释生成器和函数的不同,并实现简单的生成器与函数

# def beautiful(c):# for i in range(c):# yield i2# for item in beautiful(5):# print(item)#函数# def gensquares(N):# res=[0]# for i in range(N):# res.append(i*i)# return res# for item in gensquares(5):# print(item)

38.输入一个字符串,打印结果为倒叙

a=input('please enter a string')print(a[::-1])

32.请写出自己的计算方法,按照升序合并如下列表

list1=[2,3,4,9,6] list2=[5,6,10,17,11,2] list1.extend(list2)print(sorted(list(set(list))))33.到底什么是python?你可以在回答中进行技术对比(也鼓励这样做)。python是一门解释性语言,这里说与c语言和c的衍生语言不同,python代码不需要编译,其他解释语言还包括Ruby和pyp python是动态语言,在声明变量的时候,不需要声明变量的类型 python非常适合面向对象编程,因为它支持通过组合和继承的方式定义类 python代码编写快,但是运行速度比编译语言通常要慢,好在python允许加入基于c语言编写扩展,因此我们能优化代码,消除瓶颈,这点通常是可以实现的numpy就是一个很好的例子 python用途很广泛----网络应用,自动化,科学建模,大数据应用,等等通常被称为胶水语言

34.http状态码的不同,列出你所知道的http状态码,然后讲出他们都是什么意思

200 OK//客户端请求成功 400 Bad Request//客户端请求有语法错误,不能被服务器所理解401 Unauthorized//请求未经授权,这个状态代码必须和WWW-Authenticate报头域一起使用 403 Forbidden//服务器收到请求,但是拒绝提供服务 404 Not Found//请求资源不存在,eg:输入了错误的URL 500 Internal Server Error//服务器发生不可预期的错误 503 Server Unavailable//服务器当前不能处理客户端的请求,一段时间后可能恢复正常

34.lambda表达式和列表推导式

l2=[lambda:i for i in range(10)] l1=[i for i in range(10)] print(l1[0]) print(l2[0]) print(l2[0]())

35.eval和exce的区别

print(eval('2+3'))print(exec('2+1'))exec("print('1+1')")
eval还可以将字符串转化成对象
class obj(object):     passa = eval('obj()') print(a,type(a))  #<__main__.obj object at 0x000001F1F> <class '__main__.obj'>

36.cookie和session的区别

37.http是有状态协议还是无状态协议?如何从两次请求判断是否是同一用户?

无状态协议,判断session_id是否相同。

38.有一组数组[3,4,1,2,5,6,6,5,4,3,3]请写一个函数,找出该数组中

没有重复的数的总和

def MyPlus(l):        new_l = []      for i in l:          if i not in new_l:               new_l.append(i) return sum(new_l) print(MyPlus(l = [2,3,4,4,5,5,6,7,8]))

39.Python一行print出1-100的偶数列表:

print(i for i in range(1,100)if  i%2 ==0])

40.1,2,3,4,5能组成多少个互不相同且无重复的元素?

nums = [] for i in range(1,6): for j in range(1,6): for k in range(1,6): if i != j and j != k and i !=k: new_num = i*100 + k*10 + j if new_num not in nums: nums.append(new_num) print(nums)

41.请写出五中不同的http的请求方法:

get,post,put,delete,head,connect

42.描述多进程开发中的join与daemon的区别

43.简述GRL对python的影响

44.执行python脚本的两种方式

https://www.imooc.com/article/run.py.shell直接调用python脚本 python run.py 调用python 解释器来调用python脚本

45.TCP协议和UDP协议的区别是什么?

46.一句话总结tcp和udp的区别

tcp是面向连接的,可靠的字节流服务,udp是面向无连接的,不可靠而数据服务

47.python有如下一个字典

print(sorted(dic_tmp.items(),key=lambda dic_tmp:dic_tmp[0]))

48.简述inndb和myisam的特点:

49.利用python上下文管理器,实现一个写入文件的功能(写入内容为'hello world')

class Open(object):     def __init__(self,name,mode):        self.name=name        self.mode=mode    def __enter__(self):        self.opened=open(self.name,self.mode)        return self.opened    def __exit__(self,exc_type,exc_val,exc_tb):        self.opened.close() with open('mytext.txt','w') as write:     write.write('hello   qaweqwe world')

50.Django的请求生命周期

51.django rest framework规范

52.jsonp和cors

53.现有字典dic = {'a':1,'b':2,'c':23,'d':11,'e':4,'f':21},请按照字段中的value进行排序

print(sorted(dic.items(),key=lambda dic:dic[1]))

54.mysql 搜索引擎和局域网权限,mysql中的锁

55.二叉树的遍历

class BiTreeNode(object):     def __init__(self,data):         self.data = data         self.lchild = None         self.rchild = Nonea = BiTreeNode('A') b= BiTreeNode('B') c = BiTreeNode('C') d = BiTreeNode('D') e = BiTreeNode('E') f = BiTreeNode('F') g = BiTreeNode('G') e.lchild = a e.rchild = g a.rchild = c c.lchild = b c.rchild = d g.rchild = f root = e# 前序遍历  (先找做子树,后找右子树)def pre_order(root):     if root:         print(root.data,end='')  # EACBDGF         pre_order(root.lchild)         pre_order(root.rchild) pre_order(root) print('')# 中序遍历def in_order(root):     if root:         in_order(root.lchild)         print(root.data,end='')   # ABCDEGF         in_order(root.rchild) in_order(root)  # ABCDEGFprint('')# 后序遍历def post_order(root):     if root:         post_order(root.lchild)         post_order(root.rchild)         print(root.data,end='') post_order(root)  #BDCAFGE

56.实现页面刷新的方法:

轮训:客户端定向向服务器发送ajax请求,服务器接到请求后马上返回响应信息并关闭连接.
长轮训:客户端向服务器发送ajax请求,服务器连接到hold住连接,直到有新消息才返回响应信息并关闭连接,客户端处理完响应后再向服务端发送新的请求.

57.字典推导式

d = {key: value for (key, value) in iterable}

58.python中单下划线和双下划线

>>> class MyClass():...     def __init__(self): ...             self.__superprivate = "Hello"...             self._semiprivate = ", world!"...>>> mc = MyClass()>>> print mc.__superprivate Traceback (most recent call last):   File "<stdin>", line 1, in <module>AttributeError: myClass instance has no attribute '__superprivate'>>> print mc._semiprivate , world!>>> print mc.__dict__{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}

foo:一种约定,Python内部的名字,用来区别其他用户自定义的命名,以防冲突,就是例如init(),del(),call()这些特殊方法
_foo:一种约定,用来指定变量私有.程序员用来指定私有变量的一种方式.不能用from module import * 导入,其他方面和公有一样访问;
__foo:这个有真正的意义:解析器用classname__foo来代替这个名字,以区别和其他类相同的命名,它无法直接像公有成员一样随便访问,通过对象名.类名__xxx这样的方式可以访问.
详情见:http://www.zhihu.com/question/







59.python重载

60.虚拟内存技术

虚拟存储器是指具有请求和调入功能,能从逻辑上对内存容量加以扩充的一种存储系统

61.分页和分段

分页与分段的主要区别

62.去除列表中重复的元素

用集合

list(set(l))

用字典

l1 = ['b','c','d','b','c','a','a'] l2 = {}.fromkeys(l1).keys()print l2

用字典并保持顺序

l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) l2.sort(key=l1.index)print l2

列表推导式

l1 = ['b','c','d','b','c','a','a'] l2 = [] [l2.append(i) for i in l1 if not i in l2]

sorted排序并且用列表推导式

l = ['b','c','d','b','c','a','a'] [single.append(i) for i in sorted(l) if i not in single] print single

63.链表对调换

1->2->3->4转换成2->1->4->3.

class ListNode:     def __init__(self, x):        self.val = x        self.next = Noneclass Solution:     # @param a ListNode     # @return a ListNode     def swapPairs(self, head):        if head != None and head.next != None:             next = head.next             head.next = self.swapPairs(next.next)            next.next = head            return next         return head

65.创建字典方法

1直接创建

dict = {'name':'earth', 'port':'80'}

2工厂方法

items=[('name','earth'),('port','80')] dict2=dict(items) dict1=dict((['name','earth'],['port','80']))

3frokeys()方法

dict1={}.fromkeys(('x','y'),-1) dict={'x':-1,'y':-1} dict2={}.fromkeys(('x','y')) dict2={'x':None, 'y':None}

66.二分查找

#coding:utf-8def binary_search(list,item):     low = 0     high = len(list)-1     while low<=high:         mid = (low+high)/2         guess = list[mid]        if guess>item:             high = mid-1         elif guess<item:             low = mid+1         else:            return mid    return Nonemylist = [1,3,5,7,9]print binary_search(mylist,3)

67.列举你所知道的python代码检测工具以及它们之间的区别

pychecker是一个python代码静态分析工具,它可以帮助python代码找bug会对代码的复杂度提出警告 pulint高阶的python代码分析工具,分析python代码中的错误查找不符合代码风格标准

68.如何用python来进行查找替换一个文本字符串

69.一些经典的编程题:

转自:https://www.jianshu.com/p/177bb9c418ed

70.用Python字符串“这是一个test字符串”的中文字符个数,字符编码为utf-8

import osimport re s='这是一个test字符串'result=re.findall(r'[一-龥]',s) print(len(result)

71.什么是python或者说python的特点

python是一门解释性语言,运行之前不需要编译,动态类型语言在声明变量的时候不用声明变量的类型面向对象编程,编写快,用途广泛,能让困难的事变得容易.

72.补充缺失的代码

def print_directory_contents(sPath):     import os                                            for sChild in os.listdir(sPath):                         sChildPath = os.path.join(sPath,sChild)        if os.path.isdir(sChildPath):             print_directory_contents(sChildPath)        else:            print sChildPath

73.下面这些是什意思@classmethod,@staticmethod,@property?

74.介绍一下except的用法和作用

try....except执行try下的语句,如果发生异常,则执行过程跳到except语句,对每个except分支顺序尝试执行,如果异常与except中的异常组匹配,指行相应的语句.

75.python中的pass语句作用是什么

占位符

76.python中的unittest是什么

在python中,unittest是python中的单元测试框架,它有支持共享搭建,自动测试,在测试中暂停代码,将不同测试迭代成一组.

77.python中的模块和包

78.如何异常扑捉,常见的异常捕捉有哪些?

79.用python匹配html tag的时候,<.>和<.?>有什么区别?

80.简述标准库中functools.wraps的作用

将原函数的对象指定属性复制给包装函数对象,默认有module。name,doc

81.部署python开发的web程序的方法

82.Django的model有哪几种继承形式,有何不同?

Django中有三种继承方式

83.如何提高网页的响应速度?

84.统计一篇英文文章内每个单词出现频率,并返回出现频率最高的前10个单词及其出现次数。

from collections import Counterimport rewith open('a.txt', 'r', encoding='utf-8') as f:     txt = f.read() c = Counter(re.split('W+',txt))  #取出每个单词出现的个数print(c) ret = c.most_common(10)   #取出频率最高的前10个print(ret)

85.给你一个数,如何找到最大值

    max(iterable, *[, default=obj, key=func]) -> value     max(arg1, arg2, *args, *[, key=func]) -> value          With a single iterable argument, return its biggest item. The    default keyword-only argument specifies an object to return if     the provided iterable is empty.     With two or more arguments, return the largest argument.

86.写出这段代码的输出结果

num=9def f1():     num=20def f2():     print(num) f2() f1() f2()

常见的报错问题

AttributeError 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性x IOError 输入/输出异常;基本上是无法打开文件 ImportError 无法引入模块或包;基本上是路径问题或名称错误 IndentationError 语法错误(的子类) ;代码没有正确对齐 IndexError 下标索引超出序列边界,比如当x只有三个元素,却试图访问x[5] KeyError 试图访问字典里不存在的键 KeyboardInterrupt Ctrl+C被按下 NameError 使用一个还未被赋予对象的变量SyntaxError Python代码非法,代码不能编译(个人认为这是语法错误,写错了)TypeError 传入对象类型与要求的不符合 UnboundLocalError 试图访问一个还未被设置的局部变量,基本上是由于另有一个同名的全局变量, 导致你以为正在访问它 ValueError 传入一个调用者不期望的值,即使值的类型是正确的`
到此这篇python函数没有return,返回什么(python 没有return返回none)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • py文件用什么运行(python3运行py文件)2025-06-12 14:27:04
  • python 返回多个值(python返回多个变量)2025-06-12 14:27:04
  • python函数没有return返回值会怎么样(python函数如果没有return语句)2025-06-12 14:27:04
  • pythonprint占位符(python占位符号)2025-06-12 14:27:04
  • python字典的增删改查(python字典如何添加一个元素和修改一个元素)2025-06-12 14:27:04
  • python删除venv虚拟环境(python3 venv 虚拟环境)2025-06-12 14:27:04
  • 用python绘制函数图像(python 函数绘制)2025-06-12 14:27:04
  • 简单好玩的编程代码Python(简单好玩的编程代码复制)2025-06-12 14:27:04
  • int怎么用python(int怎么用二进制判断正负)2025-06-12 14:27:04
  • python怎么给字典增加键值(python给字典添加键值对)2025-06-12 14:27:04
  • 全屏图片