python零碎知识点一

1、一行代码实现1--100之和
>>> sum(range(1,101))
5050


2、如何在一个函数内部修改全局变量
a=5
print("修改之前的a的值: %s" % a)
def fn():
   global a
   a=3
fn()
print("修改之后的a的值: %s" % a)
[python@master2 test]$ python3 c.py
修改之前的a的值: 5
修改之后的a的值: 3
    
    
3、列出5个python标准库
os:     提供了不少与操作系统相关联的函数
sys:     通常用于命令行参数
re:      正则匹配
math:    数学运算
datetime:处理日期时间
    
    
4、字典如何删除键和合并两个字典
del和update方法
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Name']
del dict['Age']
dict2 = {'Name':'zhang','adress':'zhengzhou'}
dict.update(dict2)
dict
{'Name': 'zhang', 'Class': 'First', 'adress': 'zhengzhou'}
    
    
5、谈下python的GIL
GIL 是python的全局解释器锁,同一进程中假如有多个线程运行,一个线程在运行python程序的时候会霸占python解释器(加了一把锁即GIL),
使该进程内的其他线程无法运行,等该线程运行完后其他线程才能运行。如果线程运行过程中遇到耗时操作,则解释器锁解开,使其他线程运行。
所以在多线程中,线程的运行仍是有先后顺序的,并不是同时进行。多进程中因为每个进程都能被系统分配资源,相当于每个进程有了一个python
解释器,所以多进程可以实现多个进程的同时运行,缺点是进程系统资源开销大.


6、python实现列表去重的方法
list=[11,21,21,30,34,56,78]
a=set(list)
a
{11, 21, 30, 34, 56, 78}
[x for x in  a]
[34, 11, 78, 21, 56, 30]


7、fun(*args,**kwargs)中的*args,**kwargs什么意思?
一个星号*的作用是将tuple或者list中的元素进行unpack,分开传入,作为多个参数;两个星号**的作用是把dict类型的数据作为参数传入。
kwargs是keyword argument的缩写,args就是argument。我们知道,在Python中有两种参数,一种叫位置参数(positional argument),
一种叫关键词参数(keyword argument),关键词参数只需要用 keyword = somekey 的方法即可传参,而位置参数只能由参数位置决定。
这也就决定了位置参数一定要在前面,否则关键词参数数量的变化(比如有些kwargs有默认值因此没有传参或者在后面传参的),
都会使得位置无法判断。因此常见的也是*args 在 **kwargs 前面。

def this_fun(a,b,*args,**kwargs):
    print(a)
    print(b)
    print(args)
    print(kwargs)
this_fun(0,1,2,3,index1=11,index2=22)
[python@master2 test]$ python3 d.py
0
1
(2, 3)
{'index1': 11, 'index2': 22}
    

8、python2和python3的range(100)的区别
python2返回列表,python3返回迭代器,节约内存
[python@master2 test]$ python2
Python 2.7.5 (default, May  3 2017, 07:55:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> range(100)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>>
[python@master2 test]$ python
Python 3.7.1 (default, Dec 14 2018, 19:28:38)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> range(100)
range(0, 100)


10、python内建数据类型有哪些
整型--int
布尔型--bool
字符串--str
列表--list
元组--tuple
字典--dict


11、with和open对比
运用open
f1=open('d.py','r',encoding='utf-8')
data=f1.read()
print(data)
f1.close
防止文件读写时因为产生IOError,而导致close()未调用。我们可以用try... finally
try:
    f=open('d.py','r')
    try:
        print(f.read())
    except:
        print("文件读取异常")
    finally:
        f.close()
except:
    print('打开文件异常')


使用 with  open 方法,当文件读写完成后,会自动帮我们调用 close 方法
with open('d.py', 'r') as f1:
    print(f1.read())


12、列表[1,2,3,4,5],请使用map()函数输出[1,4,9,16,25],并使用列表推导式提取出大于10的数,最终输出[16,25]
map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回。
list=[1,2,3,4,5]
def fn(x):
     return (x**2)
res=map(fn,list)
res=[ i for i in res if i >10]
print(res)
[python@master2 test]$ python a.py
[16, 25]


13.python中生成随机整数、随机小数、0--1之间小数方法

随机整数:random.randint(a,b),生成区间内的整数
随机小数:习惯用numpy库,利用np.random.randn(5)生成5个随机小数
0-1随机小数:random.random(),括号中不传参
import random
import numpy
>>> random.randint(0,10)
8
>>> numpy.random.randn(5)
array([-0.21294827,  0.21725878,  1.22016076,  0.01280653,  0.79922363])
>>> random.random()
0.047529962365749134



14.python中断言方法举例
a=5
assert(a>1)
print('断言成功,程序继续向下执行')
b=8
assert(b>10)
print('断言失败,程序报错')
运行:
[python@master2 test]$ python b.py
断言成功,程序继续向下执行
Traceback (most recent call last):
  File "b.py", line 5, in <module>
    assert(b>10)
AssertionError


15.python2和python3区别?列举5个
1、Python3 使用 print 必须要以小括号包裹打印内容,比如print('hi')
Python2 既可以使用带小括号的方式,也可以使用一个空格来分隔打印内容,比如print 'hi'
2、python2 range(1,10)返回列表,python3中返回迭代器,节约内存
3、python2中使用ascii编码,python中使用utf-8编码
4、python2中unicode表示字符串序列,str表示字节序列
   python3中str表示字符串序列,byte表示字节序列
5、python2中为正常显示中文,引入coding声明,python3中不需要
6、python2中是raw_input()函数,python3中是input()函数



16.用lambda函数实现两个数相乘
>>> sum=lambda a,b:a*b
>>> print(sum(2,3))
6

猜你喜欢

转载自www.cnblogs.com/hello-wei/p/10288505.html
今日推荐