@linux1s1s
2017-08-09T18:31:15.000000Z
字数 1239
阅读 1220
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 t
print id(t)
t += ('c', 'd')
print t
print id(t)
# 元组(tuple)的表示,只有一个元素的元组一般要用,作为后缀
x = ('abc')
print type(x)
print x
y = ('abc',)
print type(y)
print y
def showMaxFactor(num):
count = num/2
while count > 1:
if num % 2 == 0:
print 'larget factor of %d is %d' %(num, count)
break
count -= 1
else:
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.py
abcdefgh
hgfedcba
aceg
Abcdefgh
hi 234592034860!@#$%^&*********()
there
('a', 'b')
36770056
('a', 'b', 'c', 'd')
36097384
<type 'str'>
abc
<type 'tuple'>
('abc',)
larget factor of 10 is 5
11 is prime
larget factor of 12 is 6
13 is prime
larget factor of 14 is 7
15 is prime
larget factor of 16 is 8
17 is prime
larget factor of 18 is 9
19 is prime
larget factor of 20 is 10
123
xyz
45.67
[0, 1, 4, 9, 16, 25]
[0, 1, 4, 9, 16, 25]
Process finished with exit code 0