Python100例 我的实现展示(41-45例)
作者:互联网
Python100例 我的实现展示(41-45例)
'''41、模仿静态变量的用法。(该题为宽泛性问题,模仿跟着题目做的)'''
def exchange(s2):
s = list(s2)
for i in range(int(len(s) / 2)):
t = s[i]
s[i] = s[len(s) - 1 - i]
s[len(s) - 1 - i] = t
return "".join(s)
'''class Static:
StaticVar = 5
def test_exam_41(self):
self.StaticVar += 1
print(self.StaticVar)
print(Static.StaticVar)
a = Static()
for i in range(3):
a.test_exam_41()'''
def test_exam_41():
var = 0
print('var = %d' % var)
var += 1
'''42、学习使用auto定义变量的用法。(自定义方法)'''
def auto_func():
print("调用方法并返回从1加到10的和")
total = 0
for i in range(10):
total += i
return total
def test_exam_42():
print("调用字符串逆序方法")
print(exchange("0123456789"))
print(auto_func())
'''43、模仿静态变量(static)另一案例。(参考)'''
class Num:
nNum = 1
def test_exam_43(self):
self.nNum += 1
print('nNum = %d' % self.nNum)
'''44、两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]'''
def test_exam_44():
x = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
y = [[5, 8, 1],
[6, 7, 3],
[4, 5, 9]]
z = [[0 for col in range(3)] for row in range(3)]
for i in range(3):
for j in range(3):
z[i][j] = x[i][j] + y[i][j]
print("2个3行3列的数组对应元素相加之后得到的数组如下:")
print(z)
'''45、统计 1 到 100 之和。'''
def test_exam_45():
total = 0
for i in range(1, 101):
total += i
print("1 到 100 之和为%d" % total)
if __name__ == '__main__':
'''for i in range(3):
test_exam_41()'''
# test_exam_42()
'''nNum = 2
inst = Num()
for i in range(3):
nNum += 1
print('The num = %d' % nNum)
inst.test_exam_43()'''
# test_exam_44()
test_exam_45()
标签:exam,45,41,print,range,test,Python100,def 来源: https://blog.csdn.net/supassxu/article/details/112186482