昨天和你們分享了61-70題,今天繼續來刷71~80題python
Please write a program to output a random number, which is divisible by 5 and 7, between 10 and 150 inclusive using random module and list comprehension.
import random print (random.choice([i for i in range(10,151) if i%5==0 and i%7==0]))
import random resp = [i for i in range(10,151) if i % 35 == 0 ] print(random.choice(resp))
Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive.
import random resp = random.sample(range(100,201),5) print(resp)
Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive.
import random numbers = random.sample(range(100,201,2),5) print(numbers)
import random print (random.sample([i for i in range(100,201) if i%2==0], 5))
Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive.
import random lst = [i for i in range(1,1001) if i%35 == 0] resp = random.sample(lst,5) print(resp)
Please write a program to randomly print a integer number between 7 and 15 inclusive.
import random number= random.choice([x for x in range(7,16)]) print(number)
import random print(random.randrange(7,16))
Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!".
import zlib s = 'hello world!hello world!hello world!hello world!'.encode() t = zlib.compress(s) print(t) print(zlib.decompress(t))
Please write a program to print the running time of execution of "1+1" for 100 times.
from timeit import Timer t = Timer("for i in range(100):1+1") t.timeit()
import time before = time.time() for i in range(100): x = 1 + 1 after = time.time() execution_time = after - before print(execution_time)
Please write a program to shuffle and print the list [3,6,7,8].
import random lst = [3,6,7,8] random.shuffle(lst) print(lst)
import random lst = [3,6,7,8] seed = 7 random.Random(seed).shuffle(lst) print(lst)
Please write a program to generate all sentences where subject is in ["I", "You"] and verb is in ["Play", "Love"] and the object is in ["Hockey","Football"].
subjects=["I", "You"] verbs=["Play", "Love"] objects=["Hockey","Football"] res = [[i, j, k] for i in subjects for j in verbs for k in objects] for x in res: print(" ".join(x)) Out: I Play Hockey I Play Football I Love Hockey I Love Football You Play Hockey You Play Football You Love Hockey You Love Football
subjects=["I", "You"] verbs=["Play", "Love"] objects=["Hockey","Football"] for sub in subjects: for verb in verbs: for obj in objects: print("{} {} {}".format(sub,verb,obj)) Out: I Play Hockey I Play Football I Love Hockey I Love Football You Play Hockey You Play Football You Love Hockey You Love Football
import itertools subjects=["I", "You"] verbs=["Play", "Love"] objects=["Hockey","Football"] all_list = [subjects,verbs,objects] res = list(itertools.product(*all_list)) for x in res: print(" ".join(x)) Out: I Play Hockey I Play Football I Love Hockey I Love Football You Play Hockey You Play Football You Love Hockey You Love Football
Please write a program to print the list after removing even numbers in [5,6,77,45,22,12,24].
def isEven(n): return n%2!=0 li = [5,6,77,45,22,12,24] lst = list(filter(isEven,li)) print(lst)
li = [5,6,77,45,22,12,24] lst = list(filter(lambda n:n%2!=0,li)) print(lst)
這十道題的代碼在個人github上,若是你們想看一下每道題的輸出結果,能夠點擊如下連接下載:git
個人運行環境Python 3.6+,若是你用的是Python 2.7版本,絕大多數不一樣就體如今如下3點:github
謝謝你們,咱們下期見!但願各位朋友不要吝嗇,把每道題的更高效的解法寫在評論裏,咱們一塊兒進步!!!dom