三元运算

三元运算,是条件语句的简单的写法。如果条件为真,则返回值1,否则,返回值2。

  1. ret = 1 if 条件 else 2

深浅拷贝

对于数字(int)和字符串(str)而言,赋值、深拷贝、浅拷贝都无意义,因为内存地址指向同一个。

  1. import copy
  2. # ######### 数字、字符串 #########
  3. n1 = 123
  4. # n1 = "i am a student"
  5. print(id(n1))
  6. # ## 赋值 ##
  7. n2 = n1
  8. print(id(n2))
  9. # ## 浅拷贝 ##
  10. n2 = copy.copy(n1)
  11. print(id(n2))
  12.  
  13. # ## 深拷贝 ##
  14. n3 = copy.deepcopy(n1)
  15. print(id(n3))

对于字典、元祖、列表 而言,进行赋值、浅拷贝和深拷贝时,其内存地址的变化是不同的。

(1)赋值,只是创建一个变量,该变量指向原来内存地址;

(2)浅拷贝,在内存中只额外创建第一层数据;

(3)深拷贝,在内存中将所有的数据重新创建一份(排除最后一层,即:python内部对字符串和数字的优化)。

函数

在学习函数之前,写代码一直是面向过程编程,即:代码执行顺序从上到下,一段代码执行所需的功能,频繁涉及到重复内容。

为了更好的代码重用性和可读性,出现了函数和面向对象。

  • 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可
  • 面向对象:对函数进行分类和封装,让开发“更快更好更强...”
  1. def 函数名(参数):
  2.  
  3. ...
  4. 函数体
  5. ...
  6. 返回值

函数的定义主要有如下要点:

  • def:表示函数的关键字
  • 函数名:函数的名称,日后根据函数名调用函数
  • 函数体:函数中进行一系列的逻辑计算
  • 参数:为函数体提供数据
  • 返回值:当函数执行完毕后,可以给调用者返回数据。

内置函数

查看详细内容:点这里!

open函数,该函数用于文件处理

操作文件时,一般需要经历如下步骤:

  • 打开文件
  • 操作文件

一、打开文件:f = open("文件路径","打开模式")

打开文件的模式有:

  • r ,只读模式【默认】
  • w,只写模式【不可读;不存在则创建;存在则清空内容;】
  • x, 只写模式【不可读;不存在则创建,存在则报错】
  • a, 追加模式【可读;   不存在则创建;存在则只追加内容;】

"+" 表示可以同时读写某个文件

  • r+, 读写【可读,可写】
  • w+,写读【可读,可写】
  • x+ ,写读【可读,可写】
  • a+, 写读【可读,可写】

"b"表示以字节的方式操作

  • rb  或 r+b
  • wb 或 w+b
  • xb 或 w+b
  • ab 或 a+b

注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型

