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

全部博文(57)

文章存档

2017年(57)

我的朋友

分类: Python/Ruby

2017-12-03 23:07:23

#统计行数,句子,字符

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. def wordCount(s):
  4.     chars = len(s)
  5.     words = len(s.split())
  6.     lines = s.count('\n')
  7.     print lines, words, chars


  8. s = open('/etc/passwd').read()

  9. if __name__ == '__main__':
  10.     wordCount(s)
  11. ~
执行结果:
[root@python day03]# python wc.py 
27 36 1194

#类的方法,实例化

点击(此处)折叠或打开

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


  6. class People(object):
  7.     color = 'yellow'

  8.     def think(self):
  9.         self.color = "black"
  10.         print "I am a %s" % self.color
  11.         print "I am a thinker"


  12. ren = People()
  13. print ren.color
  14. ren.think()
执行结果:
yellow
I am a black
I am a thinker

#类的私有变量,内置属性

点击(此处)折叠或打开

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


  6. class People(object):
  7.     color = 'yellow'
  8.     __age = 30

  9.     def think(self):
  10.         self.color = "black"
  11.         print "I am a %s" % self.color
  12.         print "I am a thinker"
  13.         print self.__age


  14. ren = People()
  15. print ren.color
  16. ren.think()
  17. print ren.__dict__

点击(此处)折叠或打开

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


  6. class People(object):
  7.     color = 'yellow'
  8.     __age = 30

  9.     def think(self):
  10.         self.color = "black"
  11.         print "I am a %s" % self.color
  12.         print "I am a thinker"
  13.         print self.__age


  14. ren = People()
  15. print ren.color
  16. ren.think()
  17. print ren.__dict__
执行结果:
yellow
I am a black
I am a thinker
30
{'color': 'black'}

#类的继承

点击(此处)折叠或打开

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


  6. class People(object):
  7.     color = 'yellow'

  8.     def __init__(self):
  9.         self.dwell = 'Earth'

  10.     def think(self):
  11.         print "I am a %s" % self.color
  12.         print "I am a thinker"


  13. class Chiness(People):
  14.     pass


  15. cn = Chiness()
  16. print cn.dwell
  17. cn.think()
执行结果:

Earth
I am a yellow
I am a thinker

























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

上一篇:python 语法基础4

下一篇:python 基础进阶1

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