再這個題中,我試圖用xrange代替range來解決問題,但是結果輸出了『xrange(2, 26, 4)』這樣奇怪的答案。然後才理解了書上的「xrange不產生列表」這句話的含義。
」』python
ifname== 「main「:
numbers = raw_input(「please input from, to, incremen」)
lst = numbers.strip().split()
f, t, i = (int(num) for num in lst)
print range(f, t, i)
」』此處應該給while…else…乙個掌聲。
#!/usr/bin/env python
# coding: utf-8
defisprime
(a):
b = a/2
while b > 1:
if a % b == 0:
return
false
b -= 1
else:
return
true
if __name__ == "__main__":
num = int(raw_input("please input a number: ").strip())
if (isprime(num)):
print
"the number is a prime."
else:
print
"the number is not a prime."
#!/usr/bin/env python
# coding: utf-8
defshowfactor
(a):
count = a/2 + 1
lst = [i for i in xrange(1, count) if a % i == 0]
return lst
if __name__ == "__main__":
num = int(raw_input("input a number: "))
lst = showfactor(num)
print lst
#!/usr/bin/env python
# coding: utf-8
defshowfactors
(a, lst = ):
print
"the lst is : ", lst
count = a / 2
for i in range(2, count+1):
if a % i == 0:
a = a/i
return showfactors(a, lst)
else:
return lst
if __name__ == "__main__":
num = int(raw_input("please input a num: ").strip())
lst = showfactors(num)
print lst
這道題倒是簡單,直接用8_5的答案改動一些就好了
#!/usr/bin/env python
# coding: utf-8
defshowfactor
(a):
count = a/2 + 1
lst = [i for i in xrange(1, count) if a % i == 0]
return lst
defisperfect
(num):
lst = showfactor(num)
if sum(lst) == num:
return
1else:
return
0if __name__ == "__main__":
num = int(raw_input("input a number: "))
if(isperfect(num)):
print
"it's a perfect num"
else:
print
"not a perfect"
#!/usr/bin/env python
# coding: utf-8
deffactorial
(n):
factor = 1
for i in xrange(1, n+1):
factor *= i
return factor
if __name__ == "__main__":
num = int(raw_input("input a number: "))
print
"n! is: ", factorial(num)
這道題要為了提高計算的效率,我用了乙個小技巧,每次返回的時候不但返回n-1的fibonacci數,還將n-2一併返回了,因此可以極大程度上避免重複計算。(詳見資料結構與演算法中有這部分的介紹)
#!/usr/bin/env python
# coding: utf-8
deffibonacci
(n):
if n == 1:
#之所以返回兩個數是避免重複計算n-2的大小
return (0, 1)
fibn2, fibn1 = fibonacci(n-1)
return fibn1, fibn2+fibn1
deffib
(n):
#fib2得到的是n-1的fibonacci數,所以無用
fib2, fib1 = fibonacci(n)
return fib1
if __name__ == "__main__":
num = int(raw_input("input a number: "))
print fib(num)
總覺得這道題寫的不夠pythonic,不知道有沒有什麼其他更好的寫法呢?
#/usr/bin/env python
# coding: utf-8
defcountword
(sentence):
ivowel = 0
iconsonant = 0
strvower = 'aeiou'
for i in sentence:
if i.isalpha():
if i.lower() in strvower:
ivowel += 1
else:
iconsonant += 1
iword = len(sentence.split())
return (ivowel, iconsonant, iword)
if __name__ == "__main__":
sentence = raw_input("input a sentence: ")
inum = countword(sentence)
print
"the number of vowerl, consonant, word is: %s, %s, %s" %inum
#!/usr/bin/env python
# coding: utf-8
deflistprint
():print
"_" * 20
istart = int(raw_input("enter begin value: "))
iend = int(raw_input("enter end value: "))
bisstr = false
if32
< istart < 128
or34
< iend < 129
or (istart<33
and iend>128):
bisstr = true
print
"dec\t\tbin\t\toct\t\thex\t\tascii"
else:
bisstr = false
print
"dec\t\tbin\t\toct\t\thex"
print
"_" * 30
for i in xrange(istart, iend):
if32128:
print
"%s\t\t%s\t%s\t\t%s\t\t%s" %(i, bin(i), oct(i), hex(i), chr(i))
else:
print
"%s\t\t%s\t%s\t\t%s" %(i, bin(i), oct(i), hex(i))
if __name__ == "__main__":
listprint()
python核心程式設計 第八章 8 15 練習
usr bin python coding utf 8 8 2.迴圈.編寫乙個程式,讓使用者輸入三個數字 f rom,t o,和 i ncrement 以 i為步長,從 f 計數到 t 包括 f 和 t 例如,如果輸入的是 f 2,t 26,i 4 程式將輸出 2,6,10,14,18,22,26....
Python核心程式設計 第八章
1.三元操作符 x,y 4,3 if x y smaller x else smaller y smaller 3 smaller 0 smaller x smaller 3 smaller x if x smaller 3x if c else y 2.迭代序列有三種方法。通過序列項迭代 name...
第八章上課練習
向student表中插入資料 語法 insert into 表名 列名 values 值列表 insert into student studentno,loginpwd,studentname,gradeid,phone,address,borndate,email values s1304002...