二、操作

  1. class TextIOWrapper(_TextIOBase):
  2. """
  3. Character and line based layer over a BufferedIOBase object, buffer.
  4.  
  5. encoding gives the name of the encoding that the stream will be
  6. decoded or encoded with. It defaults to locale.getpreferredencoding(False).
  7.  
  8. errors determines the strictness of encoding and decoding (see
  9. help(codecs.Codec) or the documentation for codecs.register) and
  10. defaults to "strict".
  11.  
  12. newline controls how line endings are handled. It can be None, '',
  13. '\n', '\r', and '\r\n'. It works as follows:
  14.  
  15. * On input, if newline is None, universal newlines mode is
  16. enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
  17. these are translated into '\n' before being returned to the
  18. caller. If it is '', universal newline mode is enabled, but line
  19. endings are returned to the caller untranslated. If it has any of
  20. the other legal values, input lines are only terminated by the given
  21. string, and the line ending is returned to the caller untranslated.
  22.  
  23. * On output, if newline is None, any '\n' characters written are
  24. translated to the system default line separator, os.linesep. If
  25. newline is '' or '\n', no translation takes place. If newline is any
  26. of the other legal values, any '\n' characters written are translated
  27. to the given string.
  28.  
  29. If line_buffering is True, a call to flush is implied when a call to
  30. write contains a newline character.
  31. """
  32. def close(self, *args, **kwargs): # real signature unknown
  33. 关闭文件
  34. pass
  35.  
  36. def fileno(self, *args, **kwargs): # real signature unknown
  37. 文件描述符
  38. pass
  39.  
  40. def flush(self, *args, **kwargs): # real signature unknown
  41. 刷新文件内部缓冲区
  42. pass
  43.  
  44. def isatty(self, *args, **kwargs): # real signature unknown
  45. 判断文件是否是同意tty设备
  46. pass
  47.  
  48. def read(self, *args, **kwargs): # real signature unknown
  49. 读取指定字节数据
  50. pass
  51.  
  52. def readable(self, *args, **kwargs): # real signature unknown
  53. 是否可读
  54. pass
  55.  
  56. def readline(self, *args, **kwargs): # real signature unknown
  57. 仅读取一行数据
  58. pass
  59.  
  60. def seek(self, *args, **kwargs): # real signature unknown
  61. 指定文件中指针位置
  62. pass
  63.  
  64. def seekable(self, *args, **kwargs): # real signature unknown
  65. 指针是否可操作
  66. pass
  67.  
  68. def tell(self, *args, **kwargs): # real signature unknown
  69. 获取指针位置
  70. pass
  71.  
  72. def truncate(self, *args, **kwargs): # real signature unknown
  73. 截断数据,仅保留指定之前数据
  74. pass
  75.  
  76. def writable(self, *args, **kwargs): # real signature unknown
  77. 是否可写
  78. pass
  79.  
  80. def write(self, *args, **kwargs): # real signature unknown
  81. 写内容
  82. pass
  83.  
  84. def __getstate__(self, *args, **kwargs): # real signature unknown
  85. pass
  86.  
  87. def __init__(self, *args, **kwargs): # real signature unknown
  88. pass
  89.  
  90. @staticmethod # known case of __new__
  91. def __new__(*args, **kwargs): # real signature unknown
  92. """ Create and return a new object. See help(type) for accurate signature. """
  93. pass
  94.  
  95. def __next__(self, *args, **kwargs): # real signature unknown
  96. """ Implement next(self). """
  97. pass
  98.  
  99. def __repr__(self, *args, **kwargs): # real signature unknown
  100. """ Return repr(self). """
  101. pass
  102.  
  103. buffer = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  104.  
  105. closed = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  106.  
  107. encoding = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  108.  
  109. errors = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  110.  
  111. line_buffering = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  112.  
  113. name = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  114.  
  115. newlines = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  116.  
  117. _CHUNK_SIZE = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  118.  
  119. _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None) # default

三、管理上下文

为了避免打开文件后忘记关闭,可以通过管理上下文,即:

  1. with open('log','r') as f:
  2.  
  3. ...

如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。

在Python 2.7 及以后,with又支持同时对多个文件的上下文进行管理,即:

  1. with open('log1') as obj1, open('log2') as obj2:
  2. pass

lambda表达式

学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示。

对于简单的函数,也存在一种简便的表示方式,即:lambda表达式。

  1. # 普通条件语句
  2. if 1 == 1:
  3. name = 'wupeiqi'
  4. else:
  5. name = 'alex'
  6.  
  7. # 三元运算
  8. name = 'wupeiqi' if 1 == 1 else 'alex'
  9.  
  10. # ###################### 普通函数 ######################
  11. # 定义函数(普通方式)
  12. def func(arg):
  13. return arg + 1
  14.  
  15. # 执行函数
  16. result = func(123)
  17.  
  18. # ###################### lambda ######################
  19.  
  20. # 定义函数(lambda表达式)
  21. my_lambda = lambda arg : arg + 1
  22.  
  23. # 执行函数
  24. result = my_lambda(123)

递归

利用函数编写如下数列:

斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368...

  1. def func(arg1,arg2):
  2. if arg1 == 0:
  3. print arg1, arg2
  4. arg3 = arg1 + arg2
  5. print arg3
  6. func(arg2, arg3)
  7.  
  8. func(0,1)

