Python 練習程式 02

2021-10-02 08:01:49 字數 2356 閱讀 5384

# -*- coding: utf-8 -*-

"""calcualte 5!

"""def tang(i):

sum_value = 0

if i == 0:

sum_value = 1

else:

sum_value = i * tang(i-1)

return sum_value

for j in range(10):

print('%d!=%d'%(j,tang(j)))

runfile('c:/tools/python/python資料/00-1903xly/day04/day04/code/untitled6.py', wdir='c:/tools/python/python資料/00-1903xly/day04/day04/code')

0!=1

1!=1

2!=2

3!=6

4!=24

5!=120

6!=720

7!=5040

8!=40320

9!=362880

# -*- coding: utf-8 -*-

"""利用遞迴函式呼叫方式,將所輸入的5個字元,以相反順序列印出來

"""def output(s,l):

if l == 0:

return

print(s[l-1])

output(s,l-1)

s = input("please input a string")

l = len(s)

output(s,l)

# -*- coding: utf-8 -*-

"""按逗號分割列表

"""l = ['tang','lilie','hangmeimei',123]

s = ','.join(str(n) for n in l)

print(s)

"""

將乙個陣列逆序輸出

"""a = [5,7,8,5,9,1]

n = len(a)

print(a)

for i in range(int(len(a)/2)):

a[i],a[n-1-i]= a[n-1-i],a[i]

print(a)

"""

兩個3行3列的矩陣,實現其對應位置的資料相加,並返回乙個新矩陣

"""x = [[1,2,3],[4,5,6],[7,8,9]]

y = [[10,2,3],[4,50,6],[7,8,90]]

z = [[0,0,0],[0,0,0],[0,0,0]]

for i in range(3):

for j in range(3):

z[i][j] = x[i][j] + y[i][j]

for z in z:

print(z)

"""

匿名函式求和

"""sum_value = lambda x,y:x+y

print(sum_value(1,2))

"""

查詢字串位置

"""s1 = 'asdgffgghhyjugfgd'

s2 = 'ffg'

print(s1.find(s2))

"""

在字典中找出年齡最大的,並輸出

"""people =

m = 'me'

for key in people.keys():

if people[key] > people[m]:

m = key

print(m,people[m])

"""

將列表轉為字典結構

"""key = ['a','b']

value = [123,456]

print(dict([key,value]))

"""

從鍵盤輸入乙個字串,將小寫字母全部轉換成大寫字母,然後輸出到乙個磁碟檔案中

"""f = open('test.txt','w')

s = input("please input a string")

s = s.upper()

f.write(s)

f.close()

f = open('test.txt','r')

print(f.read())

f.close()

Python練手程式 02

2 使用 列印直角三角形 3 使用 列印乙個倒三角形,要求倒三角形是等邊三角形。並且行數由使用者輸入 guess num 98 while true print please input a number num input ifnot num.isdigit print 請輸入乙個整數!conti...

python練習小程式

1.今年是否為閏年 import time thisyear time.localtime 0 print time.localtime if thisyear 400 0 or thisyear 4 0 and thisyear 100!0 print this year s is leap ye...

Python 列表練習程式

1.對以下列表中的第乙個元素首字母大小並輸出,輸出最後乙個元素 bicycles trek cannondale redline print bicycles 0 title 對bicycles的第乙個元素首字母大寫並輸出 print bicycles 1 title 索引 1代表最後乙個元素 an...