Python 判斷字符串是否爲數字
以下實例通過創建自定義函數 is_number() 方法來判斷字符串是否爲數字:
實例(Python 3.0+)
-- coding: UTF-8 --
Filename : test.py
author by : www.runoob.com
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
測試字符串和數字
print(is_number('foo')) # False
print(is_number('1')) # True
print(is_number('1.3')) # True
print(is_number('-1.37')) # True
print(is_number('1e3')) # True
測試 Unicode
阿拉伯語 5
print(is_number('٥')) # True
泰語 2
print(is_number('๒')) # True
中文數字
print(is_number('四')) # True
版權號
print(is_number('©')) # False
我們也可以使用內嵌 if 語句來實現:
執行以上代碼輸出結果爲:
False
True
True
True
True
True
True
True
False
Python 判斷奇數偶數
以下實例用於判斷一個數字是否爲奇數或偶數:
實例(Python 3.0+)
Filename : test.py
author by : www.runoob.com
Python 判斷奇數偶數
如果是偶數除於 2 餘數爲 0
如果餘數爲 1 則爲奇數
num = int(input("輸入一個數字: "))
if(num % 2) == 0:
print("{0} 是偶數".format(num))
else:
print("{0} 是奇數".format(num))
我們也可以使用內嵌 if 語句來實現:
執行以上代碼輸出結果爲:
輸入一個數字: 3
3 是奇數
筆記
優化加入輸入判斷:
while True:
try:
num=int(input('輸入一個整數:')) #判斷輸入是否爲整數
except ValueError: #不是純數字需要重新輸入
print("輸入的不是整數!")
continue
if num%2==0:
print('偶數')
else:
print('奇數')
break
Python 判斷閏年
以下實例用於判斷用戶輸入的年份是否爲閏年:
實例(Python 3.0+)
-- coding: UTF-8 --
Filename : test.py
author by : www.runoob.com
year = int(input("輸入一個年份: "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} 是閏年".format(year)) # 整百年能被400整除的是閏年
else:
print("{0} 不是閏年".format(year))
else:
print("{0} 是閏年".format(year)) # 非整百年能被4整除的爲閏年
else:
print("{0} 不是閏年".format(year))
我們也可以使用內嵌 if 語句來實現:
執行以上代碼輸出結果爲:
輸入一個年份: 2000
2000 是閏年
輸入一個年份: 2011
2011 不是閏年
Python 獲取最大值函數
以下實例中我們使用max()方法求最大值:
實例(Python 3.0+)
-- coding: UTF-8 --
Filename : test.py
author by : www.runoob.com
最簡單的
print(max(1, 2))
print(max('a', 'b'))
也可以對列表和元組使用
print(max([1,2]))
print(max((1,2)))
更多實例
print("80, 100, 1000 最大值爲: ", max(80, 100, 1000))
print("-20, 100, 400最大值爲: ", max(-20, 100, 400))
print("-80, -20, -10最大值爲: ", max(-80, -20, -10))
print("0, 100, -400最大值爲:", max(0, 100, -400))
執行以上代碼輸出結果爲:
2
b
2
2
80, 100, 1000 最大值爲: 1000
-20, 100, 400最大值爲: 400
-80, -20, -10最大值爲: -10
0, 100, -400最大值爲: 100
Python 質數判斷
一個大於1的自然數,除了1和它本身外,不能被其他自然數(質數)整除(2, 3, 5, 7等),換句話說就是該數除了1和它本身以外不再有其他的因數。
test.py 文件:
-- coding: UTF-8 --
Filename : test.py
author by : www.runoob.com
Python 程序用於檢測用戶輸入的數字是否爲質數
用戶輸入數字
num = int(input("請輸入一個數字: "))
質數大於 1
if num > 1:
查看因子
for i in range(2,num):
if(num % i) == 0:
print(num,"不是質數")
print(i,"乘於",num//i,"是",num)
break
else:
print(num,"是質數")
如果輸入的數字小於或等於 1,不是質數
else:
print(num,"不是質數")
執行以上代碼輸出結果爲:
$ python3 test.py
請輸入一個數字: 11 不是質數
$ python3 test.py
請輸入一個數字: 44 不是質數2 乘於 2 是 4
$ python3 test.py
請輸入一個數字: 55 是質數
Python 輸出指定範圍內的素數
素數(prime number)又稱質數,有無限個。除了1和它本身以外不再被其他的除數整除。
以下實例可以輸出指定範圍內的素數:
實例(Python 3.0+)
#!/usr/bin/python3
輸出指定範圍內的素數
take input from the userlower = int(input("輸入區間最小值: "))upper = int(input("輸入區間最大值: "))
for num inrange(lower,upper + 1): # 素數大於 1
if num > 1: for i in range(2,num): if (num % i) == 0: break
else: print(num)
執行以上程序,輸出結果爲:
$ python3 test.py
輸入區間最小值: 1輸入區間最大值: 100
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Python 階乘實例
整數的階乘(英語:factorial)是所有小於及等於該數的正整數的積,0的階乘爲1。即:n!=1×2×3×...×n。
實例
#!/usr/bin/python3
Filename : test.py
author by : www.runoob.com
通過用戶輸入數字計算階乘
獲取用戶輸入的數字
num = int(input("請輸入一個數字: "))
factorial = 1
查看數字是負數,0 或 正數
if num < 0:
print("抱歉,負數沒有階乘")
elif num == 0:
print("0 的階乘爲 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("%d 的階乘爲 %d" %(num,factorial))
執行以上代碼輸出結果爲:
請輸入一個數字: 3
3 的階乘爲 6
Python 九九乘法表
以下實例演示瞭如何實現九九乘法表:
實例
-- coding: UTF-8 --
Filename : test.py
author by : www.runoob.com
九九乘法表
for i in range(1, 10):
for j in range(1, i+1):
print('{}x{}={}\t'.format(j, i, i*j), end='')
print()
執行以上代碼輸出結果爲:
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
通過指定end參數的值,可以取消在末尾輸出回車符,實現不換行。
Python 斐波那契數列
斐波那契數列指的是這樣一個數列 0, 1, 1, 2, 3, 5, 8, 13,特別指出:第0項是0,第1項是第一個1。從第三項開始,每一項都等於前兩項之和。
Python 實現斐波那契數列代碼如下:
實例(Python 3.0+)
-- coding: UTF-8 --
Filename : test.py
author by : www.runoob.com
Python 斐波那契數列實現
獲取用戶輸入數據
nterms = int(input("你需要幾項?"))
第一和第二項
n1 = 0
n2 = 1
count = 2
判斷輸入的值是否合法
if nterms <= 0:
print("請輸入一個正整數。")
elif nterms == 1:
print("斐波那契數列:")
print(n1)
else:
print("斐波那契數列:")
print(n1,",",n2,end=" , ")
while count < nterms:
nth = n1 + n2
print(nth,end=" , ")
更新值
n1 = n2
n2 = nth
count += 1
執行以上代碼輸出結果爲:
你需要幾項? 10
斐波那契數列:
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,
Python 阿姆斯特朗數
如果一個n位正整數等於其各位數字的n次方之和,則稱該數爲阿姆斯特朗數。 例如1^3 + 5^3 + 3^3 = 153。
1000以內的阿姆斯特朗數: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407。
以下代碼用於檢測用戶輸入的數字是否爲阿姆斯特朗數:
實例(Python 3.0+)
Filename : test.py
author by : www.runoob.com
Python 檢測用戶輸入的數字是否爲阿姆斯特朗數
獲取用戶輸入的數字
num = int(input("請輸入一個數字: "))
初始化變量
sumsum = 0
指數
n = len(str(num))
檢測
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** n
temp //= 10
輸出結果
if num == sum:
print(num,"是阿姆斯特朗數")
else:
print(num,"不是阿姆斯特朗數")
執行以上代碼輸出結果爲:
$ python3 test.py
請輸入一個數字: 345
345 不是阿姆斯特朗數
$ python3 test.py
請輸入一個數字: 153
153 是阿姆斯特朗數
$ python3 test.py
請輸入一個數字: 1634
1634 是阿姆斯特朗數
獲取指定期間內的阿姆斯特朗數
實例(Python 3.0+)
Filename :test.py
author by : www.runoob.com
獲取用戶輸入數字
lower = int(input("最小值: "))
upper = int(input("最大值: "))
for num in range(lower,upper + 1):
初始化 sum
sum = 0
指數
n = len(str(num))
檢測
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** n
temp //= 10
ifnum == sum:
print(num)
執行以上代碼輸出結果爲:
最小值: 1
最大值: 10000
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
以上實例中我們輸出了 1 到 10000 之間的阿姆斯特朗數。
好了,本文就給大夥分享到這裏,文末分享一波福利
獲取方式:加python羣 839383765 即可獲取!