日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python 从零学起(纯基础) 笔记 (二)

發(fā)布時(shí)間:2025/5/22 python 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 从零学起(纯基础) 笔记 (二) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Day02

?自學(xué)筆記


?1. ?對(duì)于Python,一切事物都是對(duì)象,對(duì)象基于類創(chuàng)建,對(duì)象具有的功能去類里找

name = ‘Young’???????- ??對(duì)象

Li1 = [11,22,33] ??????- ??對(duì)象

列表創(chuàng)建:

  Li = [11,22,33]

也可以這樣創(chuàng)建:

  Li = list(11,22,3)

字符串:

  • S = “fha”
  • S = str(‘dd’)
  • 以此類推......

    ?

    2. ?int 內(nèi)部功能介紹

      __init__ () 構(gòu)造方法

      比如:

      Age = int(19) ??#執(zhí)行__init__()方法?

      Age = 18

      Age.__add__(7)

      18+7

      Age.__divmod__(7)

      18/7 = (,余數(shù))

      Age.__rdivmod__(7)

      7/18 = (,余數(shù))

    ?

    3. ?str內(nèi)部功能介紹:

    ?

      __contains__() ?-- ???1

    1 my_string = 'young' 2 result = my_string.__contains__('you') 3 #result = 'you' in my_string 4 print(result)

      結(jié)果:

      True

      capitalize ? ? ?-- ? ? 2

    my_string = 'young' result = my_string.capitalize() print(result)

    結(jié)果:

    Young

      center ? ? ? ? ? -- ? ? 3

    1 name = 'Young' 2 result = name.center(40,'*') 3 print(result)

    結(jié)果:

    *****************Young******************

      count ??????????-- ? ? 4

    1 name = 'Youngyounggdgfak' 2 #result = name.count('ou') 3 result = name.count('ou',0,5) 4 print(result)

    結(jié)果:

    1

      encode ????????-- ? ? 5

    1 name = '雨紛紛' 2 result = name.encode('gbk') 3 print(result)

    結(jié)果:

    b'\xd3\xea\xb7\xd7\xb7\xd7'

      endswith ???????-- ???6

    name = 'Young' #result = name.endswith('g') result = name.endswith('un',0,4) # [0,4) print(result)

    結(jié)果:

    True

      expandtabs ?????-- ???7

    name = 'You\tng' result = name.expandtabs() print(result)

    結(jié)果:

    You ????ng

      find ? ? ? ? ? ? ? ?-- ? ? 8

    name = 'Young' #result = name.find('g')#返回所在索引的位置,沒(méi)有找到返回-1 result = name.find('un',0,4) # [0,4) print(result)

    結(jié)果:

    2

      index ? ? ? ? ? ?-- ? ? 9

    1 name = 'Young' 2 #result = name.index('g')#返回所在索引的位置,沒(méi)有找到報(bào)錯(cuò) 3 result = name.index('unc',0,4) # [0,4) 4 print(result)?

    結(jié)果:

    Traceback (most recent call last):

    ??File "E:/Pycharm/01_projects/day02/01_examples.py", line 52, in <module>

    ????result = name.index('unc',0,4) # [0,4)

    ValueError: substring not found

    ?

      format ?????????-- ???10

    1 name ="Young {0}" 2 result = name.format("good") 3 print(result)

    結(jié)果:

    Young ?good

    ?

    1 name ="Young {0} as {1}" 2 result = name.format("good",'hello') 3 print(result)

    結(jié)果:

    Young ?good as hello

    1 name ="Young {name} as {id}" 2 result = name.format(name="good",id='hello') 3 print(result)

    結(jié)果:

    Young ?good as hello

      類似與 %s ,強(qiáng)過(guò)‘+

      join ???????????-- ???11

    1 li = ['g','o','o','d'] 2 #result = "".join(li) 3 result = "_".join(li) 4 print(result)

    結(jié)果:

    #good

    g_o_o_d

      partition ???????-- ???12

    1 name ="Youngisgood" 2 result = name.partition('is')#以is將name分割 3 print(result)

    結(jié)果:

    ('Young', 'is', 'good')

      replace ????????-- ???13

    1 name ="Youngisgood" 2 #result = name.replace('o','m')#用 ‘m’ 替換 'o' 3 result = name.replace('o','m',2)#用 ‘m’ 替換 'o',轉(zhuǎn)換前兩個(gè) 4 print(result)

    結(jié)果:

    Ymungisgmod

      splitlines ???????-- ???14

    1 name =''' 2 hello 3 kitty 4 good 5 man 6 ''' 7 #result = name.split('\n')#指定字符分割 8 result = name.splitlines()#按行切割,根據(jù)‘\n’ 9 print(result)

    結(jié)果:

    ['', 'hello', 'kitty', 'good', 'man']

    ?4.?列表

    ? ? 列表的元素可以是列表

    ????字典的元素也可以是字典

      extend??

    1 li = [1,2,3] 2 print(li) 3 #li.extend([4,5]) 4 li.extend((4,5,))# 5后加上個(gè)逗號(hào),大家默認(rèn)的 5 print(li)

      結(jié)果:

      [1, 2, 3]

      [1, 2, 3, 4, 5]

      ?pop

    1 li = [1,2,3] 2 print(li) 3 ret = li.pop() # ret 等于被刪除的那個(gè)數(shù) 4 print(li) 5 print(ret)

      結(jié)果:

      [1, 2, 3]

      [1, 2]

      3

    1 li = [1,2,3] 2 print(li) 3 ret = li.pop(1) #pop()里是下標(biāo) 4 print(li) 5 print(ret)

      結(jié)果:

      [1, 2, 3]

      [1, 3]

      2

      remove 和 reverse

    1 li = [11,11,2,3,99] 2 print(li) 3 li.remove(11)# 第一個(gè)11 被拿走 4 print(li) 5 li.reverse() 6 print(li)

    ?5. ??元組

      T1= (12{‘k1’:’v1’})

      元組的元素不可以變,但元組的元素的元素可修改

    1 t1 = (1,2,{'k1':'v1'}) 2 #del t1[0] 3 #t1[2] = 123 #t1[2] 字典元素 4 t1[2]['k1'] = 2 5 print(t1)

    ?6. ? 文件操作

    讀寫方式 ? ? ? ? ? ? ?1

    ??    r+ ??讀寫

    ???   ?w+ ?寫讀

    read() ? ? ? ? ? ? ? ? ? ? ? ?2

    (注:read是按字符來(lái)執(zhí)行)

    1 #coding=utf-8 2 __author__ = 'Young' 3 f =open('test.log','r') 4 #f.write('無(wú)hadksfh') # 先寫,后讀 5 ret = f.read(2) 6 f.close() 7 print(ret)

    結(jié)果:

    無(wú)華

    tell() ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3

    (注:tell按照字節(jié)來(lái)執(zhí)行的,read是按字符來(lái)執(zhí)行)

    1 f =open('test.log','r') 2 print(f.tell()) 3 ret = f.read(2) #加參數(shù)指定讀取字符 4 print(f.tell()) 5 f.close()

    結(jié)果:

    0

    4

    seek 和 truncate ? ? ? ? ?4

    (注:tell用來(lái)查看當(dāng)前指針位置 ?seek用來(lái)指定當(dāng)前指針位置 ,truncate截取數(shù)據(jù),只保留指針之前的數(shù)據(jù))

    1 f =open('test.log','r+') 2 f.seek(4) 3 print(f.tell()) 4 #print(f.read()) 5 f.truncate() #截取數(shù)據(jù),只保留指針之前的數(shù)據(jù) 6 print(f.tell()) 7 f.close()

    結(jié)果:

    4

    4

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/Bro-Young/p/5793844.html

    總結(jié)

    以上是生活随笔為你收集整理的Python 从零学起(纯基础) 笔记 (二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。