1. 程式人生 > >python基礎(不定期更新)

python基礎(不定期更新)

1、python 尋找list中最大值、最小值並返回其所在位置

c = [1,2,4,0]
c.index(min(c))
c.index(max(c))

2、 獲取N天、N小時、N分鐘前的時間

from datetime import datetime

from datetime import timedelta

dd = datetime.now() - timedelta(days=days)(timedelta的引數可以是days、hours、minutes)

3、Python判斷兩個list相等

python判斷list相等,可以將它們換成set來比較。忽略掉位置因素。

4、python2中為什麼在進行類定義時最好要加object,不叫又怎樣。

class Person:
    name = "zhengtong"


class Animal(object):
    name = "chonghong"



if __name__ == "__main__":
    x = Person()
    print "Person", dir(x)

    y = Animal()
    print "Animal", dir(y)


output:
Person ['__doc__', '__module__', 'name']
Animal ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']

可以發現Person類,只擁有了doc、module和自己定義的name變數。
Animal類繼承了object物件,擁有了很多可操作的物件。
這些都是類中的高階特性。