Python的基本运用(一)

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

第一个博客。。。

      

转载于:https://www.cnblogs.com/proton/p/6662242.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值