>>> a=[12, 3, 4, 6, 7, 13, 21] >>> newList = [x for x in a] >>> newList [12, 3, 4, 6, 7, 13, 21] >>> newList2 = [x for x in a if x%2==0] >>> newList2 [12, 4, 6]
>>> newList2=[] >>> for x in a: ... if x %2 == 0: ... newList2.append(x) >>> newList2 [12, 4, 6]
>>>a=[12, 3, 4, 6, 7, 13, 21] >>>b=['a', 'b', 'x'] >>>newList=[(x, y) for x in a for y in b] >>>newList [(12, 'a'), (12, 'b'), (12, 'x'), (3, 'a'), (3, 'b'), (3, 'x'), (4, 'a'), (4, 'b'), (4, 'x'), (6, 'a'), (6, 'b'), (6, 'x'), (7, 'a'), (7, 'b'), (7, 'x'), (13, 'a'), (13, 'b'), (13, 'x'), (21, 'a'), (21, 'b'), (21, 'x')] >>>newList2=[(x, y) for x in a for y in b if x%2==0 and y<'x'] >>>newList2 [(12, 'a'), (12, 'b'), (4, 'a'), (4, 'b'), (6, 'a'), (6, 'b')]
def print_prime(n):
for i in xrange(2, n):
# found = True
for j in xrange(2, i):
if i % j == 0:
# found = False
break
else:
print "{} it's a prime number".format(i)
# if found:
# print "{} it's a prime number".format(i)
print_prime(7)
2 it's a prime number 3 it's a prime number 5 it's a prime number
def my_to_int(str_param):
try:
print int(str_param)
except ValueError:
print 'cannot convert {} to a integer'.format(str_param)
else:
print 'convert {} to integer successfully'.format(str_param)
my_to_int("123")
my_to_int("me123")
结果:123 convert 123 to integer successfully cannot convert me123 to a integer
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有