接觸python已有一段時(shí)間了,下面針對(duì)python基礎(chǔ)知識(shí)的使用做一完整梳理:
1)避免‘/n'等特殊字符的兩種方式:
a)利用轉(zhuǎn)義字符‘/' b)利用原始字符‘r' print r'c:/now'
2)單行注釋?zhuān)褂靡粋€(gè)#,如:
#hello Python 多行注釋?zhuān)褂萌齻€(gè)單引號(hào)(或三個(gè)雙引號(hào)),如: '''hello python hello world''' 或 """hello python hello world""" 另外跨越多行的字符串。也可以使用三個(gè)單引號(hào)或三個(gè)雙引號(hào),如: '''......'''或者 """......"""
3)字符串中嵌入雙引號(hào)等特殊符號(hào)
a)利用轉(zhuǎn)義字符‘/' b)使用單引號(hào)括起這個(gè)字符串。print ('i l"o"ve fis.com')
4)條件分支:
if condition: 條件為真執(zhí)行的操作 else: 條件為假執(zhí)行的操作 if condition: action elif condition: action else: action python可以有效避免“懸掛else”(if else對(duì)應(yīng)關(guān)系出錯(cuò)) 條件表達(dá)式(三元操作符) small = x if x<y else y 如果x<y ,small=x.否則small=y 斷言assert:當(dāng)這個(gè)關(guān)鍵字后面的條件為假,程序自動(dòng)崩潰并拋出異常 assert 3>4 可以利用他置入檢查點(diǎn)
5)while條件:
條件為真執(zhí)行的操作 for 目標(biāo) in 表達(dá)式: 循環(huán)體 例:favorite='fishc' for i in favorite: print(i,end='') range([start,] stop[,step=1]) 生成一個(gè)從start參數(shù)的值到stop參數(shù)值的數(shù)字序列 break:終止當(dāng)前循環(huán)體。跳到外層程序 continue:終止本輪循環(huán),開(kāi)始下一輪循環(huán)(if condition true)
6)and邏輯操作符可以將任意表達(dá)式連接在一起,并得到一個(gè)布爾類(lèi)型值
7)引入外援:
a)random模塊 b)randint(),返回一個(gè)隨機(jī)的整數(shù) import random 或 from random import randint() secret=random.randint(1,10)
8)python數(shù)據(jù)類(lèi)型
a)數(shù)值類(lèi)型:整型、布爾類(lèi)型、浮點(diǎn)型、e記法(1.5e10) b)類(lèi)型轉(zhuǎn)換: int()轉(zhuǎn)換為整數(shù) str()轉(zhuǎn)換為字符串 float()轉(zhuǎn)換為浮點(diǎn)數(shù) c)獲取關(guān)于類(lèi)型的信息: type()函數(shù) a=520 type(a) isinstance()函數(shù) a=12 isinstance(a,int) --->返回true isinstance(a,str) -->返回false
9)Python值常用操作符
+ - * / % **(冪運(yùn)算) //(地板除法,結(jié)果偏小) 比較操作符 > < >= <= 邏輯操作符 and or not 優(yōu)先級(jí): 冪運(yùn)算** 正負(fù)號(hào) + - 算術(shù)操作符 * / // + - 比較操作符 < > = 邏輯擦作福 not and or
10)列表-->可以把整數(shù)、浮點(diǎn)數(shù)、字符串等打包在一起。數(shù)組卻不能
創(chuàng)建一個(gè)普通列表: member = ['小甲魚(yú)','小布丁','黑夜'] 創(chuàng)建一個(gè)混合列表: mix=[1,'小甲魚(yú)',3.12,[1,2,3]] 創(chuàng)建空列表: empty=[] 向列表添加元素: append(): member.append('福祿娃')-->只能添加一個(gè)。末尾添加 extend(): member.extend(['test','test1'])-->只能以列表形式添加.末尾添加 insert(): member.insert(1,'牡丹')-->第一位插入牡丹 列表中獲取元素:使用索引index。 mix[1] 列表中刪除元素:使用remove()。 mix.remove('小甲魚(yú)') 使用del。 del mix[3]/mix 使用pop()。 mix.pop()/mix.pop(1) 列表切片:使用slice。 mix[1:4]/mix[1:]/mix[:4] 列表操作符:>,and,+,*,in/not in列表的小伙伴:dir(list) mix.count('小甲魚(yú)') mix.index('小甲魚(yú)') 列表逆序:使用reverse。 mix.reverse() 列表排序:使用sort。 mix.sort() mix.sort(func,key) mix.sort(reverse=True)
新聞熱點(diǎn)
疑難解答
圖片精選