內置函數
內置函數: 不需要使用 import 導入庫,就可以直接使用的函數
函數 | 描述 | 備注 |
---|---|---|
len() | 計算容器中元素個數 | |
del( ) | 刪除變量 | |
max( ) | 返回容器中元素最大值 | 如果是字典,只針對key比較 |
min( ) | 返回容器中元素最小值 | 如果是字典,只針對key比較 |
**代碼示例
**
demo_list=[1,2,3,4,5,6,7,8]
demo_dct={"a":1,"b":2,"c":3}
# 長度
print(len(demo_list))
print(len(demo_dct))
# 刪除變量
del(demo_list[0])
del(demo_dct["b"])
print(demo_list)
print(demo_dct)
#求集合、字典中最大值
print(max(demo_list))
print(max(demo_dct))
#求集合、字典中最小值
print(min(demo_list))
print(min(demo_dct))
輸出結果
切片
描述 | Python 表達式 | 結果 | 支持的數據類型 |
---|---|---|---|
切片 | "012345" [1:2] | "543210" | 字符串、列表、元組 |
- 切片: 使用 **索引值 **來限定范圍,從一個大的 **字符串 **中 **切出 **小的 字符串
- 分片操作的的實現需要提供 **兩個索引 **作為邊界,是一個 **左閉右開 **的區間,也就是第1個索引包含在分片內,而第2個索引不包含在這個分片內
代碼示例
demo_str="abcdefghijk"
demo_list=[1,2,3,4,5,6,7,8]
#取一個范圍的值
ss=demo_str[1:5]
print(ss)
#從第三個元素開始,到最后一個元素
ll=demo_list[3:8]
print(ll)
#間隔取值
print(demo_str[::2])
print(demo_list[::2])
#倒著間隔取值
print(demo_str[::-2])
print(demo_list[::-2])
#倒著打印
print(demo_str[::-1])
print(demo_list[::-1])
#從后往前,最后一個元素到第三個元素
print(demo_str[10:1:-1])
print(demo_list[10:1:-1])
輸出結果
運算符
運算符 | Python表達式 | 結果 | 描述 | 支持的數據類型 |
---|---|---|---|---|
+ | [1,2]+[3,4] | [1,2,3,4] | 合并 | 字符串、列表、元組 |
* | ["a"]*4 | ['a', 'a', 'a', 'a'] | 重復 | 字符串、列表、元組 |
in | 1 in [1,2,3] | True | 元素是否存在 | 字符串、列表、元組、字典 |
not in | 4 not in [1,2,3] | True | 元素是否不存在 | 字符串、列表、元組、字典 |
>、>=、<、<=、== | 1<2,1==1 | True | 元素比較 | 數字類型、字符串、列表、元組 |
注:
- in 和 not in 在對 字典 操作時,判斷的是** 字典的鍵**
- in 和 not in 被稱為** 成員運算符**
成員運算符
成員運算符用于 測試 序列中是否包含指定的成員
運算符 | 描述 | 示例 |
---|---|---|
in | 在指定的序列中存在則返回True,否則返回 False | 1 in [1,2,3] 返回True |
not in | 在指定的序列中不存在則返回True,否則返回 False | 4 not in [1,2,3] 返回True |
代碼示例
#合并
s="a"+"b"
l=["a"]+["b"]
t=("a")+("b")
print(s)
print(l)
print(t)
#重復
s="a"*4
l=["a"]*4
t=("a","b")*4
print(s)
print(l)
print(t)
#判斷元素存在
s="a" in "abc"
l="a" in ["a","b","c"]
t="a" in ("abc")
d="a" in {"a":1,"b":2}
s2="f" in "abc"
l2="f" in ["a","b","c"]
t2="f" in ("abc")
d2="f" in {"a":1,"b":2}
print(s)
print(l)
print(t)
print(d)
print(s2)
print(l2)
print(t2)
print(d2)
#判斷元素不存在
s="a" not in "abc"
l="a" not in ["a","b","c"]
t="a" not in ("abc")
d="a" not in {"a":1,"b":2}
s2="f" not in "abc"
l2="f" not in ["abc"]
t2="f" not in ("abc")
d2="f" not in {"a":1,"b":2}
print(s)
print(l)
print(t)
print(d)
print(s2)
print(l2)
print(t2)
print(d2)
輸出結果
for ... else ...
語法
for 變量 in 序列:
執行邏輯
else:
沒有通過 break 退出循環,循環結束后會執行的代碼
應用場景
- 在 迭代遍歷 嵌套的數據類型時,例如:一個列表包含多個字典型數據,要判斷某一個字典中是否存在指定的值
- 存在: 提示并退出循環
- 不存在:在 循環結束后,希望得到一個統一的提示
**代碼示例
**
person_list=[
{"name":"張三"},
{"name":"李四"},
{"name":"王五"}
]
findName="李四"
for p in person_list:
if(p["name"]==findName):
print("找到了 %s" %findName)
break
else:
print("沒有找到需要找的人 %s",findName)
print("第一個循環結束")
findName="田七"
for p in person_list:
if(p["name"]==findName):
print("找到了 %s" %findName)
break
else:
print("沒有找到需要找的人 %s" %findName)
print("第二個循環結束")
輸出結果
語錄:
使人疲憊的不是遠方的高山,而是鞋里的一粒沙子
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
函數
+關注
關注
3文章
4346瀏覽量
62978 -
導入
+關注
關注
0文章
5瀏覽量
7270 -
import
+關注
關注
0文章
15瀏覽量
1987
發布評論請先 登錄
相關推薦
如何使用Python的類? 優勢有哪些?
Python是一種面向對象的高級語言,因此類對于Python非常重要。類是一個空間,在該空間中變量(屬性/方法)分別存儲。運算符' . '用于調用
發表于 07-30 18:08
python類的理解與使用
python類的理解與使用1. 通俗理解類類(英文名 class),是具有相同特性(屬性)和行為(方法)的對象(實例)的抽象模板。從定義上來
發表于 03-07 16:51
python開發之‘類’講解
Python 在盡可能不增加新的語法和語義的情況下加入了類機制。這種機制是 C++ 和 Modula-3 的混合。 Python中的類沒有在用戶和定義之間建立一個絕對的屏障,而是依賴于
發表于 03-15 14:12
?1次下載
python-速成指南
python 提供好用的兩個容器:list 和 dict。插句題外話,其實最好用的容器還是 PHP 提供的關聯數組,一個數組就包括了 python 中 list 和 dict 的全部功
發表于 03-28 16:32
?9次下載
Python類的屬性和方法是什么
編程中我們用類來創建對象。日常生活中的汽車設計圖就是我們Python中的類。日常生活中的小汽車就是Python中的對象。
Python中的類和對象詳解
Python 是一種面向對象的編程語言,它支持類和對象。類是一種用戶自定義的數據類型,用于定義對象的屬性和方法。對象是類的實例,它包含
Python中普通方法、靜態方法、類方法的區別
,由于 Python 語言的靈活性,這部分內容在日常編碼過程中,很容易被忽略掉 本篇文章將和大家一起聊聊這幾個小知識點 2.@staticmethod 裝飾器 @staticmethod 修飾的方法稱為
評論