1. 在1 2 3 4 5 6 7 8 9九个数字中插入“+”或“-”符号使得结果为100
from itertools import product
def total_100 () :
results,numbers = [],range(1 ,10 )
for item in product(['+' ,'-' ,'' ],repeat=8 ):
number_tuple = zip(numbers,item+("" ,))
a = [str(x)+y for x,y in number_tuple]
combine = "" .join(a)
if eval(combine) == 100 :
results.append(combine + "= 100" )
return results
new_lst = total_100()
for item in new_lst:
print(item)
结果
1+2+3-4+5+6+78+9 = 100
1+2+34-5+67-8+9= 100
1+23-4+5+6+78-9= 100
1+23-4+56+7+8+9= 100
12+3+4+5-6-7+89= 100
12+3-4+5+67+8+9= 100
12-3-4+5-6+7+89= 100
123+4-5+67-89= 100
123+45-67+8-9= 100
123-4-5-6-7+8-9= 100
123-45-67+89= 100
2.
print (10 //3 )
print (10 %3 )
print (10.0 % 3 )
3.
listTemp = [1 ,2 ,3 ]
print (listTemp[3 ])
print (listTemp[3 :])
4.
if dictTemp:
print("result A" )
elif dictTemp is None :
print("result B" )
else :
print("result C" )
5.
listA = [[x ,x *x ]for x in range(10 )if x %2 ]
print (listA)
6.
a = 1
def change (a) :
a = 2
change(a)
print(a)
7.
a = [1 ]
def change (a) :
a.append(2 )
a = [1 ]
change(a)
print(a)
8.
#Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy 模块。
#1 . copy .copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象。
#2 . copy .deepcopy 深拷贝 拷贝对象及其子对象
import copy
a = [1 ,2 ,3 ,4 ,['a' ,'b' ]]
b = a
c = copy .copy (a)
d = copy .deepcopy(a)
a.append (5 )
a[4 ].append ('c' )
print (a)#[1 , 2 , 3 , 4 , ['a' , 'b' , 'c' ], 5 ]
print (b)#[1 , 2 , 3 , 4 , ['a' , 'b' , 'c' ], 5 ]
print (c)#[1 , 2 , 3 , 4 , ['a' , 'b' , 'c' ]]
print (d)#[1 , 2 , 3 , 4 , ['a' , 'b' ]]
import re
a = re.compile("[a-zA-z]{4}" )
print(re.findall(a,"i Love Python" ))