目录
if...elif...else 用法 类似于while 多条件判断
for循环
Python中的for语句用于遍历序列(sequence)中的所有元素,这里的序列可以是列表(list)或者字符串(string)。
基本语法
使用方法
结果
其中,a是列表,通过for语句遍历了a中的所有元素。每次得到a中的一个元素,就将该元素的值保存到变量i中,并打印出i的取值,直至遍历a中的所有元素。
if语句实际上是:if True: …执行后面的语句
python 中的 if 有下面几种常见用法:
if...else用法
、
if...elif...else 用法 类似于while 多条件判断
if 判断条件1:
执行语句1...
elif 判断条件2:
执行语句2...
elif 判断条件3:
执行语句3...
else:
执行语句4...
while 循环
while 循环,满足条件时进行循环,不满足条件时退出循环。如下所示:
a=15
while a<20:
print(a,"小于20")
a += 1
else:
print(a,"大于或等于20")
结果
For循环 While 循环做九九乘法表
for i in range(1,10):
for j in range(1,10):
print(i,"x",j,"=", i*j,"\t",end="")
if i == j:
print("")
break
a=0
b=0
while a<9:
a+=1
while b<9:
b+=1
print(a, "x", b, "=", a * b, "\t", end="")
if a==b:
b=0
print("")
break
break:
break 用在 for 循环和 while 循环语句中,用来终止整个循环。如下所示:
结果
continue
continue 用在 for 循环和 while 循环语句中,用来终止本次循环。如下所示:
结果
pass语句
pass是空语句 一般用作站位语句 不作任何事情
导包
import与from…import
在python用import或者from..import来导入相应的模块。
将整个模块(somemodule)导入,格式为:import somemodule
从某个模块中导入某个函数,格式为:from somemodule import somefunction
从某个模块中导入多个函数,格式为:from somemodule import firstfunc,secondfunc, thirdfunc
将某个模块中的全部函数导入,格式为:from somemodule import*