python3 学习笔记


1、str.split()

spade :黑桃♠️;diamond:方块♦️;club:梅花♣️;heart:红桃♥️

>>> 'spades diamonds clubs hearts'.split()

['spades', 'diamonds', 'clubs', 'hearts']

>>> 'spades diamonds clubs hearts'.split(' ',1)

['spades', 'diamonds clubs hearts']

>>> 'spades diamonds clubs hearts'.split(' ',2)

['spades', 'diamonds', 'clubs hearts']

>>> 'spades diamonds clubs hearts'.split(' ',3)

['spades', 'diamonds', 'clubs', 'hearts']

2、list()

>>> list('JQKA')

['J', 'Q', 'K', 'A']

>>> atuple = (123,'234')

>>> list(atuple)

[123, '234']

>>> list((123,'234'))

[123, '234']

>>> list(123)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'int' object is not iterable

3、list + list

>>> >>> [str(n) for n in range(2,11)] + list('JQKA')

['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

4 、s[i:j:k]  含义从第i到第j,中间间隔 k

>>> s = range(20)

>>> w = s[::3]

>>> print(w)

range(0, 20, 3)

>>> for i in w:

...     print(i)

... 

0

3

6

9

12

15

18


注:学习资料《流畅的python》



猜你喜欢

转载自blog.csdn.net/only_anan/article/details/80050069