#判断输入的数是否是数字
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.Xie
-
# @Time :2017/12/1 20:27
-
# @File :input.py
-
-
-
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
-
-
-
if __name__ == '__main__':
-
fun()
执行结果:
please input something:htsjh
htsjh is not number
13 not number
#判断输入的参数中是不是纯数字
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.Xie
-
# @Time :2017/12/1 20:37
-
# @File :canshu.py
-
-
-
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
-
-
-
if __name__ == '__main__':
-
isNum(sys.argv[1])
执行结果:
567 is a number
125yrty is not a number
#打印系统PID
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.Xie
-
# @Time :2017/12/1 20:46
-
# @File :printpid.py
-
-
import os
-
-
-
def isNum(s):
-
for i in s:
-
if i in '0123456789':
-
pass
-
else:
-
break
-
-
else:
-
print "%s is a number" %s
-
-
-
for i in os.listdir('/proc'):
-
isNum(i)
执行结果:
[root@python day03]# python printpid.py
1 is a number
2 is a number
3 is a number
4 is a number
5 is a number
函数传参
[root@ftp day03]# vi fun1_cp.py
1 #!/usr/bin/env python
2
3 import sys
4
5 def cp(sfname,dfname):
6 src_fobj = open(sfname)
7 dst_fobj = open(dfname,'w')
8
9 while True:
10 data = src_fobj.read(4096)
11 if not data:
12 break
13 dst_fobj.write(data)
14 src_fobj.close()
15 dst_fobj.close()
16
17 cp(sys.argv[1],sys.argv[2])
[root@ftp day03]# python fun1_cp.py /etc/hosts /home/zhuji
[root@ftp day03]# ls /home
alvin zhuji
[root@ftp day03]# cat /home/zhuji
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
In [9]:import string
In [10]: string.lowercase
Out[10]: 'abcdefghijklmnopqrstuvwxyz'
In [11]: string.__file__ #2个下划线
Out[11]: '/usr/lib64/python2.6/string.pyc'
#获得文件位置后,查看文件内容
[root@ftp day03]#vi /usr/lib64/python2.6/string.py
# Some strings for ctype-style character classification
whitespace = ' \t\n\r\v\f'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
letters = lowercase + uppercase
ascii_lowercase = lowercase
ascii_uppercase = uppercase
ascii_letters = ascii_lowercase + ascii_uppercase
digits = '0123456789'
hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567'
punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
printable = digits + letters + punctuation + whitespace
#函数的递归
#计算阶乘n! = 1 x 2 x 3 x ... x n
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.Xie
-
# @Time :2017/12/1 22:25
-
# @File :digui.py
-
-
-
def fact(n):
-
if n == 1:
-
return 1
-
return n * fact(n - 1)
-
-
-
if __name__ == '__main__':
-
print fact(1)
-
print fact(5)
执行结果:
1
120
阅读(1421) | 评论(0) | 转发(0) |