


已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
帮助你快速入门的帮助你快速入门的 pythonpython 知识点知识点 下面是小编为大家精心收集整理的有关于 python 的一些学 习知识点 希望能够帮助到大家 如果你喜欢记得分享给身边 的朋友哦 学习的知识点如下 1 整型 int 与浮点型 float 可以进行基本的运算 如 5 7 35 7 2 3 5 7 3 1 等 一些相关的数学函数类似于 sin 之类都在 math 模块中 a 1 ture ab a hello a false a false print len a print a 1 3 print a 0 3 2 print a 1 a a 2 复数计算与逻辑值 复数的与或非相关的都可以很快捷 的运算出结果 逻辑值如 12 为 false bool 999 为 true 3 字符串的相关操作 连接 复制 len 长度 start end step 用来提取一部分和以及一些相关高级操作 如 下 字符串操作 a b abc xzy print ain a print ord a 0 print chr 65 print r print u123 4 列表与元组 列表可以增加 删除 替换和重排的作 用和一些切片拆分的操作 如下 list 列表 a 1 2 3 4 b a b 1 ture print a 列表组的基本操作 len in a 1 2 3 a 4 a 2 len a 2 in a 列表元素的高级操作 mylist 1 2 3 4 5 mylist 1 4 mylist 1 4 mylist 2 5 mylist 3 1 mylist 2 切片 s abcdefg12345 print s 3 9 print s 5 print s 1 6 1 print s 10 2 拆分 t mike and tom print t split 5 range 函数 是连续序列生成器 range 函数 list range 10 list range 5 10 range 0 10 tuple range 10 6 集合 set 集合是不重复元素的无序组合 用 set 可以 创建空集也可用 set 从其他序列转换生成集合 如下 set 集合 不重复元素的无序组合 a 1 2 3 4 3 2 1 b set a print a print b upper lower swapcase 修改大小写 print t upper print t lower print t swapcase 7 字典 dict 是通过 key 值来索引 values 元素 如下 mydict 1 mon linel 3332 mydict mon 3 linelinmydict mydict keys mydict values mydict items 8 运算表达式 函数调用如 import math n math sqrt 和 赋值 如 a 3 是讲 3 赋值给 a 这个逻辑 9 if 条件语句 包含 elif 或多条 elif 语句和一条 else 语句 组成 while 语句是条件循环语句 其中 break 语句是直接跳出循 环 如下 条件 if elif 逻辑条件 可以多个 elif a 12 ifa10 print great elifa6 print middle else print low while 循环 numbers 12 37 5 42 8 3 even odd whilelen numbers 0 number numbers pop if number 2 0 even append number else odd append number print even even print odd odd 10 函数 function 定义函数为 def 语句 调用函数为函数 名 参数 相关例子如下 defsum list alist 定义一个带参数的函数 sum temp 0 foriinalist sum temp i returnsum temp 函数返回值 print sum list 查看函数对象 sum list my list 23 45 67 89 100 调用函数 讲返回值赋值给 my sum my sum sum list my list print sum of my list d my sum deffunc n total 1 foriinrange 1 n 1 total total 1 returntotal defsum n total 0 foriinrange 1 n 1 total func i print sum sum 3 deffunc test key1 key2 key3 23 print k1 s k2 s k3 s key1 key2 key3 print fun test func test v1 v2 func test ab cd 768 func test key2 kk key1 k 11 参数 及位置参数 参数的默认值 defthank you name everyone print you are doing good work s name thank you thank you 李四 位置参数 defdesc person first name last name age print first name s first name title print last name s last name title print age d age desc person brain kernighan 30 desc person age 20 first name zhang last name hai 混合位置和关键字参数 defdesc person first name last name age none favorite language none print first name s first name title print last name s last name title ifage print age d age iffavorite language print favorite language s favorite language desc person brian kernighan favorite language c desc person ken thompson age 22 接受任意数量的参数 defexample function num 1 num 2 nums sum num 1 num 2 then add any other numbers that were sent fornuminnums sum sum num print the results print the sum of your numbersis d sum example function 1 2 example function 1 2 3 example function 1 2 3 4 example function 1 2 3 4 5 12 map 函数 需要对列表中每个元素做一个相同的处理 得到新列表 map defadd a b returna b lst 1 2 3 4 5 print map add lst lst print list map add lst lst filter defis even x returnx 2 0 lst 1 2 3 4 5 print list filter is even lst reduce 函数对参数进行累积 fromfunctoolsimportreduce defmyadd x y returnx y lst 1 2 3 4 5 sum reduce myadd lst print sum 13 匿名函数 lambda 可以返回一个匿名函数 表达式为 lambda 参数表 表达式 lambda 函数 匿名函数 answer lambdax x 2 print answer 5 map filter reduce 结合 lambda 表达式 print list map lambdax x x yforyinrange 10 print list map lambdaa b a b xforxinrange 10 yforyinrange 10 print list filter lambdaa a 1 0 xforxinrange 10 print reduce lambdax y x y aforainrange 1 10 14 time 函数 获取当前的时间戳 time time localtime time localtime time time print 本地时间为 localtime 格式化时间 localtime time asctime time localtime time time print 本地时间为 localtime print time s 15 面向对象 力 classforce def init self x y x y 方向分量 self fx self fy x y defshow self 打印出力的值 print force s s self fx self fy defadd self force2 与另一个力合成 x self fx force2 fx y self fy force2 fy returnforce x y 类定义中的特殊方法 def str self returnforce r r self x self y def mul sel n x y self x n self y n returnforce x y def add self other x y self x other x self y other y returnforce x y def eq self other return self x other x and self y other y def repr self returnforce 0 x r 0 y r format self 生成一个力对象 f1 force 0 1 f1 show 生成另一个力对象 f2 force 3 4 合成为新的力 f3 f1 add f2 f3 show classstudent def init self name grade self name self grade name grade 内置 sort 函数只引用比较符来判断前后 def lt self other 成绩比 other 高的 排在他前面 returnself grade other grade student 的易读字符串表示 def str self return s d self name self grade student 的正式字符串表示 我们让它跟易读表示相同 repr str 构造一个 python list 对象 s list 添加到 student 对象到 list 中 s append student jack 80 s append student jane 75 s append student smith 82 s append student cook 90 s append student tom 70 print original s 对 list 进行排序 注意这是内置 sort 方法 s sort 查看结果 已经按照成绩排好序 print sorted s classcar def init self name self name name self remain mile 0 deffill fule self miles self remain mile miles defrun self miles print self name end ifself remain mile miles self remain mile miles print run d miles miles else print fuel out classgascar car deffill fuel self gas 加汽油 gas 升 self remain mile gas 6 0 每升跑 6 英里 classeleccar car deffill fuel self power 充电 power 度 self remain mile power 3 0 每度电 3
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年初级工程师机械设计与制造方向考试题库及答案解析
- 2025年初级产品经理面试秘籍及预测题
- 2025年初级人事专员面试问题与预测答案大揭秘
- 2025年CATV QAM调制器项目发展计划
- 2025年票务服务合作协议书
- 2025年U型荧光灯管项目合作计划书
- 辽宁省沈文新高考研究联盟2025-2026学年高二上学期开学质量监测数学试卷(含解析)
- 广西部分学校2025-2026学年高一上学期开学质量检测生物试题(有答案)
- 安徽省滁州市定远三中2025-2026学年高三开学摸底物理试卷(含答案)
- 2025年氮氧化铝晶体(ALON)项目建议书
- 股骨骨折病人护理要点
- 日历表2026年日历中文版纵向排版周日开始带周数带节假日调休安排
- 学生会综合事务部述职报告
- 《鲍曼不动杆菌》课件
- 2025年上海合伙企业协议模板
- Unit 1 Making friends Section A How do we greet friends 第一课时(说课稿)-2024-2025学年人教PEP版(2024)英语三年级上册
- DB11-T 1211-2023 中央空调系统运行节能监测
- 《肺动脉高压的护理》课件
- 《继电保护知识培训》课件
- 《英语测试与评价》教学大纲
- 新疆生产建设兵团第六师五家渠市事业单位公开招聘284人高频重点提升(共500题)附带答案详解
评论
0/150
提交评论