Python入门知识点总结
Python基礎的重要性不言而喻,是每一個入門Python學習者所必備的知識點,作為Python入門,這部分知識點顯得很龐雜,內容分支很多,大部分同學在剛剛學習時一頭霧水。
本節將Python的知識點進行總結與歸納,節選部分在數據分析過程中用到比較多的一些知識,例如字符串、列表、元組、字典等的用法,以及控制流if、for、while的用法,下面一起來學習。
Python 是一種解釋型、面向對象、動態數據類型的高級程序設計語言。Python基礎知識包含Python數據類型,數據結構,控制流等,與其他高級語言類似,順序語句、條件語句、循環語句等是其基本結構。
1.Python基本命令
1.1? 列出已安裝的包
pip list1.2??查看可升級的包
pip list -o1.3??安裝包
pip install SomePackage # 最新版本 pip install SomePackage==1.5.0 # 指定版本1.4??鏡像站安裝
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package1.5??升級包
pip install --upgrade SomePackage # 升級至最新版本 pip install --upgrade SomePackage==1.5.0 # 升級為指定版本1.6??卸載包
pip uninstall SomePackage#導入sys庫只是為了確認一下Python的版本 import sys #導入pandas import pandas as pd import numpy import matplotlib print('Python 版本為:' + sys.version) print('Pandas 版本為:' + pd.__version__) print('Numpy 版本為:' + pd.__version__) print('Matplotlib 版本為:' + matplotlib.__version__)2.變量與保留字
2.1??變量
變量相當于一個內存容器,可以指定存入不同的數據類型,可以是整數,小數或字符。
#Jupyter notebook打印多個變量結果 from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity='all'使用如上的代碼可以使得變量結果多行顯示。
name = "大話數據分析" # 字符串 age = 18 # 賦值整型變量 height = 178.4 # 浮點型 name age height2.2??保留字
Python中的保留字不能用作變量名稱,常見的Python保留字如下所示。
import keyword print(keyword.kwlist)['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
3. 三大數據類型
3.1??str
3.1.1??初識字符串
# 字符串定義 a = 'hello world' a'hello world'
str
Out[82]:11
3.1.2??索引和切片
使用[頭下標:尾下標]來切片,其中下標是從 0 開始算起,切片的范圍前閉后開,表示方括號的左邊可以切切到,而右邊切不到。
# 切片索引 a[0:5],a[:5]('hello', 'hello')
a[:],a[::1]('hello world', 'hello world')
#負號代表從右邊開始截取,這里表示取反 a[::-1]'dlrow olleh'
3.1.3??方法
'-'.join(a) a.replace('world','boy') a.zfill(15)#返回指定長度的字符串,原字符串右對齊,前面填充0today=['2015','10','15'] print("-".join(today))'h-e-l-l-o- -w-o-r-l-d'
Out[6]:'hello boy'
Out[6]:'0000hello world'
2015-10-15
a.count('o') #字符串計數 a.index('o') #字符串索引 a.find('o') #字符串查找 a.capitalize()#首字母大寫 a.title() #設置為標題 a.upper() #字母大寫 a.lower() #字母小寫 a.startswith('h') #開頭包含的字符2
Out[70]:4
Out[70]:4
Out[70]:'Hello world'
Out[70]:'Hello World'
Out[70]:'HELLO WORLD'
Out[70]:'hello world'
Out[70]:True
s = '**2021/12/16**' s.strip('*')#去除頭和尾部字符 s.lstrip('*')#去除左邊字符 s.rstrip('*')#去除右邊字符 s.strip('*').split('/') #去除頭和尾部字符,并按照/分隔開'2021/12/16'
'2021/12/16**'
'**2021/12/16'
['2021', '12', '16']
3.1.4??字符運算
# 運算符運算:+ 和 * s = '大話數據分析' 'Hello' + ' ' + s s * 3'Hello 大話數據分析'
'大話數據分析大話數據分析大話數據分析'
3.2??int
num = 10 print(type(num)) #基本的算術運算 print('加法:',num + 2) print('減法:',num - 2) print('乘法:',num * 2) print('除法',num / 2) print('地板除法',num // 2) print('冪運算',num ** 2) print('余數',num % 2)加法: 12
減法: 8
乘法: 20
除法 5.0
地板除法 5
冪運算 100
余數 0
#算術運算的順序,先計算括號里邊的內容,再乘除后加減 print(num * (2 + 1))30
11
11
50
50
3.3??float
num = 10.01 print(type(num))<class 'float'>
#基本的算術運算 print('加法:',num + 2) print('減法:',num - 2) print('乘法:',num * 2) print('除法',num / 2) print('地板除法',num // 2) print('冪運算',num ** 2) print('余數',num % 2)加法: 12.01
減法: 8.01
乘法: 20.02
除法 5.005
地板除法 5.0
冪運算 100.20009999999999
余數 0.009999999999999787
#算術運算的順序,先計算括號里邊的內容,再乘除后加減 print(num * (2 + 1))30.03
11.0
11.0
50.0
50.0
3.4 ?類型轉化
str,int,float數據類型相互轉化。
#將string內容為數字,字符串相連 num1 = '10' num2 = '20' num3 = '30.0' print('字符串相連:',num1+num2+num3)#使用int()函數將字符型轉換為int,float函數將字符型轉換為float num1_int = int(num1) num2_int = int(num2) num3_int = int(float(num3)) print('數值相加:',num1_int + num2_int + num3_int)字符串相連:102030.0
數值相加:60
4. 三大數據結構
4.1??列表
4.1.1??初識列表
列表是 Python 中使用最頻繁的數據類型,列表中的每個元素都可變的,可以對每個元素進行修改和刪除,且列表是有序的,每個元素的位置是確定的,可以用索引去訪問每個元素,并且,列表中的元素可以是Python中的任何對象,比如字符串、整數、元組、也可以是list等Python中的對象。
# 列表定義 lst = [1,2,3,4,5,6,7,8,9] lst[1, 2, 3, 4, 5, 6, 7, 8, 9]
lst = list(range(5)) lst[0, 1, 2, 3, 4]
# 類型和長度 type(lst) len(lst)4.1.2??索引和切片
使用[頭下標:尾下標]來截取部分字符串,其中下標是從 0 開始算起,可以是正數或負數,下標可以為空表示取到頭或尾。
# 索引 lst = [1,2,3,4,5,6,7,8,9] lst[0] lst[-1]9
# 切片 lst[0:4] lst[:4] lst[0:4:1] lst[:] lst[::-1][0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[4, 3, 2, 1, 0]
lst[::1] #缺省為1,默認間隔一個位置提取 lst[::2] #步長為 2(間隔一個位置)來截取[0, 1, 2, 3, 4]
[0, 2, 4]
4.1.3 方法
# append,列表末尾添加新的對象 lst = [0,1,2,3,4] lst.append(5) lst[0, 1, 2, 3, 4, 5]
#extend合并列表內容 lst = [0,1,2,3,4] lst.extend([5]) lst[0, 1, 2, 3, 4, 5]
#insert指定位置插入數據 lst = [0,1,2,3,4] lst.insert(1,5) lst[0, 5, 1, 2, 3, 4]
lst = [0,1,2,3,4] lst.copy()[0, 1, 2, 3, 4]
# remove lst = [0,1,2,3,4] lst.remove(4) lst[0, 1, 2, 3]
# count,index lst = [7,8,5,4,3,3,5,6,7,5] lst.count(5) lst.index(3)3
4
# 默認升序排列 lst = [7,8,5,4,3,3,5,6,7,5] lst.sort() lst[3, 3, 4, 5, 5, 5, 6, 7, 7, 8]
4.1.4 列表運算
# 運算符運算:+ 和 * lst = [1,2,3,4] lst + [3,4,5] lst * 2[1, 2, 3, 4, 3, 4, 5]
[1, 2, 3, 4, 1, 2, 3, 4]
4.2??元組
元組可理解為一個固定列表,一旦初始化其中的元素便不可修改,只能對元素進行查詢。
4.2.1??初識元組
# 元組的定義 t = (0, 1, 2, 3, 4) t(0, 1, 2, 3, 4)
t = tuple(range(5)) t(0, 1, 2, 3, 4)
# 屬性和長度 type(t) len(t)tuple
Out[8]:5
4.2.2??索引和切片
# 索引 t = (0, 1, 2, 3, 4) t[0] t[1] t[-1]0
1
4
# 切片 t = (0, 1, 2, 3, 4) t[0:4] t[:4] t[0:4:1](0, 1, 2, 3)
Out[10]:(0, 1, 2, 3)
Out[10]:(0, 1, 2, 3)
t = (0, 1, 2, 3, 4) t[:] t[::-1](0, 1, 2, 3, 4)
Out[14]:(4, 3, 2, 1, 0)
t = (0, 1, 2, 3, 4) t[::1] t[::2](0, 1, 2, 3, 4)
Out[13]:(0, 2, 4)
# count---index t = (2, 1, 2, 4, 2) t.count(2) t.index(2) t.index(4)3
Out[16]:0
Out[16]:3
4.2.3??元組運算
# 運算符運算:+ 和 * t = (1, 2, 3, 4) t + (4,5,6) t * 2(1, 2, 3, 4, 4, 5, 6)
Out[17]:(1, 2, 3, 4, 1, 2, 3, 4)
4.3?字典
字典中的數據必須以鍵值對的形式出現,其中,鍵是唯一的,不可重復,值可重復,字典中鍵(key)是不可變的,為不可變對象,不能進行修改;而值(value)是可以修改的,可以是任何對象。
4.3.1??初識字典
# 字典的定義: 鍵值對 d = {'a':10,'b':20,'c':30} d{'a': 10, 'b': 20, 'c': 30}
# 屬性和長度 d = {'a':10,'b':20,'c':30} type(d) len(d)4.3.2?索引和切片
# 索引 d = {'a':10,'b':20,'c':30} d['b']20
4.3.3?方法
# keys---values---items d = {'a':10,'b':20,'c':30} d.keys() d.values() d.items()dict_keys(['a', 'b', 'c'])
Out[24]:dict_values([10, 20, 30])
Out[24]:dict_items([('a', 10), ('b', 20), ('c', 30)])
# update d = {'a':10,'b':20,'c':30} d.update({'d':40}) d# pop d = {'a':10,'b':20,'c':30} d.pop('a')# get d = {'a':10,'b':20,'c':30} d.get('b')5.??三大控制流
5.1??if語句
當 if "判斷條件" 成立時,則執行后面的語句,else 為可選語句,當條件不成立時可以執行該語句。
#if...else語句 score = 60 if?score?<60:print('不及格')print('還需要在努力!') else:print('很棒!及格了')很棒!及格了
成績:中等
5.2??while語句
在某特定條件下,循環執行某命令。
#while a = 0 i = 100while i < 100:a=a+ii=i+1 print(a)5050
5.3 while……else……語句
else中的語句會在循環正常執行完(即不是通過break跳出而中斷的)的情況下執行。
#while…else… a = 1 b?=?1while a > b:print(a) else:print('數值大小相等')數值大小相等
數值大小相等
5.4?for 循環語句
對集合(如列表或元組)或迭代器進行迭代。
range函數用于產生一組間隔相等的整數序列的可迭代對象,可以指定起始值、終止值以及步長,常用于按索引對序列進行迭代。
a=0 for i in range(1,101):a = a+i print(a)5050
#for for i in range(1,5):print(i * 10)10
20
30
40
#for…if…else… for i in range(1,5):if i < 3:print(str(i) + 'Python')else:print(str(i) + 'Java')1Python
2Python
3Java
4Java
for i in range(1,5):print(i)if i > 2:break1
2
3
5.5循環控制語句
break:結束(終止)循環
continue:中止當前循環,跳到下一次循環的開始
while true/break:實現一個永遠不會自己停止的循環
else:在使用break時,可以使用else語句在沒有調用break時執行對應的語句
pass:不做任何事情,一般用做占位語句
-?END -
對比Excel系列圖書累積銷量達15w冊,讓你輕松掌握數據分析技能,可以在全網搜索書名進行了解: 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Python入门知识点总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 想戒都戒不掉?这届年轻人培养爱好确实有点
- 下一篇: 120Hz高刷+44W快充!千元新机vi