Python入门知识点总结
Python基礎(chǔ)的重要性不言而喻,是每一個(gè)入門Python學(xué)習(xí)者所必備的知識(shí)點(diǎn),作為Python入門,這部分知識(shí)點(diǎn)顯得很龐雜,內(nèi)容分支很多,大部分同學(xué)在剛剛學(xué)習(xí)時(shí)一頭霧水。
本節(jié)將Python的知識(shí)點(diǎn)進(jìn)行總結(jié)與歸納,節(jié)選部分在數(shù)據(jù)分析過程中用到比較多的一些知識(shí),例如字符串、列表、元組、字典等的用法,以及控制流if、for、while的用法,下面一起來學(xué)習(xí)。
Python 是一種解釋型、面向?qū)ο蟆?dòng)態(tài)數(shù)據(jù)類型的高級(jí)程序設(shè)計(jì)語言。Python基礎(chǔ)知識(shí)包含Python數(shù)據(jù)類型,數(shù)據(jù)結(jié)構(gòu),控制流等,與其他高級(jí)語言類似,順序語句、條件語句、循環(huán)語句等是其基本結(jié)構(gòu)。
1.Python基本命令
1.1? 列出已安裝的包
pip list1.2??查看可升級(jí)的包
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??升級(jí)包
pip install --upgrade SomePackage # 升級(jí)至最新版本 pip install --upgrade SomePackage==1.5.0 # 升級(jí)為指定版本1.6??卸載包
pip uninstall SomePackage#導(dǎo)入sys庫(kù)只是為了確認(rèn)一下Python的版本 import sys #導(dǎo)入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??變量
變量相當(dāng)于一個(gè)內(nèi)存容器,可以指定存入不同的數(shù)據(jù)類型,可以是整數(shù),小數(shù)或字符。
#Jupyter notebook打印多個(gè)變量結(jié)果 from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity='all'使用如上的代碼可以使得變量結(jié)果多行顯示。
name = "大話數(shù)據(jù)分析" # 字符串 age = 18 # 賦值整型變量 height = 178.4 # 浮點(diǎn)型 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. 三大數(shù)據(jù)類型
3.1??str
3.1.1??初識(shí)字符串
# 字符串定義 a = 'hello world' a'hello world'
str
Out[82]:11
3.1.2??索引和切片
使用[頭下標(biāo):尾下標(biāo)]來切片,其中下標(biāo)是從 0 開始算起,切片的范圍前閉后開,表示方括號(hào)的左邊可以切切到,而右邊切不到。
# 切片索引 a[0:5],a[:5]('hello', 'hello')
a[:],a[::1]('hello world', 'hello world')
#負(fù)號(hào)代表從右邊開始截取,這里表示取反 a[::-1]'dlrow olleh'
3.1.3??方法
'-'.join(a) a.replace('world','boy') a.zfill(15)#返回指定長(zhǎng)度的字符串,原字符串右對(duì)齊,前面填充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') #字符串計(jì)數(shù) a.index('o') #字符串索引 a.find('o') #字符串查找 a.capitalize()#首字母大寫 a.title() #設(shè)置為標(biāo)題 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??字符運(yùn)算
# 運(yùn)算符運(yùn)算:+ 和 * s = '大話數(shù)據(jù)分析' 'Hello' + ' ' + s s * 3'Hello 大話數(shù)據(jù)分析'
'大話數(shù)據(jù)分析大話數(shù)據(jù)分析大話數(shù)據(jù)分析'
3.2??int
num = 10 print(type(num)) #基本的算術(shù)運(yùn)算 print('加法:',num + 2) print('減法:',num - 2) print('乘法:',num * 2) print('除法',num / 2) print('地板除法',num // 2) print('冪運(yùn)算',num ** 2) print('余數(shù)',num % 2)加法: 12
減法: 8
乘法: 20
除法 5.0
地板除法 5
冪運(yùn)算 100
余數(shù) 0
#算術(shù)運(yùn)算的順序,先計(jì)算括號(hào)里邊的內(nèi)容,再乘除后加減 print(num * (2 + 1))30
11
11
50
50
3.3??float
num = 10.01 print(type(num))<class 'float'>
#基本的算術(shù)運(yùn)算 print('加法:',num + 2) print('減法:',num - 2) print('乘法:',num * 2) print('除法',num / 2) print('地板除法',num // 2) print('冪運(yùn)算',num ** 2) print('余數(shù)',num % 2)加法: 12.01
減法: 8.01
乘法: 20.02
除法 5.005
地板除法 5.0
冪運(yùn)算 100.20009999999999
余數(shù) 0.009999999999999787
#算術(shù)運(yùn)算的順序,先計(jì)算括號(hào)里邊的內(nèi)容,再乘除后加減 print(num * (2 + 1))30.03
11.0
11.0
50.0
50.0
3.4 ?類型轉(zhuǎn)化
str,int,float數(shù)據(jù)類型相互轉(zhuǎn)化。
#將string內(nèi)容為數(shù)字,字符串相連 num1 = '10' num2 = '20' num3 = '30.0' print('字符串相連:',num1+num2+num3)#使用int()函數(shù)將字符型轉(zhuǎn)換為int,float函數(shù)將字符型轉(zhuǎn)換為float num1_int = int(num1) num2_int = int(num2) num3_int = int(float(num3)) print('數(shù)值相加:',num1_int + num2_int + num3_int)字符串相連:102030.0
數(shù)值相加:60
4. 三大數(shù)據(jù)結(jié)構(gòu)
4.1??列表
4.1.1??初識(shí)列表
列表是 Python 中使用最頻繁的數(shù)據(jù)類型,列表中的每個(gè)元素都可變的,可以對(duì)每個(gè)元素進(jìn)行修改和刪除,且列表是有序的,每個(gè)元素的位置是確定的,可以用索引去訪問每個(gè)元素,并且,列表中的元素可以是Python中的任何對(duì)象,比如字符串、整數(shù)、元組、也可以是list等Python中的對(duì)象。
# 列表定義 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]
# 類型和長(zhǎng)度 type(lst) len(lst)4.1.2??索引和切片
使用[頭下標(biāo):尾下標(biāo)]來截取部分字符串,其中下標(biāo)是從 0 開始算起,可以是正數(shù)或負(fù)數(shù),下標(biāo)可以為空表示取到頭或尾。
# 索引 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,默認(rèn)間隔一個(gè)位置提取 lst[::2] #步長(zhǎng)為 2(間隔一個(gè)位置)來截取[0, 1, 2, 3, 4]
[0, 2, 4]
4.1.3 方法
# append,列表末尾添加新的對(duì)象 lst = [0,1,2,3,4] lst.append(5) lst[0, 1, 2, 3, 4, 5]
#extend合并列表內(nèi)容 lst = [0,1,2,3,4] lst.extend([5]) lst[0, 1, 2, 3, 4, 5]
#insert指定位置插入數(shù)據(jù) 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
# 默認(rèn)升序排列 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 列表運(yùn)算
# 運(yùn)算符運(yùn)算:+ 和 * 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??元組
元組可理解為一個(gè)固定列表,一旦初始化其中的元素便不可修改,只能對(duì)元素進(jìn)行查詢。
4.2.1??初識(shí)元組
# 元組的定義 t = (0, 1, 2, 3, 4) t(0, 1, 2, 3, 4)
t = tuple(range(5)) t(0, 1, 2, 3, 4)
# 屬性和長(zhǎng)度 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??元組運(yùn)算
# 運(yùn)算符運(yùn)算:+ 和 * 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?字典
字典中的數(shù)據(jù)必須以鍵值對(duì)的形式出現(xiàn),其中,鍵是唯一的,不可重復(fù),值可重復(fù),字典中鍵(key)是不可變的,為不可變對(duì)象,不能進(jìn)行修改;而值(value)是可以修改的,可以是任何對(duì)象。
4.3.1??初識(shí)字典
# 字典的定義: 鍵值對(duì) d = {'a':10,'b':20,'c':30} d{'a': 10, 'b': 20, 'c': 30}
# 屬性和長(zhǎng)度 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語句
當(dāng) if "判斷條件" 成立時(shí),則執(zhí)行后面的語句,else 為可選語句,當(dāng)條件不成立時(shí)可以執(zhí)行該語句。
#if...else語句 score = 60 if?score?<60:print('不及格')print('還需要在努力!') else:print('很棒!及格了')很棒!及格了
成績(jī):中等
5.2??while語句
在某特定條件下,循環(huán)執(zhí)行某命令。
#while a = 0 i = 100while i < 100:a=a+ii=i+1 print(a)5050
5.3 while……else……語句
else中的語句會(huì)在循環(huán)正常執(zhí)行完(即不是通過break跳出而中斷的)的情況下執(zhí)行。
#while…else… a = 1 b?=?1while a > b:print(a) else:print('數(shù)值大小相等')數(shù)值大小相等
數(shù)值大小相等
5.4?for 循環(huán)語句
對(duì)集合(如列表或元組)或迭代器進(jìn)行迭代。
range函數(shù)用于產(chǎn)生一組間隔相等的整數(shù)序列的可迭代對(duì)象,可以指定起始值、終止值以及步長(zhǎng),常用于按索引對(duì)序列進(jìn)行迭代。
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循環(huán)控制語句
break:結(jié)束(終止)循環(huán)
continue:中止當(dāng)前循環(huán),跳到下一次循環(huán)的開始
while true/break:實(shí)現(xiàn)一個(gè)永遠(yuǎn)不會(huì)自己停止的循環(huán)
else:在使用break時(shí),可以使用else語句在沒有調(diào)用break時(shí)執(zhí)行對(duì)應(yīng)的語句
pass:不做任何事情,一般用做占位語句
-?END -
對(duì)比Excel系列圖書累積銷量達(dá)15w冊(cè),讓你輕松掌握數(shù)據(jù)分析技能,可以在全網(wǎng)搜索書名進(jìn)行了解: 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Python入门知识点总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 想戒都戒不掉?这届年轻人培养爱好确实有点
- 下一篇: websocket python爬虫_p