Chinaunix首页 | 论坛 | 博客
  • 博客访问: 235711
  • 博文数量: 57
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 557
  • 用 户 组: 普通用户
  • 注册时间: 2015-10-01 18:05
文章分类

全部博文(57)

文章存档

2017年(57)

我的朋友

分类: Python/Ruby

2017-12-01 22:15:41

#判断输入的数是否是数字

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.Xie
  4. # @Time :2017/12/1 20:27
  5. # @File :input.py


  6. def fun():
  7.     sth = raw_input("please input something:")
  8.     try:
  9.         if type(int(sth)) == type(1):
  10.             print "%s is a number" % sth
  11.     except ValueError:
  12.         print "%s is not number" % sth


  13. if __name__ == '__main__':
  14.     fun()
执行结果:
please input something:htsjh
htsjh is not number
13 not number

#判断输入的参数中是不是纯数字

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.Xie
  4. # @Time :2017/12/1 20:37
  5. # @File :canshu.py


  6. import sys


  7. def isNum(s):
  8.     for i in s:
  9.         if i in '0123456789':
  10.             pass
  11.         else:
  12.             print "%s is not a number" %s
  13.             sys.exit()
  14.     else:
  15.         print "%s is a number" %s


  16. if __name__ == '__main__':
  17.     isNum(sys.argv[1])

执行结果:

567 is a number
125yrty is not a number

#打印系统PID

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.Xie
  4. # @Time :2017/12/1 20:46
  5. # @File :printpid.py

  6. import os


  7. def isNum(s):
  8.     for i in s:
  9.         if i in '0123456789':
  10.             pass
  11.         else:
  12.             break

  13.     else:
  14.         print "%s is a number" %s


  15. for i in os.listdir('/proc'):
  16.     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

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.Xie
  4. # @Time :2017/12/1 22:25
  5. # @File :digui.py


  6. def fact(n):
  7.     if n == 1:
  8.         return 1
  9.     return n * fact(n - 1)


  10. if __name__ == '__main__':
  11.     print fact(1)
  12.     print fact(5)
执行结果:
1
120






























阅读(1421) | 评论(0) | 转发(0) |
0

上一篇:python 语法基础2

下一篇:python 语法基础4

给主人留下些什么吧!~~