python qq模块_常用的Python模块
目錄
1、使用copy模塊來(lái)復(fù)制
>>> class Animal:
def _init_(self, species, number_of_legs, color):
self.species = species
self.number_of_legs = number_of_legs
self.color = color
>>> harry = Animal()
>>> harry._init_('hippogriff', 6, 'pink')
>>> import copy
>>> harriet = copy.copy(harry)
>>> print(harry.species)
hippogriff
>>> print(harriet.species)
hippogriff
a、淺拷貝
my_animals[0].spcies = 'ghoul'
print(my_animals[0].species)
ghoul
print(more_animals[0].species)
ghoul
物種都變了是因?yàn)閏opy實(shí)際上只做了“淺拷貝”,也就是說(shuō)他不會(huì)拷貝我們要拷貝的對(duì)象中的對(duì)象。在這里,它拷貝了主對(duì)象list對(duì)象,但是并沒有拷貝其中的每個(gè)對(duì)象。因此我們得到的是一個(gè)新列表,但其中的對(duì)象不是新的,列表more_animals中還是那三個(gè)同樣的對(duì)象。
b、?深拷貝
more_animals = copy.deepcopy(my_animals)
myanimals[0].species = 'wyrm'
print(my_animals[0].species)
wyrm
print(more_animals[0].spcies)
ghoul
在copy模塊中的另一個(gè)函數(shù)deepcopy,則會(huì)創(chuàng)建被拷貝對(duì)象中的所有對(duì)象是拷貝。當(dāng)我們用deepcopy來(lái)復(fù)制my_animals時(shí),我們會(huì)得到一個(gè)新列表,它的內(nèi)容是所有對(duì)象的拷貝。這樣做的結(jié)果是,對(duì)于原來(lái)列表中Animal對(duì)象的改動(dòng)不會(huì)影響到新列表。
2、keyword模塊記錄了所有的關(guān)鍵字
Python自身所用到的那些單詞被稱為關(guān)鍵字,比如if,else等。
iskeyword函數(shù)返回一個(gè)字符串是否是Python關(guān)鍵字
變量kwlist包含所有Python關(guān)鍵字的列表。
>>> import keyword
>>> print(keyword.iskeyword('if'))
True
>>> print(keyword.iskeyword('ozwald'))
False
>>> print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
3、用random模塊獲得隨機(jī)數(shù)
a、randint函數(shù)在一個(gè)數(shù)字范圍內(nèi)隨機(jī)挑選一個(gè)數(shù)字。
>>> print (random.randint(1,100))
60
>>> print(random.randint(100,1000))
102
>>> print(random.randint(1000,5000))
1536
猜數(shù)字游戲
>>> import random
>>> num = random.randint(1,100)
>>> while True:
print('Guess a number between 1 and 100')
guess = input()
i = int(guess)
if i == num:
print('You guess right')
break
elif i < num:
print('Try higher')
elif i > num:
print('Try lower')
b、用choice從列表中隨機(jī)選取一個(gè)元素
>>> import random
>>> desserts = ['ice cream', 'pancakes', 'brownies', 'cookies', 'candy']
>>> print(random.choice(desserts))
cookies
c、用shuffle來(lái)給列表洗牌
>>> import random
>>> desseerts = ['ice cream', 'pancakes', 'brownies', 'cookies', 'candy']
>>> random.shuffle(desserts)
>>> print(desserts)
['brownies', 'candy', 'pancakes', 'ice cream', 'cookies']
shuffle函數(shù)用來(lái)給列表洗牌,把元素打亂。
4、用sys模塊來(lái)控制Shell程序
a、用exit函數(shù)來(lái)退出shell程序
>>> import sys
>>> sys.exit()
b、從stdin對(duì)象讀取
>>> import sys
>>> v = sys.stdin.readline()
He who laughs last thinks slowest
>>> print(v)
He who laughs last thinks slowest
c、用stdout對(duì)象來(lái)寫入
>>> import sys
>>> sys.stdout.write('What does a fish say when it swims into a wall?Dam.')
What does a fish say when it swims into a wall?Dam.50
當(dāng)write結(jié)束時(shí),他返回他所寫入的字符的個(gè)數(shù)。
5、用time模塊來(lái)得到時(shí)間
>>> import time
>>> print(time.time())
1539761973.2906716
對(duì)time()的調(diào)用所返回的數(shù)字實(shí)際上是自1970年1月1日00:00:00AM以來(lái)的秒數(shù)。
def lots_of_numbers(max):
t1 = time.time()
for x in range(0, max):
print(x)
t2 = time.time()
print('it took %s seconds' %(t2-t1))
a、用asctime來(lái)轉(zhuǎn)換日期
>>> import time
>>> print(time.asctime())
Wed Oct 17 16:34:40 2018
asctime以日期的元組為參數(shù),并把它轉(zhuǎn)換成更可讀的形式。
>>> t = (2020, 2, 23, 10, 30, 48, 6, 0, 0)
>>> print(time.asctime(t))
Sun Feb 23 10:30:48 2020
b、用localtime來(lái)得到日期和時(shí)間
函數(shù)localtime把當(dāng)前的日期和時(shí)間作為一個(gè)對(duì)象返回,其中的值大體與asctime的參數(shù)順序一樣。
>>> import time
>>> print(time.localtime())
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=17, tm_hour=16, tm_min=39, tm_sec=32, tm_wday=2, tm_yday=290, tm_isdst=0)
>>> t = time.localtime()
>>> year = t[0]
>>> month = t[1]
>>> print(year)
2018
c、用sleep來(lái)休息
但你想推遲或者讓你的程序慢下來(lái)時(shí),可以用sleep函數(shù)。
>>> for x in range(1, 61):
print(x)
time.sleep(1)
6、用pickle模塊來(lái)保存信息
>>> import pickle
>>> game_data = {'a':'1','b':'2','c':'3'}
>>> save_file = open('save.dat', 'wb')
>>>pickle.dump(game.data, save_file)
>>>save_file.close()
>>>load_file = open('save.dat', 'rb')
>>>loaded_game_data = pickle.load(load_file)
>>>load_file.close()
print(loaded_game_file)
{...}
總結(jié)
以上是生活随笔為你收集整理的python qq模块_常用的Python模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux 轻量化图形界面,YOXIOS
- 下一篇: MCMC算法原理及其实例