給小白整理的第二篇Python知識點
python影片教學欄位介紹第二篇Python基礎。
本系列Python基礎教學共四篇,本文為第二篇。
6.2 元組
tuple和list十分相似,但是tuple是不可變的,即不能修改tuple,元組透過圓括號中用逗號分割的項定義。
- 支援索引和切片操作
- 可以使用 in查看一個元素是否在tuple中。
- 空元組()
- 只含有一個元素的元組("a",) #需要加個逗號
優點:tuple比list速度快;對不需要修改的資料進行'寫保護',可以是程式碼更安全
tuple與list可以互相轉換,使用內建的函數list()和tuple()。
l = [1, 2, 3] print( l )# [1, 2, 3]t = tuple(l) print(t) # (1, 2, 3)l = list(t)print (l) #[1, 2, 3]复制代码
元組最通常的用法是用在列印語句,如下例:
name = "Runsen"age = 20print( "Name: %s; Age: %d") % (name, age)# Name: Runsen; Age: 20复制代码
函數如下:
- count(value)
傳回元組中值為value的元素的個數
t = (1, 2, 3, 1, 2, 3)print (t.count(2) )# 2复制代码
- index(value, [start, [stop]])
#傳回清單中第一個出現的值為value的索引,如果沒有,則例外ValueError
t = (1, 2, 3, 1, 2, 3) print( t.index(3) )# 2try: print (t.index(4))except ValueError as V: print(V) # there is no 4 in tuple复制代码
6.3 字典
字典由鍵值對組成,鍵必須是唯一的;
eg: d = {key1:value1, key2:value2};
空字典用{}表示;字典中的鍵值對是沒有順序的,如果想要一個特定的順序,那麼使用前需要對它們排序;
d[key] = value
,如果字典中已有key
,則為其賦值為value
,否則新增新的鍵值對key/value
;
del d [key] 可以刪除鍵值對;判斷字典中是否有某鍵,可以使用in 或not in;
d = {} d["1"] = "one"d["2"] = "two"d["3"] = "three"del d["3"]for key, value in d.items(): print ("%s --> %s" % (key, value))#1 --> one#2 --> two复制代码
dict函數如下:
- clear()
d1 = {"1":"one", "2":"two"} d1.clear()print (d1 )# {}复制代码
- copy()
d1 = {"1":"one", "2":"two"} d2 = d1.copy() print( d2 )#{'1': 'one', '2': 'two'}print(d1 == d2) # Trueprint(d1 is d2) # False复制代码
- dict.fromkeys(seq,val=None)
l = [1, 2, 3] t = (1, 2, 3) d3 = {}.fromkeys(l)print (d3) #{1: None, 2: None, 3: None}d4 = {}.fromkeys(t, "default") print(d4) #{1: 'default', 2: 'default', 3: 'default'}复制代码
- get(key,[default])
d5 = {1:"one", 2:"two", 3:"three"}print (d5.get(1) )#oneprint (d5.get(5)) #Noneprint (d5.get(5, "test") )#test复制代码
- has_key(key)
d6 = {1:"one", 2:"two", 3:"three"} print( d6.has_key(1) ) #Trueprint (d6.has_key(5)) #False复制代码
- items()
d7 = {1:"one", 2:"two", 3:"three"}for item in d7.items(): print (item)#(1, 'one')#(2, 'two')#(3, 'three')for key, value in d7.items(): print ("%s -- %s" % (key, value))#1 -- one#2 -- two#3 -- three复制代码
- keys()
d8 = {1:"one", 2:"two", 3:"three"}for key in d8.keys(): print (key)#1#2#3复制代码
- values()
d8 = {1:"one", 2:"two", 3:"three"}for value in d8.values(): print( value)#one#two#three复制代码
- pop(key, [default])
d9 = {1:"one", 2:"two", 3:"three"}print (d9.pop(1) )#oneprint( d9) #{2: 'two', 3: 'three'}print( d9.pop(5, None)) #Nonetry: d9.pop(5) # raise KeyErrorexcept KeyError, ke: print ( "KeyError:", ke) #KeyError:5复制代码
- popitem()
d10 = {1:"one", 2:"two", 3:"three"}print (d10.popitem() ) #(1, 'one')print (d10) #{2: 'two', 3: 'three'}复制代码
- setdefault(key,[default])
d = {1:"one", 2:"two", 3:"three"}print (d.setdefault(1)) #oneprint (d.setdefault(5)) #Noneprint( d) #{1: 'one', 2: 'two', 3: 'three', 5: None}print (d.setdefault(6, "six")) #sixprint (d) #{1: 'one', 2: 'two', 3: 'three', 5: None, 6: 'six'}复制代码
- update(dict2)
d = {1:"one", 2:"two", 3:"three"} d2 = {1:"first", 4:"forth"} d.update(d2)print (d) #{1: 'first', 2: 'two', 3: 'three', 4: 'forth'}复制代码
- viewitems()
d = {1:"one", 2:"two", 3:"three"}for key, value in d.viewitems(): print ("%s - %s" % (key, value))#1 - one#2 - two#3 - three复制代码
- viewkeys()
d = {1:"one", 2:"two", 3:"three"}for key in d.viewkeys(): print( key)#1#2#3复制代码
- viewvalues()
d = {1:"one", 2:"two", 3:"three"}for value in d.viewvalues(): print (value)#one#two#three复制代码
- 索引運算子和切片運算子
- 索引可以得到特定元素
- #切片可以得到部分序列
#索引運算子和切片運算子
numbers = ["zero", "one", "two", "three", "four"] print (numbers[1] )# oneprint (numbers[-1] )# four#print( numbers[5]) # raise IndexErrorprint (numbers[:]) # ['zero', 'one', 'two', 'three', 'four']print (numbers[3:]) # ['three', 'four']print (numbers[:2]) # ['zero', 'one']print (numbers[2:4] )# ['two', 'three']print (numbers[1:-1] )# ['one', 'two', 'three'] 复制代码
相關免費學習推薦:python影片教學
以上是給小白整理的第二篇Python知識點的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Uvicorn是如何持續監聽HTTP請求的? Uvicorn是一個基於ASGI的輕量級Web服務器,其核心功能之一便是監聽HTTP請求並進�...

本文討論了諸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和請求等流行的Python庫,並詳細介紹了它們在科學計算,數據分析,可視化,機器學習,網絡開發和H中的用途

在Python中,如何通過字符串動態創建對象並調用其方法?這是一個常見的編程需求,尤其在需要根據配置或運行...
