生活随笔
收集整理的這篇文章主要介紹了
【Python】学习笔记总结1(Python基础)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄 一、Python基礎(chǔ) 1.注釋 2.輸入輸出 3.數(shù)據(jù)類型 4.運(yùn)算符 4.1.算數(shù)運(yùn)算符 4.2.比較運(yùn)算符 4.3.邏輯運(yùn)算符 4.4.賦值運(yùn)算符 5. 判斷與循環(huán)語句 5. 1.If-else 5. 2.While 5. 3.for 5. 4.break 5. 5.continue 6. 高級數(shù)據(jù)類型 6. 1.字符串 6. 2.列表 6. 3.字典 6. 4.元組 7. 函數(shù) 7.1.參數(shù) 7.1.1.必選參數(shù) 7.1.2.默認(rèn)參數(shù) 7.1.3.可選參數(shù):元組類型,參數(shù)個數(shù)不確定 7.1.4.關(guān)鍵字參數(shù):字典類型,key為字符串 7.2.返回值 7.3.局部變量與全局變量 7.4.函數(shù)參數(shù)引用傳值 7.5.匿名函數(shù)(lambda表達(dá)式) 7.6.遞歸函數(shù) 8. 內(nèi)置函數(shù) 8.1.數(shù)學(xué)函數(shù) 8.2.類型轉(zhuǎn)換函數(shù) 8.3.序列操作函數(shù) 8.4.集合Set
一、Python基礎(chǔ)
1.注釋
注釋代碼 單行注釋 #內(nèi)容 多行注釋 '''內(nèi)容'''
python2需要指定python解析器的路徑,指定編碼格式
2.輸入輸出
輸入輸出代碼 普通輸入 content = input('請輸入內(nèi)容')——默認(rèn)為str類型 普通輸出 print('內(nèi)容') 格式化輸出1 print('姓名:%s:\n 年齡:%d'%(name,age)) 格式化輸出2 print('姓名:{} \n 年齡:{}'.format(name,age))
格式符號轉(zhuǎn)換 %c 字符 %s 通過str()字符串轉(zhuǎn)換來格式化 %i 有符號十進(jìn)制整數(shù) %d 有符號十進(jìn)制整數(shù) %u 無符號十進(jìn)制整數(shù) %o 八進(jìn)制整數(shù) %x 十六進(jìn)制整數(shù)(小寫字母) %e 索引符號(小寫’e‘) %E 索引符號(大寫’E‘) %f 浮點(diǎn)實(shí)數(shù) %g %f和%e的簡寫 %G %f和%E的簡寫
3.數(shù)據(jù)類型
數(shù)據(jù)類型代碼 int a = 10 float a = 10.5 complex a = 10j bool a = true str a = '內(nèi)容' dict dict = {"name":王一,"age":20} Tuple tupleA = (1,2,5,'王一') list listA = [1,2,5,'王一']
4.運(yùn)算符
4.1.算數(shù)運(yùn)算符
算數(shù)運(yùn)算符代碼 + sum = a+b - sum = a-b * sum = a*b / sum = a/b ** sum = a**2 % sum = a%2 // sum = a//2
4.2.比較運(yùn)算符
比較運(yùn)算符代碼 == a == b != a != b > a > b < a < b >= a >= b <= a <= b
4.3.邏輯運(yùn)算符
邏輯運(yùn)算符代碼 and a == 10 and b == 10 or a == 10 or b == 10 not not a == 10
4.3.1.短路運(yùn)算
簡單的說:
表達(dá)式用and的則將容易為False的條件放到前面,
表達(dá)式有or的則將容易為True的條件放到前面
詳細(xì)介紹大家可百度查看
4.4.賦值運(yùn)算符
賦值運(yùn)算符代碼 = a = b += a += b -= a -= b *= a *= b /= a /= b %= a %= 2 **= a **= 2 //= a //= 2
5. 判斷與循環(huán)語句
5. 1.If-else
num
= int ( input ( "輸入一個數(shù)字:" ) )
if num
> 2 : if num
> 3 : print ( '數(shù)大于3' ) else : print ( '數(shù)大于2且小于等于3' )
elif num
< 2 : print ( '數(shù)小于2' )
else : print ( '數(shù)等于2' )
5. 2.While
i
= 9
while i
>= 1 : j
= 1 while j
<= i
: print ( '%d*%d=%2d\t' % ( j
, i
, i
* j
) , end
= '' ) j
+= 1 i
-= 1 print ( )
5. 3.for
for i
in range ( 0 , 10 ) : for j
in range ( 0 , i
+ 1 ) : print ( '%d * %d = %d' % ( i
, j
, i
* j
) , end
= ' ' ) print ( )
5. 4.break
import random
win
= 0
while True : if win
>= 3 : print ( '你已經(jīng)勝利三次' ) break else : inp
= int ( input ( '請出拳:' ) ) computer
= random
. randint
( 0 , 2 ) print ( '電腦出拳:%d' % computer
) if inp
> 2 : print ( '輸入錯誤' ) elif ( inp
== 0 and computer
== 2 ) or \
( inp
== 1 and computer
== 0 ) \
or ( inp
== 2 and computer
== 1 ) : print ( "厲害了,居然贏了" ) win
+= 1 elif inp
== computer
: print ( '不錯,居然打平了' ) else : print ( '呵呵,輸了吧' )
5. 5.continue
a
= 'python'
for i
in a
: if i
== 'h' continue print ( i
)
6. 高級數(shù)據(jù)類型
公有方法作用代碼 + 兩個對象相加操作,會合并兩個對象 - * 對象自身按指定次數(shù)進(jìn)行 + 操作 - in 判斷指定元素是否存在于對象中 -
6. 1.字符串
strA
= 'abc123'
常用方法作用代碼 [起始下標(biāo):結(jié)束下標(biāo):步長] 字符串切片 strA[2:4] capitalize() 首字母變大寫 - isalnum() 判斷是否是字母和數(shù)字 - islower() 判斷是否是小寫 - swapcase() 大寫變小寫,小寫變大寫 - title() 把每個單詞的首字母變成大寫 - endswith/startswith() 是否 x結(jié)束/開始 - isalpha() 判斷是否是字母 - join() 循環(huán)取出所有值用xx去連接 - lstrip/rstrip/strip 移除左/右/兩側(cè)空白 - replace(old, new, count=None) old被換字符串,new替換字符串,count換多少個。無count表示全部替換 - find() 檢測x是否在字符串中 - isdigit() 判斷是否是數(shù)字 'abc123’.isdigit() lower/upper 大小寫轉(zhuǎn)換 - split() 切割字符串 - count() 統(tǒng)計(jì)出現(xiàn)的次數(shù) -
6. 2.列表
listA
= [ 'a' , 'b' , 'c' , 1 , 2 , 3 ]
常用方法作用代碼 [起始下標(biāo):結(jié)束下標(biāo):步長] 字符串切片 listA[2:4] append() 在列表后面追加元素 - count() 統(tǒng)計(jì)元素出現(xiàn)的次數(shù) - extend() 擴(kuò)展,相當(dāng)于批量添加 - index() 獲取指定元素索引號 - insert() 在指定位置插入 - pop() 刪除最后一個元素 - remove() 移除左邊找到的第一個元素 - reverse() 反轉(zhuǎn)列表 - sort() 列表排序(reverse=True 倒序) -
6. 3.字典
scores
= [ ( '英語' , 58 ) , ( '政治' , 79 ) ]
常用方法作用代碼 scores[‘語文’] 通過key訪問value scores['celery'] scores[‘?dāng)?shù)學(xué)’] = 93 修改/增加key-value對 scores['數(shù)學(xué)'] = 93 del 刪除key-value 對 del scores['數(shù)學(xué)'] in 判斷是否包換鍵 '數(shù)學(xué)' in scores len() 查看字典中有幾個鍵值對 - keys 返回包含字典所有key值 - values 返回一個包含所有value值 - items 返回一個包含所有(鍵,值)元祖的列表 - pop(‘鍵’) 刪除指定鍵 -
6. 4.元組
tupleA
= ( 'a' , 'b' , 'c' , 1 , 2 , 3 )
常用方法作用代碼 [起始下標(biāo):結(jié)束下標(biāo):步長] 字符串切片 listA[2:4] index() 獲取指定元素索引號 - count() 統(tǒng)計(jì)元素出現(xiàn)的次數(shù) -
7. 函數(shù)
7.1.參數(shù)
參數(shù):必選參數(shù)、默認(rèn)參數(shù)[缺省參數(shù)]、可選參數(shù)、關(guān)鍵字參數(shù)
7.1.1.必選參數(shù)
def Sum ( a
, b
) : '''打印函數(shù):return:''' sum1
= a
+ b
; print ( sum1
)
7.1.2.默認(rèn)參數(shù)
def Sum ( a
= 0 , b
= 1 ) : '''打印函數(shù):return:''' sum1
= a
+ b
print ( sum1
)
7.1.3.可選參數(shù):元組類型,參數(shù)個數(shù)不確定
def Sum ( * args
) : '''計(jì)算累加和:param args: 可變長參數(shù)類型:return:''' sum1
= 0 for item
in args
: sum1
+= item
print ( sum1
)
7.1.4.關(guān)鍵字參數(shù):字典類型,key為字符串
def keyFunc ( ** kwargs
) : '''計(jì)算累加和:param args: 可變長參數(shù)類型:return:''' print ( kwargs
) dictA
= { '1' : 2 , '2' : 4 } keyFunc
( ** dictA
)
keyFunc
( name
= '2' , age
= '3' )
7.2.返回值
def Sum ( a
, b
) : '''相加:return:和''' sum1
= a
+ b
return sum1
7.3.局部變量與全局變量
注意:函數(shù)內(nèi)修改全局變量需要加關(guān)鍵字global
mark
= '1234'
print ( mark
)
def changeInfo ( ) : global markmark
= '5678' pass changeInfo
( )
print ( mark
)
7.4.函數(shù)參數(shù)引用傳值
python中,萬物皆對象,在函數(shù)調(diào)用時(shí),實(shí)參傳遞的就是對象的引用。 不可變類型(Number(數(shù)字)、String(字符串)、Tuple(元組))修改時(shí),修改的是存儲實(shí)際數(shù)據(jù)的引用,對原數(shù)據(jù)無影響 但可變類型(Set(集合)、List(列表)、Dictionary(字典))修改時(shí),修改的是同一地址下的數(shù)據(jù),對原數(shù)據(jù)有影響。 可以使用id()獲取數(shù)據(jù)地址
7.5.匿名函數(shù)(lambda表達(dá)式)
m
= lambda x
, y
: x
+ y
print ( m
( 1 , 2 ) ) age
= 15
print ( 'ok' if age
> 18 else 'no' ) m
= lambda x
, y
: x
if x
> y
else y
print ( m
( 1 , 2 ) ) m
= ( lambda x
, y
: x
if x
> y
else y
) ( 1 , 2 )
print ( m
)
7.6.遞歸函數(shù)
def jiecheng ( n
) : if n
== 1 : return 1 else : return n
* jiecheng
( n
- 1 ) print ( jiecheng
( 5 ) )
7.6.1.文件查找
import os
def findFile ( file_Path
) : listRs
= os
. listdir
( file_Path
) for item
in listRs
: full_path
= os
. path
. join
( file_Path
, item
) if os
. path
. isdir
( full_path
) : findFile
( full_path
) else : print ( item
+ '----------------------' ) else : return findFile
( 'E:\\umengok' )
8. 內(nèi)置函數(shù)
8.1.數(shù)學(xué)函數(shù)
函數(shù)作用 abs() 絕對值 round() 四舍五入,近似值 pow() 冪運(yùn)算 divmod() 返回一個包含商和余數(shù)的元組 max() 最大值 min() 最小值 sum() 求和 eval() 用來執(zhí)行一個字符串表達(dá)式,并返回表達(dá)式的值(動態(tài)執(zhí)行的函數(shù))
8.2.類型轉(zhuǎn)換函數(shù)
函數(shù)作用 int() 轉(zhuǎn)化為int類型 float() 轉(zhuǎn)化為float類型 str() 轉(zhuǎn)化為str類型 ord() 字符轉(zhuǎn)數(shù)字(ASCII) chr() 數(shù)字轉(zhuǎn)字符(ASCII) bool() 轉(zhuǎn)化為bool類型 bin() 十進(jìn)制轉(zhuǎn)化為二進(jìn)制 hex() 十進(jìn)制轉(zhuǎn)化為十六進(jìn)制 oct() 十進(jìn)制轉(zhuǎn)化為八進(jìn)制 list() 元組轉(zhuǎn)化為列表類型 tuple() 列表轉(zhuǎn)化為元組類型 dict() 創(chuàng)建字典 bytes() 轉(zhuǎn)化為字節(jié)數(shù)組
8.3.序列操作函數(shù)
函數(shù)作用 all() 判斷可迭代對象元素是否全部都為True(除了0,空,False都是) any() 判斷可迭代對象元素是否全部為False(0,空,False都是) sorted() 對可迭代對象進(jìn)行排序(返回新的list)(reverse參數(shù):升序降序) reverse() 反轉(zhuǎn)列表中元素 range() 創(chuàng)建一個整數(shù)列表 zip() 將可迭代對象打包壓縮為元組,返回元組列表(打包序列對應(yīng)索引存儲為一個元組,按照最少的元素那個進(jìn)行壓縮) enumerate() 可遍歷的數(shù)據(jù)對象組合為一個索引序列
8.4.集合Set
無序、不重復(fù)的數(shù)據(jù)結(jié)構(gòu)不支持索引和切片(類似于一個只有key的字典),作用去重
set1
= { 1 , 2 , 3 , 4 }
常用方法作用代碼 add() 添加元素 - clear() 清空集合 - difference() 兩個集合的差集(set1-set2) - intersection() 兩個集合的交集(set1&set2) - union() 兩個集合的并集(set1|set2) - pop() 移除數(shù)據(jù)刪除 - discard() 移除指定數(shù)據(jù)刪除 - update() 把set2更新到set1中 -
總結(jié)
以上是生活随笔 為你收集整理的【Python】学习笔记总结1(Python基础) 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。