python 學習筆記

來源:互聯網
上載者:User

標籤:tutorial   建立   初始化   perl   type   enumerate   -o   統一   ops   

 

顯示類型

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import types>>> type([])<class ‘list‘>>>> type(1)<class ‘int‘>

 上面試在REPL中操作的,, 如果在源碼檔案中要顯示類型則要加上print

import typesprint(type([]))a = 123;print(type(a))

 

常用資料結構

list

list 是可變長度的, 可以動態添加元素, 元素類型不要求統一, 有序,可重複

建立空list , 兩種方式

alist = []blist = list()

 直接建立帶元素的list

alist = [‘one‘, ‘two‘, 3, True];

 

在末尾追加元素

alist.append(‘four‘) # api list.append(obj)

 

在指定索引位置增加元素

alist.insert(1, ‘one point five‘) # api list.insert(index, obj)

查詢某個索引位置的元素

element1 = alist[0]element2 = alist[2]print(element1)print(element2)

 

修改某個索引位置的元素

alist[3] = ‘three‘

刪除某個索引位置的元素, 知道索引位置的時候使用

alist = [1, 2 ,3, 4]a = alist.pop(1)  # 刪除api , 參數 要刪除的位置, 傳回值 被刪除的元素print(a)  # 2print(alist) #[1, 3, 4]alist.pop()  # 不給參數時 刪除末尾print(alist) # [1, 3]

 

刪除除某個元素, 不知道索引位置的時候

alist = [1, 2, 2, 3]alist.remove(2) # 刪除api , 參數是想要刪除的元素,如果元素值有重複的,只會刪除最靠前的。 沒有傳回值print(alist) #[1, 2, 3]

擷取list 長度

size = len(alist)
print(size)

遍曆list

for v in alist:    print(v) # v 是元素值

遍曆的時候還要拿到索引

for idx, val in enumerate(alist):    print(idx, val) # idx是索引, val是元素值

 

 參考:http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops

 

擷取某一個值的元素有幾個

list = [1, 2 , 2, 4, ‘a‘, ‘a‘,‘b‘]print(list.count(2)) #2print(list.count(‘a‘)) #2

擷取最大元素

llist = [1, 2 , 3, 4, True]max = max(list) # apiprint(max) # 4

混合類型的list 使用這個api 要小心, 比如 int 和 str混合會出錯

list = [1, 2 , 3, 4, ‘a‘]max = max(list)print(max)運行報錯Traceback (most recent call last):  File "c:\Users\myname\Documents\pythonporjects\test.py", line 5, in <module>    max = max(list)TypeError: ‘>‘ not supported between instances of ‘str‘ and ‘int‘

 

 

tuple

元組的元素一旦確定不可更改,只能查詢, 元組本身也不能增加或刪除元素。 元素類型不要求統一, 有序, 可重複

建立空tuple, 兩種方式

atuple = ()btuple = tuple()

空tuple的用處不多 所以通常建立就初始化tuple的元素

atuple = (1, 2 , 2, 4, ‘a‘, ‘b‘)print(atuple)

只有一個元素的tuple 應該這樣

atuple = (1, )print(atuple)

查詢元素

atuple = (1, 2 , 2, 4, ‘a‘, ‘b‘)print(atuple[0])print(atuple[1])

如果嘗試修改元素值則會報錯

atuple[0] = 0Traceback (most recent call last):  File "c:\Users\myname\Documents\pythonporjects\test.py", line 8, in <module>    atuple[0] = 0TypeError: ‘tuple‘ object does not support item assignment

 

擷取tuple長度

t = (1, 2, 3)size = len(t) #apiprintln(size)

 

擷取某一個值的元素有幾個

t = (1, 2 , 2, 4, ‘a‘, ‘a‘,‘b‘)print(t.count(2)) # 2print(t.count(‘a‘)) # 2

 

擷取最大的元素, 參考 list 章節

tuple unpacking

t = (1 ,2)a , b = tprint(a) # 1print(b) # 2

參考 http://stackoverflow.com/questions/10867882/tuple-unpacking-in-for-loops

 

set

set 類似於數學中的集合, 元素無序,元素不可重複

建立空set 只有一種方式

aset = set()

 遍曆set , 因為無序所以不存在索引

for el in aset:    print(el)

 

遍曆一個在遍曆時增長長度的set

 

http://stackoverflow.com/questions/28584470/iterating-over-a-growing-set-in-python

建立空dictionary, 兩種方式

adict = {}bdict = dict()

 

遍曆dictionary

只遍曆key

for k in adict:    print(k) # k

遍曆key 和 value

for key, value in adict.items():    print(key, value)

參考 http://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops-in-python

 

時間和日期處理

 

#!/usr/bin/python# -*- coding:utf-8 -*-import timea = time.time()print("timestamp" , a)a = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) print("now is", a)a = time.localtime()year = a.tm_yearprint("year is", year)

更多參考 Python 日期和時間 | 菜鳥教程 http://www.runoob.com/python/python-date-time.html

 

 

參考 

http://www.runoob.com/python/python-tutorial.html

http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000

http://stackoverflow.com/questions/tagged/python

https://www.dotnetperls.com/python

python 學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.