Python中表达式和语句及for、while循环练习
1)表达式
常用的表达式操作符: x + y, x - y x * 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.attribute s(...) +x, -x, ~x x ** y *, /, //, % +, - <<, & ^ | <, <=, >, >=, ==, != is, not is in, not in not and or lambda
2)语句:
赋值语句 调用 print: 打印对象 if/elif/else: 条件判断 for/else: 序列迭代 while/else: 普通循环 pass: 占位符 break: continue def return yield global: 命名空间 raise: 触发异常 import: from: 模块属性访问 class: 类 try/except/finally: 捕捉异常 del: 删除引用 assert: 调试检查 with/as: 环境管理器 赋值语句: 隐式赋值:import, from, def, class, for, 函数参数 元组和列表分解赋值:当赋值符号(=)的左侧为元组或列表时,Python会按照位置把右边的对象和左边的目标自左而右逐一进行配对儿;个数不同时会触发异常,此时可以切片的方式进行; 多重目标赋值 增强赋值: +=, -=, *=, /=, //=, %=,
3)for循环练习
练习1:逐一分开显示指定字典d1中的所有元素,类似如下 k1 v1 k2 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 += 2 print 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循环练习实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月13日
2024年11月13日
- 魔兽世界wlk暗牧一键输出宏是什么 wlk暗牧一键输出宏介绍
- 群星.1996-红不让台语原唱2辑【福和唱片】【WAV+CUE】
- 郭书瑶.2009-爱的抱抱(EP)【种子音乐】【FLAC分轨】
- 郑瑞芬.1989-BE.MY.BABY【现代】【WAV+CUE】
- 花钱请人每周放30万只不咬人的蚊子 防治登革热传播
- 饭制《第一后裔》丧尸版弗蕾娜
- 贝克汉姆亲临!2024FC品类游戏嘉年华圆满落幕
- 「命轨爻错之翼」风之翼发放说明
- 《原神》前瞻特别节目回顾长图
- 米游币抽抽乐-原神专场现已开启!
- 黑鸭子2001《风情中国HQCD》[日本版][WAV+CUE]
- 陈杰洲1990-成人礼[滚石][WAV+CUE]
- MarkAanderud-HandsFree(2024)[24-44,1]FLAC
- 孙露《观心》1:1母盘直刻限量版[低速原抓WAV+CUE][361M]
- 钟志刚《汽车DJ玩主》[低速原抓WAV+CUE][1G]