Python开发【第三篇】:函数&读写文件的更多相关文章

  1. Python开发第三篇

    函数 一.函数参数传值 形参:函数在定义的时候给定的参数 实参:函数在运行时赋给的参数: def func(i):#i为定义时的参数,为形参 pass func(name)#name为运行时的参数,为 ...

  2. Python开发【第二篇】:初识Python

    Python开发[第二篇]:初识Python   Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏 ...

  3. Python开发【第一篇】:目录

    本系列博文包含 Python基础.前端开发.Web框架.缓存以及队列等,希望可以给正在学习编程的童鞋提供一点帮助!!! Python开发[第一篇]:目录 Python开发[第二篇]:初识Python ...

  4. Python开发【第一篇】:目录

    本系列博文包含Python基础.前端开发.Web框架.缓存以及队列等,希望可以给正在学习Python编程的朋友们提供一点帮助! .Python开发[第一篇]:目录 .Python开发[第二篇]:初始P ...

  5. Python【第三篇】文件操作、字符编码

    一.文件操作 文件操作分为三个步骤:文件打开.操作文件.关闭文件,但是,我们可以用with来管理文件操作,这样就不需要手动来关闭文件. 实现原理: import contextlib @context ...

  6. 第五篇:使用无缓冲IO函数读写文件

    前言 本文介绍使用无缓冲IO函数进行文件读写. 所谓的无缓冲是指该IO函数通过调用系统调用实现,其实系统调用内部的读写实现也是使用了缓冲技术的. 读写步骤 1. 打开文件 open 函数 2. 读写文 ...

  7. python数据挖掘第三篇-垃圾短信文本分类

    数据挖掘第三篇-文本分类 文本分类总体上包括8个步骤.数据探索分析->数据抽取->文本预处理->分词->去除停用词->文本向量化表示->分类器->模型评估.重 ...

  8. Python基础第三篇

    一.collections系列 Counter是对字典类型的补充,用于追踪值的出现次数,具备字典的所有功能 + 自己的功能 1.计数器Counter import collections a='aba ...

  9. python day5 lambda,内置函数,文件操作,冒泡排序以及装饰器

    目录 python day 5 1. 匿名函数lambda 2. python的内置函数 3. python文件操作 4. 递归函数 5. 冒泡排序 6. 装饰器 python day 5 2019/ ...

随机推荐

  1. [转]【docker】CMD ENTRYPOINT 区别

    本文转自:https://blog.csdn.net/u010900754/article/details/78526443 昨天用Dockerfile来启动mongodb的集群,启动参数--repl ...

  2. [转]What is Blue Prism?

    本文转自:https://www.guru99.com/blue-prism-tutorial.html#5 What is Blue Prism? Blue Prism is a UK-based ...

  3. vs 调试 iis中的网站

    打开网站,在vs中附加进程,选择w3wp.exe,如果不能下断点,设置一下pdb文件位置

  4. Docker 系列四(自定义仓库).

    一.Docker hub 交互 Docker hub 是 Docker 官方维护的一个公共仓库,大部分需求都可以通过在 Docker hub 中直接下载镜像来完成.接下来,来看一下怎么与 Docker ...

  5. Java 中变量初始化、子类和父类构造器调用的顺序

    先说结论 变量初始化 -> 父类构造器 -> 子类构造器 贴代码 Animcal.java 父类 public class Animal { private static int inde ...

  6. JavaScript是如何工作的:与WebAssembly比较及其使用场景

    摘要: WebAssembly未来可期. 原文:JavaScript是如何工作的:与WebAssembly比较及其使用场景 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 这是专门探索 ...

  7. git参考, 小结

    git官网: https://git-scm.com 菜鸟教程: http://www.runoob.com/git/git-tutorial.html 廖雪峰: https://www.liaoxu ...

  8. PS换脸操作

    1,使用套索工具抠出人的五官. 2,Ctrl+C复制黏贴到另一张头像中,调节透明度50%,与需要换脸的头像的眼睛,嘴巴,鼻子重合,透明度回归100%. 3,为了不该变原图,需要新建一张原图. 4,在抠 ...

  9. springboot 常用配置文件

    1.连接数据库 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId> ...

  10. bitnami_redmine3.3.0-1 问题及备份恢复

    1. 服务不见了处理方法: 安装Bitnami Redmine之后,会生成5个与之相关的进程,分别是 redmineApache redmineMySQL redmineSubversion redm ...