@linux1s1s
2017-08-09T10:31:15.000000Z
字数 1239
阅读 1497
Python 2017-08
这里简要记录
Python核心编程读书笔记 整段代码 可以直接运行
# -*- coding:gb18030 -*-# 序列s = 'abcdefgh'# 顺序取出字符print s[::1]# 倒序取出字符print s[::-1]# 每隔一个取出一个字符print s[::2]# 内置函数,将首字母大写print s.capitalize()st = ''' hi 234592034860!@#$%^&*********()there'''print st# 验证元组的Immutable(其和Java中String一样具有Immutable)t = ('a', 'b')print tprint id(t)t += ('c', 'd')print tprint id(t)# 元组(tuple)的表示,只有一个元素的元组一般要用,作为后缀x = ('abc')print type(x)print xy = ('abc',)print type(y)print ydef showMaxFactor(num):count = num/2while count > 1:if num % 2 == 0:print 'larget factor of %d is %d' %(num, count)breakcount -= 1else:print num, 'is prime'for eachNum in range(10,21):showMaxFactor(eachNum)# 元组,迭代器myTuple = (123, 'xyz', 45.67)i = iter(myTuple)print i.next()print i.next()print i.next()# lambda表达式x = map(lambda x: x ** 2 , range(6))print x# 列表解析式z = [x ** 2 for x in range(6)]print z# 生成器解析j = (x ** 2 for x in range(7))
C:\Python27\python.exe H:/workspace/python-hw/hw-2.pyabcdefghhgfedcbaacegAbcdefghhi 234592034860!@#$%^&*********()there('a', 'b')36770056('a', 'b', 'c', 'd')36097384<type 'str'>abc<type 'tuple'>('abc',)larget factor of 10 is 511 is primelarget factor of 12 is 613 is primelarget factor of 14 is 715 is primelarget factor of 16 is 817 is primelarget factor of 18 is 919 is primelarget factor of 20 is 10123xyz45.67[0, 1, 4, 9, 16, 25][0, 1, 4, 9, 16, 25]Process finished with exit code 0
