这是去年看书自学python时写下的笔记,现在还在整理,有些地方也在修改,持续更新中
参考书籍《Python基础教程(第3版》——Magnus Lie Hetland 《Python编程从入门到实践》——Eric Matthes
input('请输入:')
name = input('请输入您的姓名:')
print("您的名字是:",name)
a = input("请输入数字:")
b = input("请输入数字:")
print("a+b = ",a+b)
a = int(input("请输入数字:"))
b = int(input("请输入数字:"))
print("a+b = ",a+b)
int i = 1;
printf("%d\n",i);
a = int(input("请输入数字:"))
b = int(input("请输入数字:"))
print("正确输出格式",'%d + %d = %d' % (a, b, a + b))# 注意百分号
print("错误输出格式",'%d + %d = %d' , (a, b, a + b))
序列是一种数据结构
其中的元素带有编号
列表、字符串和元组都是序列
列表可以修改内容,字符串和元组不可以修改
指定编号,第一个元素编号是0,第二个元素编号是1,以此类推
x = 'Hello'
print(x[0])
------------------
print('Hello'[0])
这两个结果一样
x = 'Hello world!'
print(x[0:12])
列表名 = ['a','b','c','d','e']
print(列表名[1:4])
'''1和4为列表索引,列表名[1:4]指的是取
列表名[1],列表名[2],列表名[3]
即,['b','c','d']这三个元素
'''
List = ['a','b','c','d','e']
print(List[1:4])
列表名 = ['a','b','c','d','e']
print(列表名[:4])
'''
python从列表开头提取元素
['a','b','c','d']
'''
List = ['a','b','c','d','e']
print(List[:4])
列表名 = ['a','b','c','d','e']
print(列表名[1:])
'''
由于起始索引是1,python会从列表第二个元素‘b’开始提取至最后一个元素
['b','c','d','e']
'''
List = ['a','b','c','d','e']
print(List[1:])
列表名 = ['a','b','c','d','e']
print(列表名[-3:])
'''输出列表中最后三个元素:
['c', 'd', 'e']
注意:
若负数索引放在切片表达式 列表名[:-n] 冒号的后面,则表明去掉列表中最后的几个元素元素
列表名[:-1] 去掉最后一个元素
列表名[:-2] 去掉最后两个元素
列表名[:-3] 去掉最后三个元素
以此类推
'''
List = ['a','b','c','d','e']
print(List[-3:])
List = ['a','b','c','d','e']
print(List[:-3])
'''
列表[起点索引:终点索引:n]
*步长为n,从起点到终点每隔n-1个位置提取相应元素
'''
x = [1,2,3,4,5,6,7,8,9,10]
print(x[0:10:2])
'''结果 [1,3,5,7,9]'''
x = [1,2,3,4,5,6,7,8,9,10]
print(x[0:10:2])
'''
*步长为负数时,从右向左提取元素
第一个索引必须比第二个索引大
'''
x = [1,2,3,4,5,6,7,8,9,10]
print(x[10:0:-2])
# 结果 [10,8,6,4,2]
print(x[0:10:-2])
# 结果 []
# 省略第一个索引,则从列表最后一个元素开始从右向左遍历
print(x[:5:-2])
# 结果 [10,8]
print([1,2,3]+[4,5,6])
# [1,2,3,4,5,6]
print([32]*4)
# [32,32,32,32]
None、空列表和初始化
x = [None]*10
print(x)
# [None, None, None, None, None, None, None, None, None, None]
List = ['No1','No2','No3','No4']
print('No1' in List)
print('No5' in List)
List = ['No1','No2','No3','No4']
number = 'No5'
if number not in List:
print(number + "不在列表中")
x = [1,2,3,4,5,6,7,8,9,10]
print(min(x))
# 1
print(max(x))
# 10
print(sum(X))
# 55
print(len(x))
# 10
list('Hello')
# ['H','e','l','l','o']
列表名[] = 'xxx' # 直接将列表中指定位置的某个元素替换修改
x = [1,1,1]
x[1] = 2
print(x)
x = [1,1,1]
del x[1]
print(x)
# [1,1]
x = [1,1,1,1,1]
x[2:] = [3,3,3,3,3]
print(x)
#[1,1,3,3,3,3,3]
x = [1,1,1,1,1]
x[:2] = [3,3,3,3,3]
print(x)
x = [1,1,1,1,1]
x[2:2] = [3,3,3,3,3]
print(x)
x = [1,1,1,1,1]
x[1:3] = [] # 等效于 del x[1:3]
print(x)
x = [1,2,3]
x.append(4)
print(x)
x = [1,2,3]
x.clear()
print(x)
x = [1,2,3]
y = x.copy()
y[2] = 4
print(y)
print(x)
x = [1,2,3,1,1,1,1]
print(x.count(1))
x = [1,2,3]
y = [4,5,6]
x.extend(y)
print(x)
x = [1,2,3]
y = [4,5,6]
print(x+y)
print(x)
print(y)
x = [1,2,3,1,2,1]
print(x.index(1))
x = [1,2,3]
x.insert(3,4)
print(x)
x.insert(3,'awsl')
print(x)
x = [1,2,3]
print(x.pop())
print(x)
print(x.pop(0))
print(x)
x = [1,2,3,1]
x.remove(1)
print(x)
x = [1,2,3,4,5]
x.reverse()
print(x)
x = [2,4,5,6,3,1]
x.sort()
print(x)
y = ['asd','cse','bfhg','awsl']
y.sort()
print(y)
y = ['asd','csecd','bfhgsw','awsl']
y.sort(key=len) # 根据长度对元素进行排列
print(y)
x = [2,4,5,6,3,1]
x.sort(reverse = True)
print(x)
# 创建一个值的元组
x = (42,)
print(x)
print("Hello World!".center(39))
print("Hello World!".center(39,"*"))
print("Hello World! Hello Python!".find('World'))
# 指定搜索起点和终点
sub = "$$$ get rich now!!! $$$"
print(sub.find('!!!',1,21))
q = ['1','2','3','4','5']
p = '+'
print(p.join(q))
print("HELLO WORLD!".lower())
print("hello world!".title())
print("Hello World!".replace('World','Python'))
print('1+2+3+4+5'.split('+'))# 把‘+’号作为分隔符
print("Hello World".split())# 把空格符作为分隔符
print(' Hello Python ! '.strip())
print('*** Hello * Python !* world!***'.strip('world * !'))
因为strip只删除开头或末尾的指定字符,因此中间的星号未被删除
table = str.maketrans('s','w')
print('Awsl'.translate(table))
# 字典名 = {键:值,键:值...}
user = {
'name':'LinY','id':123456}
# 字典名[键名]
user = {
'name':'LinY','id':123456}
print('name:',user['name'])
# 字典名[键名] = 值
user = {
}
print("这里面是空的:",user)
user['name'] = 'LinY'
print('user = ',user)
# 字典名[键名] = "新"值
user = {
'name': 'Auto', 'id': 123456}
print('user = ', user)
user['name'] = 'Ato_Uo'
print('user = ', user)
# del 字典名[键名]
user = {
'name': 'Auto', 'id': 123456}
print('user = ', user)
del user['name']
print('user = ', user)
user = {
'username': 'LinY',
'address': 'Location'
}
for key, value in user.items():
print("\nKey: " + key)
print("Value: " + value)
user = {
'username': 'LinY',
'address': 'Location'
}
for k in user.keys():
print(k.title())
print('\n')
for k in user:
print(k.title())
'''
“遍历字典”时会默认遍历所有的键
所以 for k in user.keys():
可以写成 for k in user:
但是直接打印字典中的键时 print(user_0.keys()) 这时,keys()不能省略
'''
user = {
'username': 'LinY',
'address': 'Location'
}
for v in user.values():
print(v)
ball_0 = {
'color': 'green', 'volume': 10}
ball_1 = {
'color': 'yellow', 'volume': 10}
ball_2 = {
'color': 'red', 'volume': 10}
balls = [ball_0, ball_1, ball_2]
for ball in balls:
print(ball)
ball = {
'colors': ['yellow', 'red', 'blue']
}
for color in ball['colors']:
print(color)
users = {
'LinY': {
'id': 123456,
'location': 'beijing'
},
'Asail': {
'id': 987654,
'location': 'shanghai'
}
}
print(users.keys())
print(users.values())
for user_name, user_info in users.items():
print("\nUser_name: " + user_name)
location = user_info['location']
print('Loction:' + location.title())
users = {
'LinY': {
'id': 123456,
'location': 'beijing'
},
'Asail': {
'id': 987654,
'location': 'shanghai'
}
}
print(users)
users.clear()
print(users)
users_info = {
'name': 'LinY', 'loves': ['game', 'music', 'reading']}
users_copy = users_info.copy()
print("users_info = ", users_info)
print("users_copy = ", users_copy)
print("users_copy == users_info :", users_copy == users_info)
print("users_copy.keys() 内存地址:", id(users_copy.keys()))
print("users_info.keys() 内存地址:", id(users_info.keys()))
print("users_copy.values() 内存地址:", id(users_copy.values()))
print("users_info.values() 内存地址:", id(users_info.values()))
print("==============================================================")
users_copy['loves'].remove('reading')
print("users_copy['loves'].remove('reading')")
print("users_info = ", users_info)
print("users_copy = ", users_copy)
print("users_copy == users_info :", users_copy == users_info)
print("users_copy.keys() 内存地址:", id(users_copy.keys()))
print("users_info.keys() 内存地址:", id(users_info.keys()))
print("users_copy.values() 内存地址:", id(users_copy.values()))
print("users_info.values() 内存地址:", id(users_info.values()))
print("==============================================================")
users_copy['loves'] = 'game'
print("users_copy['loves'] = 'game'")
print("users_info = ", users_info)
print("users_copy = ", users_copy)
print("users_copy == users_info :", users_copy == users_info)
print("users_copy.keys() 内存地址:", id(users_copy.keys()))
print("users_info.keys() 内存地址:", id(users_info.keys()))
print("users_copy.values() 内存地址:", id(users_copy.values()))
print("users_info.values() 内存地址:", id(users_info.values()))
print("==============================================================")
'''
如果直接使用remove()函数users_copy['loves'].remove('reading')进行修改,输出的值一样,副本修改,原件也修改
用等号users_copy['loves'] = 'game'直接替换健对应的值,输出时和原件的值不一样,
但奇怪的是,用print(id(users_copy.values()))和print(id(users_info.values()))查看两者的内存地址却是相同的
建议避免使用copy()函数使用deepcopy()函数
'''
注意:
如果直接使用remove()函数users_copy[‘loves’].remove(‘reading’)进行修改,输出的值一样,副本修改,原件也修改
用等号users_copy[‘loves’] = 'game’直接替换健对应的值,输出时和原件的值不一样,
但奇怪的是,用print(id(users_copy.values()))和print(id(users_info.values()))查看两者的内存地址却是相同的
建议避免使用copy()函数使用deepcopy()函数
深复制使用模块copy中的函数deepcopy,同时复制值及其包含的所有值