时间:2021-05-22
Python中表达式和语句及for、while循环练习
1)表达式
常用的表达式操作符:x + y, x - yx * y, x / y, x // y, x % y逻辑运算:x or y, x and y, not x成员关系运算:x in y, x not in y对象实例测试:x is y, x not is y比较运算:x < y, x > y, x <= y, x >= y, x == y, x != y位运算:x | y, x & y, x ^ y, x << y, x >> y一元运算:-x, +x, ~x:幂运算:x ** y索引和分片:x[i], x[i:j], x[i:j:stride]调用:x(...)取属性: x.attribute元组:(...)序列:[...]字典:{...}三元选择表达式:x if y else z匿名函数:lambda args: expression生成器函数发送协议:yield x 运算优先级:(...), [...], {...}s[i], s[i:j]s.attributes(...)+x, -x, ~xx ** y*, /, //, %+, -<<, >> &^|<, <=, >, >=, ==, !=is, not isin, not innotandorlambda2)语句:
3)for循环练习
练习1:逐一分开显示指定字典d1中的所有元素,类似如下k1 v1k2 v2... >>> d1 = { 'x':1,'y':2,'z':3,'m':4 } >>> for (k,v) in d1.items(): print k,v y 2 x 1 z 3 m 4 练习2:逐一显示列表中l1=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]中的索引为奇数的元素; >>> l1 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] >>> for i in range(1,len(l1),2): print l1[i] Mon Wed Fri 练习3:将属于列表l1=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],但不属于列表l2=["Sun","Mon","Tue","Thu","Sat"]的所有元素定义为一个新列表l3; >>> l1 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] >>> l2 = ["Sun","Mon","Tue","Thu","Sat"] >>> l3 = [ ] >>> for i in l1: if i not in l2:l3.append(i) >>> l3 ['Wed', 'Fri'] 练习4:已知列表namelist=['stu1','stu2','stu3','stu4','stu5','stu6','stu7'],删除列表removelist=['stu3', 'stu7', 'stu9'];请将属于removelist列表中的每个元素从namelist中移除(属于removelist,但不属于namelist的忽略即可); >>> namelist= ['stu1','stu2','stu3','stu4','stu5','stu6','stu7'] >>> removelist = ['stu3', 'stu7', 'stu9'] >>> for i in namelist: if i in removelist :namelist.remove(i) >>> namelist ['stu1', 'stu2', 'stu4', 'stu5', 'stu6']4)while循环练习
练习1:逐一显示指定列表中的所有元素; >>> l1 = [1,2,3,4,5] >>> i = 0 >>> while i < len(l1) print l1[i] i += 1 1 2 3 4 5 >>> l1 = [1,2,3,4,5] >>> while l1: print l1.pop(0) 1 2 3 4 5 练习2:求100以内所有偶数之和; >>> i = 0 >>> sum = 0 >>> while i < 101: sum += i i += 2print sum 2550 >>> for i in range(0,101,2): sum+=i print sum 2550 练习3:逐一显示指定字典的所有键;并于显示结束后说明总键数; >>> d1 = {'x':1, 'y':23, 'z': 78} >>> i1 = d1.keys() >>> while i1: print i1.pop(0)else: print len(d1) x y z 3 练习4:创建一个包含了100以内所有奇数的列表; >>> d1 = [ ] >>> i = 1 >>> while i < 101: d1.append(i) i+=2 >>> print d1 [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] >>> d1 = [ ] >>> for i in range(1,101,2) d1.append(i) >>> print d1 [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] 练习5:列表l1=[0,1,2,3,4,5,6], 列表l2=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],以第一个列表中的元素为键,以第二个列表中的元素为值生成字典d1; >>> l1 = [0,1,2,3,4,5,6] >>> l2 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] >>> d1 = {} >>> count = 0 >>> if len(l1) == len(l2): while count < len(l1):d1[l1[count]] = l2[count] count += 1以上这篇python 表达式和语句及for、while循环练习实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
while循环结构格式:while表达式:语句块执行流程:当程序执行到while语句时,首先判断表达式的真假。若表达式的值为真,则执行缩进的语句块,之后返回表达
一、while和do-while的简介1).while语句语法:while(表达式){循环体;}循环过程:1.先判断表达式,是否为真,如果为真跳转到2,否则跳转
C++while循环while语句的一般形式如下:while(表达式)语句其作用是:当指定的条件为真(表达式为非0)时,执行while语句中的内嵌语句。其流程图
前言本篇博文介绍一下Python中的if条件语句、while循环语句、forin循环语句以及break和continue控制关键字。分支的基本语法if条件表达式
1.while循环(只有在条件表达式成立的时候才会进入while循环)while条件表达式: passwhile条件表达式: passelse: pass