pythontip 挑战python ,1-5

挑战python 1-5的入门题的解答过程,有更好的方法请留言

题目(id:1):

just print a+b

描述:

give you two var a and b, print the value of a+b, just do it!!

提示:

挑战python栏目的所有题目,题目中所给变量使用前不用声明,也不用赋值,系统自动赋值。

如本题,只需一行代码即可: print a + b

系统会自动为a和b赋值,并检查代码执行结果和标准答案是否相同。

Python代码:

print a+b

题目(id:2):

list排序

描述:

给你一个list L, 如 L=[2,8,3,50], 对L进行升序排序并输出,
如样例L的结果为[2,3,8,50]

Python代码:

print sorted(L)

或者:

L.sort()
print L

题目(id:3):

字符串逆序

描述:

给你一个字符串 a, 如a=‘12345’,对a进行逆序输出a。

Python代码:

print a[::-1]

题目(id:3):

输出字典key

描述:

给你一字典a,如a={1:1,2:2,3:3},输出字典a的key,以','链接,如‘1,2,3'。

Python代码:

print ','.join(a.keys())

题目(id:5):

输出字符奇数位置的字符串

描述:

给你一个字符串 a, 输出字符奇数位置的字符串。如a=‘12345’,则输出135。

Python代码:

print a[::2]