Python100例 我的實現展示 46 50例

2021-10-14 01:25:11 字數 2137 閱讀 1954

python100例 我的實現展示(46-50例)

import random

'''46、求輸入數字的平方,如果平方運算後小於 50 則退出。'''

deftest_exam_46()

: x =

int(

input

("請輸入乙個數字,程式將計算並輸出大於等於50的數字和它的平方運算值。\n"))

y = math.

pow(x,2)

if y <50:

exit(

)else

:print

("輸入的數為,計算得出的平方值為"

.format

(x, y)

)'''47、兩個變數值互換。'''

deftest_exam_47()

: a =

3 b =

'abcdefg'

print

("轉換前變數a為,變數b為"

.format

(str

(a), b)

) t = a

a = b

b = t

print

("轉換前變數a為,變數b為"

.format

(str

(a),

str(b)))

'''48、數字比較。'''

deftest_exam_48()

: a =

int(

input

("請輸入第1個數字,程式將比對並輸出和第2個數的大小結果。\n"))

b =int(

input

("請輸入第2個數字,程式將比對並輸出和第1個數的大小結果。\n"))

if a == b:

print

("等於"

.format

(str

(a),

str(b)))

elif a < b:

print

("小於"

.format

(str

(a),

str(b)))

else

:print

("大於"

.format

(str

(a),

str(b)))

'''49、使用lambda來建立匿名函式。'''

deftest_exam_49()

:print

("使用lambda函式傳入引數建立並初始化2維陣列!"

) x =

lambda a, b:[[

0for col in

range

(a)]

for row in

range

(b)]

print

(x(5,5

))'''50、輸出乙個隨機數。'''

deftest_exam_50()

:print

("程式將輸出0-100之間的隨機數。"

)print

(random.randint(0,

100)

)# 產生n--m範圍內的乙個隨機數

print

(random.random())

# 產生0到1之間的乙個隨機數

print

(random.uniform(

1.3,

3.6)

)# 產生n--m範圍內的乙個浮點數

print

(random.randrange(0,

100,5)

)# 產生n--m範圍內間隔為k的整數

print

(random.choice([0

,1,2

,3,4

,5,6

,7,8

,9])

)# 序列中隨機選取乙個元素

if __name__ ==

'__main__'

:# test_exam_46()

# test_exam_47()

# test_exam_48()

# test_exam_49()

test_exam_50(

)

Python100例 我的實現展示 1 5例

python100例 我的實現展示 1 5例 1 有四個數字 1 2 3 4,能組成多少個互不相同且無重複數字的三位數?各是多少?import math deftest exam 01 list x for a in range 1 5 for b in range 1 5 if a b for c...

Python100例 我的實現展示 6 10例

python100例 我的實現展示 6 10例 6 斐波那契數列。deftest exam 06 x int input 請輸入整數x,程式將輸出長度為x的斐波那契數列。n list x 0 1 for i in range 2 x 2 list x i 1 print 長度為的斐波那切數列如下 f...

Python100例 我的實現展示 51 55例

python100例 我的實現展示 51 55例 51 學習使用按位與 deftest exam 51 a 58 a 0011 1010 b 11 b 0000 1011 c 0 c 0000 0000 c a b c 0000 1010 10 ten print c 52 學習使用按位或 deft...