#while 循环
· 当一个条件成立就可以循环
· 不知道具体的循环次数,但是知道循环的成立条件
benjin = 1000
year = 0
while benjin < 2000:
benjin = benjin * (1 + 0.1)
year +=1
print("第{0}年拿了{1}块钱。".format(year,benjin))
第1年拿了1100.0块钱。
第2年拿了1210.0块钱。
第3年拿了1331.0块钱。
第4年拿了1464.1000000000001块钱。
第5年拿了1610.5100000000002块钱。
第6年拿了1771.5610000000004块钱。
第7年拿了1948.7171000000005块钱。
第8年拿了2143.5888100000006块钱。
#while 的另一种表达式
· while 条件表达式:
语句1
else:
语句2
·意思是,执行完循环之后,再执行一次else语句,只执行一次
#函数
· 代码的一种组织形式
· 一个代码完成一项特定的功能
· 函数需要提前定义
· 使用时,调用
def fun():
print("i love you .")
fun() #函数调用
i love you .
#函数的参数和返回值
· 参数负责给函数传递一些必要的数据或者信息
- 形参,实参
· 返回值,函数的执行结果
#函数的定义和使用
def hello(person):
print("hello {0}".format(person))
l = "liziqiang"
hello(l)
hello liziqiang
#函数的定义和使用
def hello(person):
print("hello {0}".format(person))
return "{0}不理我!".format(person) #函数返回一个值
l = "李自强"
rst = hello(l)
print(rst)
hello 李自强
李自强不理我!
#函数的定义和使用
def hello(person):
return "{0}不理我!!!!".format(person) #函数返回一个值
print("hello {0}".format(person))
return "{0}不理我!".format(person) #函数返回一个值
l = "李自强"
rst = hello(l)
print(rst)
李自强不理我!!!!
#99乘法表
def chengfa():
for i in range(1,10):
for j in range(1, i+1):
k = i*j
print("{0}X{1}={2}".format(i,j,k),end =" ")
print("\n") #换行
chengfa()
1X1=1
2X1=2 2X2=4
3X1=3 3X2=6 3X3=9
4X1=4 4X2=8 4X3=12 4X4=16
5X1=5 5X2=10 5X3=15 5X4=20 5X5=25
6X1=6 6X2=12 6X3=18 6X4=24 6X5=30 6X6=36
7X1=7 7X2=14 7X3=21 7X4=28 7X5=35 7X6=42 7X7=49
8X1=8 8X2=16 8X3=24 8X4=32 8X5=40 8X6=48 8X7=56 8X8=64
9X1=9 9X2=18 9X3=27 9X4=36 9X5=45 9X6=54 9X7=63 9X8=72 9X9=81
#查找函数帮助文档
#用help函数
help(print) #如果需要打印东西,默认以空格隔开,\n表示换行,
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
#默认参数示例
#报名的人,大多都是男生,如果没有特别指出,就认为是男生
def baoming(name, age, gender ="male"):
if gender == "male":
print("{0} ,he is a good student!".format(name))
else:
print("{0}, she is a good student!".format(name))
baoming("lisi", 22)
baoming("zhangsan", 23,"female")
lisi ,he is a good student!
zhangsan, she is a good student!