标签:初识 python
函数:判断是否是数字
法一:
vi 11.py
#!/usr/bin/python
def fun():
sth = raw_input("please input something: ")
try:
if type(int(sth)) ==type(1):
print "%s is a number" %sth
except ValueError:
print "%s is not number" %sth
fun()
保存并退出
[root@Client-1 day2]# python 11.py
please input something: 45
45 is a number
-----------------------------------------------
法二:
[root@Client-1 day2]# vi 12.py
#!/usr/bin/python
import sys
def isNum(s):
for i in s:
if i in "0123456789":
pass
else:
print "%s is not a number" %s
sys.exit()
else:
print "%s is a number" % s
isNum(sys.argv[1])
保存并退出
[root@Client-1 day2]# python 12.py 123
123 is a number
[root@Client-1 day2]# python 12.py 123kh
123kh is not a number
--------------------------------------
打印系统进程号
vi 13.py
#!/usr/bin/python
import sys
import os
def isNum(s):
for i in s:
if i not in ‘0123456789‘:
return False
return True
for i in os.listdir(‘/proc‘):
if isNum(i):
print i
保存并退出
[root@Client-1 day2]# python 13.py
1
2
3
本文出自 “知识改变命运” 博客,谢绝转载!
标签:初识 python
原文地址:http://ahtornado.blog.51cto.com/4826737/1795743