初识 Python 4
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初识 Python 4相关的知识,希望对你有一定的参考价值。
函数:判断是否是数字
法一:
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()
保存并退出
[[email protected] day2]# python 11.py
please input something: 45
45 is a number
-----------------------------------------------
法二:
[[email protected] 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])
保存并退出
[[email protected] day2]# python 12.py 123
123 is a number
[[email protected] 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
保存并退出
[[email protected] day2]# python 13.py
1
2
3
本文出自 “知识改变命运” 博客,谢绝转载!
以上是关于初识 Python 4的主要内容,如果未能解决你的问题,请参考以下文章