>>?2**532第二種方法用內(nèi)置的 pow方法>>>?pow(2,?5)32第三種方法用 math 模塊里的 pow 方法>>>?import?math>..." />
<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          90% 人會做錯常見的10道Python面試題

          共 2287字,需瀏覽 5分鐘

           ·

          2020-07-28 12:37



          閱讀文本大概需要 5?分鐘


          作者:pk哥
          公眾號:Python知識圈

          用三種方法寫出次方計算

          第一種方法是用運算符 **

          >>>?2**5
          32

          第二種方法用內(nèi)置的 pow方法

          >>>?pow(2,?5)
          32

          第三種方法用 math 模塊里的 pow 方法

          >>>?import?math
          >>>?math.pow(2,5)
          32.0

          怎么讓字符串居中

          用字符串中的 center 方法,他會在兩邊自動填充字符(默認為空格),讓字符串居中

          >>>?k?=?'更多精彩,請關注公眾號「Python知識圈」'
          >>>?k.center(50)
          '??????????????更多精彩,請關注公眾號「Python知識圈」??????????????'
          >>>?k.center(50,?'*')
          '**************更多精彩,請關注公眾號「Python知識圈」**************'

          用兩種方法讓字符的首字母大寫,其他字母小寫

          解法1:用 title 方法。

          >>>?ss?=?'welcome?to?pay?attention?to?my?weChat?official?accounts:?PythonCircle'
          >>>?ss.title()
          'Welcome?To?Pay?Attention?To?My?Wechat?Official?Accounts:?Pythoncircle'

          解法2:用 string 模塊里的 capwords 方法,記得先 import string

          >>>?ss?=?'welcome?to?pay?attention?to?my?weChat?official?accounts:?PythonCircle'
          >>>?string.capwords(ss)
          'Welcome?To?Pay?Attention?To?My?Wechat?Official?Accounts:?Pythoncircle'

          一個序列中隨機返回 n 個不同值的元素

          用 random 中的 sample 方法

          >>>?import?random
          >>>?t?=?(2020,?7,?3,?21,?48,?56,?4,?21,?0)
          >>>?random.sample(t,?2)
          [56,?0]

          一行代碼在等差數(shù)列中隨機選擇一個數(shù)

          用 random 中的 randrange 方法

          >>>?random.randrange(0,?100,?10)
          70

          快速隨機打亂列表的順序

          用 random 模塊里的 shuffle 方法

          >>>?import?random
          >>>?t?=?list(range(20))
          >>>?t
          [0,?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15,?16,?17,?18,?19]
          >>>?random.shuffle(t)
          >>>?t
          [16,?3,?13,?7,?6,?12,?17,?4,?15,?2,?5,?8,?18,?10,?9,?19,?14,?0,?1,?11]

          創(chuàng)建一個空集合a={},這樣對嗎?

          不對,這樣創(chuàng)建的是空字典

          >>>?a?=?{}
          >>>?type(a)
          'dict'>

          用 set 關鍵字創(chuàng)建空集合

          >>>?a?=?set()
          >>>?type(a)
          'set'>

          怎么打印出數(shù)字中分數(shù)格式

          用 fractions 中的 Fraction 方法

          >>>?from?fractions?import?Fraction
          >>>?print(Fraction(1,?3))
          1/3

          列出一個目錄下所有的文件名和子文件名

          用 os.walk 生成器函數(shù),我用 site-packages 目錄舉例。

          >>>?import?os
          >>>?dirs?=?os.walk('C:\Program?Files\Python36\Lib\site-packages')
          >>>?for?dir?in?dirs:
          ?print(dir)

          ?
          ('C:\\Program?Files\\Python36\\Lib\\site-packages',?['ad3',?'ad3-2.2.1.dist-info',?'adodbapi',?'aip',?'appium',?'AppiumLibrary',?'Appium_Python_Client-0.46-py3.6.egg-info',?'apscheduler',?'APScheduler-3.6.0.dist-info',?'atomicwrites',?'atomicwrites-1.3.0.dist-info',?...)

          一行代碼拼接字符串和序列形成新的列表

          用 itertools 里的 chain 方法可以一行代碼搞定。

          >>>?import?itertools
          >>>?list(itertools.chain('ABC',?range(5)))
          ['A',?'B',?'C',?0,?1,?2,?3,?4]

          今天分享的題目覺得不錯,點個贊哦,歡迎分享給其他朋友!

          —?【 THE END 】—
          本公眾號全部博文已整理成一個目錄,請在公眾號里回復「m」獲取!


          3T技術資源大放送!包括但不限于:Java、C/C++,Linux,Python,大數(shù)據(jù),人工智能等等。在公眾號內(nèi)回復「1024」,即可免費獲取!!




          瀏覽 37
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日本六十老龄高熟0930 | 亚洲色综合视频 | 蜜桃久久久亚洲精品 | 六月丁香激情 | 欧老太做爱 亚洲性猛交 |