您的位置:首页 > 编程语言 > Python开发

python学习 流程控制语句

2016-05-31 16:56 561 查看
#####################################分支语句python3.5########################################
#代码的缩进格式很重要建议4个空格来控制
#根据逻辑值(True,Flase)判断程序的运行方向
#Ture:表示非空的量(String,tuple元组、list、set、dictonary),所有非零的数字
#False:0,None、空的量
#逻辑表达式可以包含逻辑运算符andornot

if:


#####################################if########################################
if1<2:
print("小于");
print("<");
print("缩进很重要");
#demo
deffunc01():
return1;
iffunc01():
print(func01());
######################################ifelse########################################
if1<2:
print("小于");
else:
print("不小于");
#######################################ifelif######################################
num=int(input("请输入一个数:"));
ifnum>=90:
print("A");
elifnum>=80:
print("B");
elifnum>=70:
print("C");
elifnum>=60:
print("D");
else:
print("E");
#########################################if与andornot################################
if1and0:
print("1");
else:
print("0");#输出0

if1or0:
print("1");#输出1
else:
print("0")

ifnot1:
print("1");
else:
print("0");#输出0


循环、控制语句for:

##############################for循环#########################################
forstrin"abcdefg":
print(str,"Hello");
forarrin(1,2,3,4.5,5,45):
print(arr);
################################range()生成序列################################
#python2.x中,range返回的是一个列表
#python3.x中,range返回的是一个迭代值
forxinrange(10):
print("range:",x);
#如果要在3.x中产生1-10的列表,可以list(range(1,10))
range=list(range(1,10));
print(range);#[1,2,3,4,5,6,7,8,9]
###################################使用enumerate##############################
#在遍历list同时需要用到index和value值的时候可以用到enumerate,参数为可遍历的序列
app_list=[1234,5677,8899]
forindex,app_idinenumerate(app_list):
print(index,app_id);
#####################################使用索引遍历##############################
#str="abcde";
#print(str[0]);
#print(range[len(str)]);
#forvinrange(len(str)):
#print(str[x]);
#Traceback(mostrecentcalllast):File"E:/workSpace/pythonWork/function/com/round.py",
#line24,in<module>forvinrange(len(str)):TypeError:'list'objectisnotcallable
#这个错误一直没有解决
#######################################遍历字典#################################
dic_map={"a":"AAAA","b":"BBBB","c":"CCCCC","d":"DDDD"};
forxindic_map:
print(x,dic_map[x]);

print(dic_map.items());#[('a','AAAA'),('b','BBBB')]
fork,jindic_map.items():
print(k);#key值
print(j);#Value值
#######################################循环控制##################################
dic_map2={1:"AAAA",2:"BBBB",3:"CCCCC",4:"DDDD"};

fork,jindic_map2.items():
print(k);
print(j);
else:
print("循环正常结束(没有遇到break)");

print("#######break"*10);

fork,jindic_map2.items():
print(k);
print(j);
ifk==2:
break;#终止循环
else:
print("循环正常结束(没有遇到break)");

print("#######continue"*10);

fork,jindic_map2.items():
ifk==3:
continue;#终止当前循环
print(k);
print(j);
ifk==2:
exit();#终止整个程序
else:
print("循环正常结束(没有遇到break)");


循环、控制语句while:

#########################################while循环########################################
#根据表达式的结果判断程序是否继续循环
m=0;
whileTrue:
print("hello");
m+=1;
print(m);
ifm>10:
break;

f=10;
whilef>5:
print("Word");
f-=1;#控制条件表达式

###########################whileelse###########################
n=10;
whilen>5:
print("Word");
n-=1;#控制条件表达式
ifn==5:
continue;
print("测试continue",n);
else:
print("循环正常结束(没有遇到break)");



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: