learning python,python learning1.py

 2023-10-21 阅读 29 评论 0

摘要:# 廖雪峰的官方網站 python教材 1~4章# 格式控制符語法print('Hello, %s' % 'world') print('hello, %s, you have %d dollars' % ('mickael', 1000)) print('hello, {0:s}, your grade {1:.1f}%'.format('xiaoming', 17.125
# 廖雪峰的官方網站 python教材 1~4章# 格式控制符語法print('Hello, %s' % 'world')
print('hello, %s, you have %d dollars' % ('mickael', 1000))
print('hello, {0:s}, your grade {1:.1f}%'.format('xiaoming', 17.125))s1 = 72
s2 = 85
improve = (s2-s1)/s1 * 100
print('hello, {0:s}, your grade improved by {1:.1f}%'.format("xiaoming", improve));# 列表 listclassmate = ['xiaoming','xiaozhao','xiaohong']
print(classmate)print(len(classmate))print(classmate[1])classmate.insert(1,"Jack")
print(classmate)classmate.pop()
print(classmate)classmate.pop(1)
print(classmate)classmate[1] = "Sarah"
print(classmate)L = ['apple', 123, True]
print(L)# 判斷age = 3
if age>= 18:print("adult")elif age >= 6:print("teenager")
else: print("kids")print("your age is", age)# 轉換成 intbirth = input('birth: ')
birth = int(birth)
if birth < 2000:print("00qian")
else:print("00hou")# 循環sum = 0
for x in list(range(5)):sum = sum + x
print(sum)alphobets = ['a','b','c']
for char in alphobets:print(char)names = ['michael', 'bob', 'Tracy']
for name in names:print(name)sum = 0
i = 0
while(i<101):sum += ii += 1
print(sum)L = ['Bart', 'Lisa', 'Adam']
for name in L:print("hello, %s!" % name)# 字典 dictionaryd = {'Michael':95, 'Bob':75, 'Tracy':85}
d['Adam'] = 67
print(d['Adma'])# 函數def myabs(x):if not isinstance(x, (int, float)):raise TypeError('bad operand type')if x>=0:return xelse:return -xprint(myabs(111))# 函數返回元組import mathdef move(x, y, step, angle=0):nx = x + step * math.cos(angle)ny = y + step * math.sin(angle)return nx, ny# 分別賦給每個變量x, y = move(100,100,60,math.pi/6)
print(x,y)def quadratic(a,b,c):delta = b*b - 4*a*cx1 = (-b + math.sqrt(delta)) / (2 * a)x2 = (-b - math.sqrt(delta)) / (2 * a)return x1, x2print('quadratic(2, 3, 1) =', quadratic(2, 3, 1))
print('quadratic(1, 3, -4) =', quadratic(1, 3, -4))if quadratic(2, 3, 1) != (-0.5, -1.0):print('測試失敗')
elif quadratic(1, 3, -4) != (1.0, -4.0):print('測試失敗')
else:print('測試成功')#def power(x):# return x * x# 默認參數def power(x,n=2):s = 1while n > 0:n = n - 1s = s * xreturn sprint(power(5))
print(power(5,3))# 可變長度參數def clac(*numbers):sum = 0for n in numbers:sum = sum + n * nreturn sumprint(clac(1,2))
print(clac())# 遞歸:漢諾塔
# 參數n,表示初始時,3個柱子a、b、c中第1個柱子a上的盤子數量,
# 打印出把所有盤子從A借助B移動到C的方法
def move(n, a, b, c):if n == 1:print(a, '-->', c)else:move(n-1,a,c,b)print(a,'-->',c)move(n-1,b,a,c)returnmove(3,'A','B','C')

轉載于:https://www.cnblogs.com/ZCplayground/p/8974159.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/155151.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息