日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python qq模块_常用的Python模块

發布時間:2023/12/9 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python qq模块_常用的Python模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

1、使用copy模塊來復制

>>> 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

物種都變了是因為copy實際上只做了“淺拷貝”,也就是說他不會拷貝我們要拷貝的對象中的對象。在這里,它拷貝了主對象list對象,但是并沒有拷貝其中的每個對象。因此我們得到的是一個新列表,但其中的對象不是新的,列表more_animals中還是那三個同樣的對象。

b、?深拷貝

more_animals = copy.deepcopy(my_animals)

myanimals[0].species = 'wyrm'

print(my_animals[0].species)

wyrm

print(more_animals[0].spcies)

ghoul

在copy模塊中的另一個函數deepcopy,則會創建被拷貝對象中的所有對象是拷貝。當我們用deepcopy來復制my_animals時,我們會得到一個新列表,它的內容是所有對象的拷貝。這樣做的結果是,對于原來列表中Animal對象的改動不會影響到新列表。

2、keyword模塊記錄了所有的關鍵字

Python自身所用到的那些單詞被稱為關鍵字,比如if,else等。

iskeyword函數返回一個字符串是否是Python關鍵字

變量kwlist包含所有Python關鍵字的列表。

>>> 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模塊獲得隨機數

a、randint函數在一個數字范圍內隨機挑選一個數字。

>>> print (random.randint(1,100))

60

>>> print(random.randint(100,1000))

102

>>> print(random.randint(1000,5000))

1536

猜數字游戲

>>> 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從列表中隨機選取一個元素

>>> import random

>>> desserts = ['ice cream', 'pancakes', 'brownies', 'cookies', 'candy']

>>> print(random.choice(desserts))

cookies

c、用shuffle來給列表洗牌

>>> import random

>>> desseerts = ['ice cream', 'pancakes', 'brownies', 'cookies', 'candy']

>>> random.shuffle(desserts)

>>> print(desserts)

['brownies', 'candy', 'pancakes', 'ice cream', 'cookies']

shuffle函數用來給列表洗牌,把元素打亂。

4、用sys模塊來控制Shell程序

a、用exit函數來退出shell程序

>>> import sys

>>> sys.exit()

b、從stdin對象讀取

>>> import sys

>>> v = sys.stdin.readline()

He who laughs last thinks slowest

>>> print(v)

He who laughs last thinks slowest

c、用stdout對象來寫入

>>> 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

當write結束時,他返回他所寫入的字符的個數。

5、用time模塊來得到時間

>>> import time

>>> print(time.time())

1539761973.2906716

對time()的調用所返回的數字實際上是自1970年1月1日00:00:00AM以來的秒數。

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來轉換日期

>>> import time

>>> print(time.asctime())

Wed Oct 17 16:34:40 2018

asctime以日期的元組為參數,并把它轉換成更可讀的形式。

>>> t = (2020, 2, 23, 10, 30, 48, 6, 0, 0)

>>> print(time.asctime(t))

Sun Feb 23 10:30:48 2020

b、用localtime來得到日期和時間

函數localtime把當前的日期和時間作為一個對象返回,其中的值大體與asctime的參數順序一樣。

>>> 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來休息

但你想推遲或者讓你的程序慢下來時,可以用sleep函數。

>>> for x in range(1, 61):

print(x)

time.sleep(1)

6、用pickle模塊來保存信息

>>> 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)

{...}

總結

以上是生活随笔為你收集整理的python qq模块_常用的Python模块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。