小例二

一: 已知:元组 a = (1,2,3) 利用list方法,输出下面的结果:
(1,2,4)
1 #! /urs/bin/evn python
2 # -*- coding:utf-8 -*-
3 
4 a = (1, 2, 3)
5 mylsit = list(a)
6 mylsit[2] = 4
7 a = tuple(mylsit)
8 print(a, type(a))
View Code


二: 利用列表推导完成下面习题:

1 输出结果:[1 love python,2 love python,3 love python,.... 10 love python]

1 myslit = ["%s love python" % i for i in range(1, 11)]
2 print(myslit)
View Code

结果:

1 ['1 love python', '2 love python', '3 love python', '4 love python', '5 love python', '6 love python', '7 love python', '8 love python', '9 love python', '10 love python']
View Code

2 输出结果:[(0,0),(0,2),(2,0),(2,2)]
1 mylist = [(x, y) for x in range(0, 3, 2) for y in range(0, 3, 2)]
2 print(mylist)
3 
4 
5 结果:
6 [(0, 0), (0, 2), (2, 0), (2, 2)]
View Cod





原文地址:https://www.cnblogs.com/zqxqx/p/9206799.html