Python学习笔记

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

                          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())









 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值