SI100b期末复习

1.print函数

    完整语法:print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

   解释:

  • *Object: 一个或多个要print的Object
  • sep: 每个object间的间隔字符,默认为''
  • end: 在最后打印的字符,默认为'\n'
  • file: 必须是一个写入方法的object,默认为sys.stdout, 即objects会打印在屏幕上。
  •  **flush** :flush参数主要是刷新,flush=True时,会在print结束后立即将内存中的东西显示到屏幕上并清空缓存。(一般print都是将要输出的东西先存到内存中,再输出。

2. Escape Character    

Escape Character   Meaning    Example (print())  Output  
\''Don\'t do thisDon't do this
\""Penny said \"hi\"Penny said "hi"
\\\backslash: \\bbackslash: \
\nlinebreaka\nba
b
\tTABa\tba    b
\bbackspaceab\bcac
\roverriding123456\rXX_XXXX_XX6

3. 变量命名规则

  • 只能是字母,数字或下划线的组合
  • 只能以字母或下划线作为首字符
  • 大小写区分
  • keywords 不能用作变量名

4.标量

    1.int:only signed int;在32位系统里占32bits,64位系统里则占64bits。 

    2.float:有科学计数法e.g. 1.23\times 10^{23}   ->1.23e23;占64bits/8bytes

    3.complex:两种表示方法:<real part> + <imaginary part>j

                                                 <real part> + <imaginary part>J

    4.type()函数返回变量的类型

5.Strings 字符串

     using raw strings by adding r before the first quote without interpretation of escape characters

     e.g.print(r'Don\'t fo this')

           Don\'t do this

     String 切片[a:b] means [a,b)

     String Methods

    Some of the daily used built-in string operations

  • strip()
    • removes the whitespace at the beginning or the end
      wd = 'my name is '
      wd.strip()
  • len()
    • returns the length of the string
      wd = 'Python'
      len(wd)
  • lower() returns a string with lower case
  • upper() returns a string with upper case
     wd = 'Python'
     wd.lower()
     wd.upper()
  • replace(old, new)
    • return a copy of the string with all occurrences of substring old replaced by new
      wd = 'Python'
      wd.replace('P', 'p')

  6.一些等价关系

>>> 1 == 1.0 
True 
>>> 1 is 1.0
False
>>> 1 !=1.0 
False 
>>> 1 is not 1.0 
True
>>> None == False
False

7.LEGB原则

   查找变量名时的顺序:Local->Enclosing->Global->Built-in

8.函数参数

def example( required_parameter,default_parameter = '',*args,**kw):
    pass

参数种类:必选参数,默认参数,可选参数,关键字参数

note:默认参数有多个的话,可以不按顺序传参。

           可选参数本质上传进去的是一个tuple;关键字参数本质上传进去的是一个dict。

9.Call by object

个人认为可以这么理解,传进去的是对象的地址的复制,所以在list,dict这样的mutable对象上进行操作,实际上是会改变其本来的值的;而在str,tuple,int这样的对象上进行操作,因为其本身immutable的属性,所以大多进行的是赋值的操作,这个时候,参数不再指向原来的对象,而是指向一个新的对象,原先对象的值没有发生任何改变,所以这相当于call by value,不会改变其本身的值。

10. Working with files

     1.使用open()函数  

  • open(filename, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
  • returns 一个文件对象
    file_obj = open(filename, mode)

                 Note:文件名要保留后缀

  •  使用close()函数关闭文件
  •        f.close()
  • modedetails
    'w': writeused when files need to be changed, changed from the very beginning of the file, file is erased
    'r': readonly read the file, do not change the file, file pointer is placed at the very beginning of the file
    'a': appendadds information to the end of the file, file pointer is placed at the end of the file
    'r+': read and writefile pointer is placed at the very beginning of the file
    'a+': append and readfile is opened and allows program to read information and new information to be added at the end of the file
    't': text modeby default
    'b': binary modeonly used for data
    'x': exclusive creationexclusively creat a file, if a file of the same name already exists, it will fail
     

    files are opened in text and read mode by default

  • it means you read and write strings from and to the file, which are encoded in a specific encoding. keyword argument encoding can be set
  • open file in binary mode, append 'b' to the mode argument, e.g., 'rb'
  • 写入文件用‘w’mode,使用write函数
f.open('text.txt','w')
f.write('Hello')
f.close()

    读取文件

  • 使用 read(size)
    • size specifies the amount of data to be read
    • when size omitted, read the entire file
  • using the readline() function to read line by line
  • using readlines() to get a list of remaining lines of the file
  • Note:readline()和read()函数会读取换行符

11.Class 

    最好通读lecture7,都蛮重要的。

12.List.__mul__(self,n)

     

__mul__(self, n) : implementation of * operator

 lis.__mul__(n), or lis * n
  • n must be an integer, otherwise TypeError
  • if n <= 0, returns an empty list
  • if n = 1, returns a new list that has the same reference of the object lis
  • otherwise, returns a new list that contains n times of references of the object lis

 

lis1 = [[1, 2], [3, 4]]
lis2 = lis1 * 2
print(lis2)

#output:[[1, 2], [3, 4], [1, 2], [3, 4]]

 

 13.map(function,Iterable,......)

      简单说,就是把function应用到每个Iterable上,返回一个Iterator

    

x = map(lambda x : x ** 2, range(3))


next(x)
>>> 0
next(x)
>>> 1
next(x)
>>> 4
next(x)
>>> Stop Iteration

14 Generator

     例

def foo(): 
    print("begin")
    for i in range(3): 
        print("before yield", i) 
        yield i 
        print("after yield", i) 
    print("end")

      每次yield之后,Generator都会保存yield之前的数据。

>>> import os 
>>> os.chdir("D:\\Test") 
>>> from foo import foo 
>>> f = foo() 
>>> print(f) 
<generator object foo at 0x035A7470> 
>>> next(f) 
begin 
before yield 0 
0 
>>> next(f) 
after yield 0 
before yield 1 
1

      当Generator终止后,会自动调用StopIteration

      Generator有更紧凑的表达方式

x = (i+1 for i in range(5))

     (注意与列表生成式区分!!!!)

x = [i+1 for i in range(5)]#列表生成式

15.默认参数的值

   lecture9 P41-51

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值