#统计行数,句子,字符
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
def wordCount(s):
-
chars = len(s)
-
words = len(s.split())
-
lines = s.count('\n')
-
print lines, words, chars
-
-
-
s = open('/etc/passwd').read()
-
-
if __name__ == '__main__':
-
wordCount(s)
-
~
执行结果:
[root@python day03]# python wc.py
27 36 1194
#类的方法,实例化
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.Xie
-
# @Time :2017/12/3 20:10
-
# @File :c1.py
-
-
-
class People(object):
-
color = 'yellow'
-
-
def think(self):
-
self.color = "black"
-
print "I am a %s" % self.color
-
print "I am a thinker"
-
-
-
ren = People()
-
print ren.color
-
ren.think()
执行结果:
yellow
I am a black
I am a thinker
#类的私有变量,内置属性
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.Xie
-
# @Time :2017/12/3 21:41
-
# @File :c2.py
-
-
-
class People(object):
-
color = 'yellow'
-
__age = 30
-
-
def think(self):
-
self.color = "black"
-
print "I am a %s" % self.color
-
print "I am a thinker"
-
print self.__age
-
-
-
ren = People()
-
print ren.color
-
ren.think()
-
print ren.__dict__
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.Xie
-
# @Time :2017/12/3 21:41
-
# @File :c2.py
-
-
-
class People(object):
-
color = 'yellow'
-
__age = 30
-
-
def think(self):
-
self.color = "black"
-
print "I am a %s" % self.color
-
print "I am a thinker"
-
print self.__age
-
-
-
ren = People()
-
print ren.color
-
ren.think()
-
print ren.__dict__
执行结果:
yellow
I am a black
I am a thinker
30
{'color': 'black'}
#类的继承
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.Xie
-
# @Time :2017/12/3 22:56
-
# @File :c3.py
-
-
-
class People(object):
-
color = 'yellow'
-
-
def __init__(self):
-
self.dwell = 'Earth'
-
-
def think(self):
-
print "I am a %s" % self.color
-
print "I am a thinker"
-
-
-
class Chiness(People):
-
pass
-
-
-
cn = Chiness()
-
print cn.dwell
-
cn.think()
执行结果:
Earth
I am a yellow
I am a thinker
阅读(1351) | 评论(0) | 转发(0) |