Python学习笔记
print('hello, world')
name = input("Enter the name: ")
print("name: ", name)
print("{} * {} = {}".format(1024, 768, 1024 * 768))
print("测试中文")
print('growth rate: %d %%' % 7)
names = ['aa', 'bb', 'cc']
for x in names:
print(x, end = " ")
#print(x)
print()
print("-" * 20)
sum = 0
for x in range(1, 101):
sum += x
print(sum)
sum = 0
n = 0
while n <= 100:
sum += n
n += 2
print(sum)
m = {'a': 'aa', 'b': 'bb', 'c': 'cc'}
print(m['a'])
s = set([1, 1, 2, 2, 3, 3])
print(s)
a = "abc"
a = a.replace("a", "A")
print(a)
def power(x, n=2):
s = 1
while n > 0:
n -= 1
s *= x
return s
print(power(5))
print(power(5, 4))
def person(name, age, **kw):
print('name:', name, 'age:', age, 'other:', kw)
person('Bob', 35, city='Beijing')
person('Adam', 45, gender='M', job='Engineer')
extra = {'city': 'Beijing', 'job': 'Engineer'}
person('Jack', 24, city=extra['city'], job=extra['job'])
def product(*x):
sum = 1
for a in x:
sum *= a
return sum
print('product(5) =', product(5))
print('product(5, 6) =', product(5, 6))
print('product(5, 6, 7) =', product(5, 6, 7))
print('product(5, 6, 7, 9) =', product(5, 6, 7, 9))
if product(5) != 5:
print('测试失败!')
elif product(5, 6) != 30:
print('测试失败!')
elif product(5, 6, 7) != 210:
print('测试失败!')
elif product(5, 6, 7, 9) != 1890:
print('测试失败!')
else:
print("测试成功!")
print('product() =', product())

本文详细介绍了Python编程的基础知识和实际应用,包括变量赋值、输入输出操作、字符串处理、循环控制、字典和集合的使用,以及函数定义与调用。通过具体示例展示了算术运算、条件判断、循环迭代等基本编程技巧,并介绍了如何利用函数进行幂运算和参数传递,还展示了如何处理关键字参数和不定长参数。
2391

被折叠的 条评论
为什么被折叠?



