python知识点博客园_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
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
17.冒泡排序
lis = [56,12,2,4,100,34,345,-65]
print(lis)
def sortport():for i in range(len(lis)-1):for j in range(len(lis)-1-i):if lis[j]>lis[j+1]:
lis[j],lis[j+1]=lis[j+1],lis[j]
returnlis
sortport()print(lis)[python@master2 sys]$ python3 a.py[56, 12, 2, 4, 100, 34, 345, -65]
[-65, 2, 4, 12, 34, 56, 100, 345]
總結
以上是生活随笔為你收集整理的python知识点博客园_python零碎知识点一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java的注释规范_Java 注释规范
- 下一篇: linux eclipse java_实