入門Python必備100道練習(xí)題

Python基礎(chǔ)習(xí)題
1、怎么計(jì)算2的3次方
解法1:直接用運(yùn)算符
>>> 2**3
解法2:用函數(shù) pow
>>> pow(2,3)
2、怎么找出序列中的最大值和最小值?
>>> l = (123, 888, 666)
>>> max(l)
888
>>> min(l)
123
3、怎么將字符列表轉(zhuǎn)為字符串
>>> l = ['Python', 'Circle', 'is', 'ok']
>>> j = ' '.join(l)
>>> j
'Python Circle is ok'
4、怎么快速打印出包含所有 ASCII 字母(大寫和小寫)的字符串
>>> import string
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
5、怎么讓字符串居中
>>> k = 小魚Python
>>> k.center(50)
' 更多精彩,請(qǐng)關(guān)注「小魚Python」 '
>>> k.center(50, '*')
6、怎么在字符串中找到子串
>>> ss = 'I Love Python'
>>> ss.find('I')
0
>>> ss.find('Python')
7
7、怎么讓字符的首字母大寫,其他字母小寫
解法1:用 title 方法。
>>> ss = 'i love python'
>>> ss.title()
'I Love Python'
解法2:用 string 模塊里的 capwords 方法。
>>> import string
>>> ss = 'i love python'
>>> string.capwords(ss)
'I Love Python'
9、怎么清空列表內(nèi)容
解法1:用 clear 方法
>>> l = [1, 2, 3]
>>> l.clear()
>>> l
[]
解法2:用切片賦值的方法
>>> k = [1, 2, 3]
>>> k[:] = []
>>> k
[]
10、怎么計(jì)算指定的元素在列表中出現(xiàn)了多少次?
>>> l = ['i', 'am', 'ok', 'ok']
>>> l.count('ok')
2
11、怎么在列表末尾加入其它元素
>>> l = [1, 2, 3]
>>> j = [4, 5, 6]
>>> l.extend(j)
>>> l
[1, 2, 3, 4, 5, 6]
12、extend 和列表相加的區(qū)別?
>>> l = [1, 2, 3]
>>> j = [4, 5, 6]
>>> l + j
[1, 2, 3, 4, 5, 6]
13、怎么查找列表中某個(gè)元素第一次出現(xiàn)的索引,從 0 開始
>>> l = ['are', 'you', 'ok']
>>> l.index('you')
1
14、怎么將一個(gè)對(duì)象插入到列表中
解法1:用 insert 方法
>>> num = [1, 2, 4, 5]
>>> num.insert(2, 'three')
>>> num
[1, 2, 'three', 4, 5]
解法2:用切片的方式插入
>>> num = [1, 2, 4, 5]
>>> num[2:2] = ['three']
>>> num
[1, 2, 'three', 4, 5]
15、怎么刪除列表中元素
>>> num = [1, 2, 4, 5]
>>> num.pop()
5
>>> num
[1, 2, 4]
>>> num.pop(1)
2
>>> num
[1, 4]
16、怎么刪除列表中指定元素
>>> num
[1, 4]
>>> num = [1, 2, 4, 5, 4]
>>> num.remove(4)
>>> num
[1, 2, 5, 4]
remove 方法只會(huì)刪除第一次出現(xiàn)的元素/
17、怎么讓列表按相反順序排列?
解法1:用 reverse 方法
>>> num = [1, 22, 45, 99, 49]
>>> num.reverse()
>>> num
[49, 99, 45, 22, 1]
解法2:用切片的方式
>>> num = [1, 22, 45, 99, 49]
>>> num[::-1]
[49, 99, 45, 22, 1]
18、怎么表示只包含一個(gè)元素的元組
>>> t= (1)
>>> type(t)
<class 'int'>
>>> t= (1,)
>>> type(t)
<class 'tuple'>
19、怎么批量替換字符串中的元素
>>> 'i love Python'.replace('o', 'ee')
'i leeve Pytheen'
20、怎么把字符串按照空格進(jìn)行拆分
>>> 'i love Python'.split()
['i', 'love', 'Python']
Python進(jìn)階習(xí)題
1、怎么用for循環(huán)實(shí)現(xiàn)把字符串變成Unicode碼位的列表
>>> st = '!@#$%^&*'
>>> codes = []
>>> for s in st:
codes.append(ord(s))
>>> codes
[33, 64, 35, 36, 37, 94, 38, 42]
2、怎么用列表推導(dǎo)式實(shí)現(xiàn)把字符串變成Unicode碼位的列表
>>> st = '!@#$%^&*'
>>> codes = [ord(s) for s in st]
>>> codes
[33, 64, 35, 36, 37, 94, 38, 42]
3、打印出兩個(gè)列表的笛卡爾積
解法1:使用生成器表達(dá)式產(chǎn)生笛卡爾積,可以幫忙省掉運(yùn)行 for 循環(huán)的開銷。
>>> colors = ['blacks', 'white']
>>> sizes = ['S', 'M', 'L']
>>> for tshirt in ('%s %s'%(c, s) for c in colors for s in sizes):
print(tshirt)
blacks S
blacks M
blacks L
white S
white M
white L
解法2:使用 itertools 里的 product 生成器函數(shù)。
>>> import itertools
>>> list(itertools.product(['blacks', 'white'], ['S', 'M', 'L']))
[('blacks', 'S'), ('blacks', 'M'), ('blacks', 'L'), ('white', 'S'), ('white', 'M'), ('white', 'L')]
4、可迭代對(duì)象拆包時(shí),怎么賦值給占位符
>>> player_infos = [('Kobe', '24'), ('James', '23'), ('Iverson', '3')]
>>> for player_names, _ in player_infos:
print(player_names)
Kobe
James
Iverson
5、Python3 中,用什么方式接收不確定值或參數(shù)
>>> a, b, *c = range(8)
>>> a, b, c
(0, 1, [2, 3, 4, 5, 6, 7])
>>> a, *b, c, d = range(5)
>>> a,b,c,d
(0, [1, 2], 3, 4)
>>> *a, b, c, d = range(5)
>>> a,b,c,d
([0, 1], 2, 3, 4)
6、用切片將對(duì)象倒序
>>> s = 'basketball'
>>> s[::-1]
'llabteksab'
7、怎么查看列表的 ID
>>> l = [1, 2, 3]
>>> id(l)
4507638664
8、可變序列用*=(就地乘法)后,會(huì)創(chuàng)建新的序列嗎?
>>> l = [1, 2, 3]
>>> id(l)
4507939272
>>> l *= 2
>>> l
[1, 2, 3, 1, 2, 3]
>>> id(l)
9、不可變序列用*=(就地乘法)后,會(huì)創(chuàng)建新的序列嗎?
>>> t = (1, 2, 3)
>>> id(t)
4507902240
>>> t *= 2
>>> t
(1, 2, 3, 1, 2, 3)
>>> id(t)
4507632648
10、關(guān)于+=的一道謎題
t = (1, 2, [30, 40])
t[2] += [50, 60]
>>> t = (1, 2, [30, 40])
>>> t[2] += [50, 60]
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
t[2] += [50, 60]
TypeError: 'tuple' object does not support item assignment
>>> t
(1, 2, [30, 40, 50, 60])

搜索下方加老師微信
老師微信號(hào):XTUOL1988【切記備注:學(xué)習(xí)Python】
領(lǐng)取Python web開發(fā),Python爬蟲,Python數(shù)據(jù)分析,人工智能等精品學(xué)習(xí)課程。帶你從零基礎(chǔ)系統(tǒng)性的學(xué)好Python!
*聲明:本文于網(wǎng)絡(luò)整理,版權(quán)歸原作者所有,如來源信息有誤或侵犯權(quán)益,請(qǐng)聯(lián)系我們刪除或授權(quán)

評(píng)論
圖片
表情
