标签:其他 init extend word 多少 summer number something str
1.a**b 表示a的b次方。
2.def something(a,b): 定义函数,注意 python的缩进 。
3.print (a)与print a 的区别,python3中不支持print a 。
4.
>>> class superList(list):
... def __sub__(self,b):
... a = self[:]
... b = b[:]
... while len(b) > 0:
... element_b = b.pop()
... if element_b in a:
... a.remove(element_b)
... return a
...
>>> print (superList([1,2,3]) - superList([3,4]))
[1, 2]
>>> print (superList([1,2,3]) - superList([3,4,1,2]))
[]
自己写的python中的list的减法运算 。
5.
>>> class Bird(object):
... have = True
... way = ‘egg‘
... def move(self,dx,dy):
... position = [0,0]
... position[0] = position[0] + dx
... position[1] = position[1] + dy
... return position
...
>>> summer = Bird()
>>> print (summer.move(5,8))
[5, 8]
>>> class happyBird(Bird):
... def __init__(self,more_words):
... print (‘we are happy birds‘,more_words)
...
>>> summer = happyBird(‘happy,happy‘)
we are happy birds happy,happy
注意python中类的表示,最基本的是object表示没有父节点,否则就要表示出父节点 。
6.
>>> class Human(object):
... def __init__(self,input_gender):
... self.gender = input_gender
... def printGender(self):
... print (self.gender)
...
>>> li_lei = Human(‘male‘)
>>> print (li_lei.gender)
male
>>> li_lei.printGender()
male
>>> print (dir(list))
[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘clear‘, ‘copy‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]
注意__init__以及其他东西的运用,在python中会对有__init__等带有__的特殊运用 。
7.几个基本对list的自带函数:
>>>nl = [1,2,5,3,5]
>>>print nl.count(5) # 计数,看总共有多少个5
>>>print nl.index(3) # 查询 nl 的第一个3的下标
>>>nl.append(6) # 在 nl 的最后增添一个新元素6
>>>nl.sort() # 对nl的元素排序
>>>print nl.pop() # 从nl中去除最后一个元素,并将该元素返回。
>>>nl.remove(2) # 从nl中去除第一个2
>>>nl.insert(0,9) # 在下标为0的位置插入9
8.最后送上自己写的简单判断闰年的python代码。
>>> def judge(a,b,c):
... if (a % 4) == 0:
... print (a,‘ ‘,b,‘ ‘,c,‘ ‘,‘is leap year‘)
... else:
... print (a,‘ ‘,b,‘ ‘,c,‘ ‘,‘is not a leap year‘)
...
>>> judge(2017,4,3)
2017 4 3 is not a leap year
第一个博客。。。
标签:其他 init extend word 多少 summer number something str
原文地址:http://www.cnblogs.com/proton/p/6662242.html