edward = ['Edward', 42]
john = ['John', 50]
database = [edward, john]
database
[['Edward', 42], ['John', 50]]
database[0]
['Edward', 42]
database[0][0][-1]
'd'
fourth = input('Year:')[3]
Year:2018
fourth
'8'
months = [
'Jan.',
'Feb.',
'Mar.',
'Apr.',
'May',
'Jun.',
'Jul',
'Aug.',
'Sep.',
'Oct.',
'Nov.',
'Dec.'
]
# 此处乘法为对应字符的个数
endings = ['st','nd','rd'] + 17 * ['th']\
+ ['st','nd','rd'] + 7 * ['th']\
+ ['st']
year = input('Year: ')
month = input('Month(1-12):')
day = input('Day(1-31):')
month_number = int(month)
day_number = int(day)
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print(month_name + ' ' + ordinal + ',' + year)
Year: 2018 Month(1-12):12 Day(1-31):1 Dec. 1st,2018
tag = '<a href="http://csyanwb.cn">JingShui</a>'
tag[9:-14]
'http://csyanwb.cn'
numbers = [1,2,3,4,5,6,7,8,9,10]
numbers[3:6]
[4, 5, 6]
numbers[0:1]
[1]
numbers[-3:0] #向右 --->
[]
numbers[-3:]
[8, 9, 10]
numbers[:3]
[1, 2, 3]
numbers[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers[0:10:2]
[1, 3, 5, 7, 9]
numbers[::2]
[1, 3, 5, 7, 9]
numbers[1:9:2]
[2, 4, 6, 8]
numbers[9:1:-2]
[10, 8, 6, 4]
numbers[1:9:-2]
[]
numbers[::-2]
[10, 8, 6, 4, 2]
numbers[:5:-2]
[10, 8]
[1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
'Hello,' + 'world!'
'Hello,world!'
[1,2,3] + 'Hello'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-62-aee2ba5ec26d> in <module>() ----> 1 [1,2,3] + 'Hello' TypeError: can only concatenate list (not "str") to list
[42]*10
[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]
'python'*5
'pythonpythonpythonpythonpython'
sequence = [None]*10
sequence
[None, None, None, None, None, None, None, None, None, None]
sentence = input("Sentence:")
screen_width = 80
text_width = len(sentence)
box_width = text_width + 2
left_margin = (screen_width - box_width)//2
print(" ")
print(' '*left_margin + '+' + "-"*(box_width-2) + '+')
print(' '*left_margin + '|' + " "*text_width + '|')
print(' '*left_margin + '|' + sentence + '|')
print(' '*left_margin + '|' + " "*text_width + '|')
print(' '*left_margin + '+' + "-"*(box_width-2) + '+')
print(" ")
Sentence:Today is Sunday! +----------------+ | | |Today is Sunday!| | | +----------------+
permissions= 'rw'
'w' in permissions
True
users = ['mlh','ywb','bar']
input("Enter your user name:") in users
Enter your user name:ywb
True
subject = "$$$ Get rich now!!! $$$"
'$$$' in subject
True
database = [
['albert', '1234'],
['dilbert','4567'],
['smith','7524'],
['jones','9843']
]
username = input("User name:")
pin = input("PIN Code:")
if [username, pin] in database:
print("Access granted!")
User name:jones PIN Code:9843 Access granted!
numbers=[100,34,678]
len(numbers)
3
max(numbers)
678
min(numbers)
34
max(32,65,12,78,99)
99
list("Hello")
['H', 'e', 'l', 'l', 'o']
元素赋值
x = [1,1,1]
x[1] = 2
x
[1, 2, 1]
删除元素 del
del x[2]
x
[1, 2]
分片赋值
name = list('Perl')
name
['P', 'e', 'r', 'l']
name[2:] = list("ar")
name
['P', 'e', 'a', 'r']
name[1:] = list("ython")
name
['P', 'y', 't', 'h', 'o', 'n']
numbers = [1,5]
numbers[1:1] = [2,3,4]
numbers
[1, 2, 3, 4, 5]
numbers[1:4] = []
numbers
[1, 5]
lst = [1,2,3]
lst.append(4)
lst
[1, 2, 3, 4]
['to','be','or','not','to','be'].count('to')
2
x = [[1,2],1,1,[2,1,[1,2]]]
x.count([1,2])
1
a = [1,2,3]
b = [4,5,6]
a.extend(b)
a
[1, 2, 3, 4, 5, 6]
a = [1,2,3]
b = [4,5,6]
a[len(a):] = b
a
[1, 2, 3, 4, 5, 6]
knights = ['we', 'are', 'the', 'knights', 'who', 'say', 'ni']
knights.index('who')
4
knights.index("herring")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-166-87226c97b57d> in <module>() ----> 1 knights.index("herring") ValueError: 'herring' is not in list
numbers = [1,2,3,5,6,7]
numbers.insert(3,'four')
numbers
[1, 2, 3, 'four', 5, 6, 7]
numbers = [1,2,3,5,6,7]
numbers[3:3] = ['four']
numbers
[1, 2, 3, 'four', 5, 6, 7]
x = [1,2,3]
x.pop()
3
x
[1, 2]
x.pop(0)
x
[2]
x = [1,2,3]
x.append(x.pop())
x
[1, 2, 3]
x = ['to','be','or','not','to','be']
x.remove('be')
x
[9, 7, 6, 4, 2, 1]
x = [1,2,3]
x.reverse()
x
[3, 2, 1]
list(reversed(x))
[1, 2, 3]
x = [5,3,9,1,6,8]
x.sort()
x
[1, 3, 5, 6, 8, 9]
y = x.sort() # Wrong
print(y)
None
x = [5,3,9,1,6,8]
y = x #直接赋值也不能保持x不发生变化!
y.sort()
x
[1, 3, 5, 6, 8, 9]
x = [5,3,9,1,6,8]
y = sorted(x) #sorted
x
[5, 3, 9, 1, 6, 8]
y
[1, 3, 5, 6, 8, 9]
sorted('Python') #现在已区分大小写!
['P', 'h', 'n', 'o', 't', 'y']
cmp在python3未指定
x = ['asdasdasd','sdssdsd','weqqw','weqe','dsfddfs']
x.sort(key=len)
x
['weqe', 'weqqw', 'sdssdsd', 'dsfddfs', 'asdasdasd']
x = [4,6,2,1,7,9]
x.sort(reverse=True)
x
[9, 7, 6, 4, 2, 1]
1,2,3
(1, 2, 3)
()
()
42
42
42,
(42,)
3*(40+2)
126
3*(40+2,)
(42, 42, 42)
tuple([1,2,3])
(1, 2, 3)
tuple('abc')
('a', 'b', 'c')
tuple((1,2,3))
(1, 2, 3)
x = 1,2,3
x[1]
2
x[0:2]
(1, 2)