network 发表于 2018-8-13 13:24:26

python 基础教程 笔记 一

  第一章 python 基础知识
  1.1 数字和数学表达式
  1.2 python 2.x 和python 3.x print的区别
  1.3 python 2.x 和 python 3.x input 的区别
  1.4 数学函数
  1.5 input 和 raw_input 区别
  第二章 列表和元组
  第三章 使用字符串
  1.1 数字和表达式
  Python 默认的除法:整数/整数,无论是否整除都得到整数,不整除的截取小时部分
  1 / 2
  0
  如果想要Python 执行普通的除法,可以之用浮点数来实现也可以改变Python执行除法的方法
  1.0 / 2
  0.5
  1.0 / 2.0
  0.5
  1 / 2.0
  0.5
  1/2.
  0.5
  from __future__ import division
  1 / 2
  0.5
  __future__ 前后均有两个下划线
  Linux下 使用Qnew 参数也可以实现普通除法运算
  # python -Qnew
  Python 2.7.5 (default, Nov 6 2016, 00:28:07)
   on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> 1 / 2
  0.5
  注意Python3 默认是执行普通除法运算
  # python3
  Python 3.6.1 (default, May 31 2017, 18:02:43)
   on linux
  Type "help", "copyright", "credits" or "license" for more information.
  >>> 1 / 2
  0.5
  整除使用双斜杠。python 2 和 python 3 使用方式都一样
  >>> 4 // 6
  0
  >>> 6 // 3
  2
  >>> 6 // 5
  In : 5.4 // 1.8
  Out: 3.0
  In : 5.4 // 1.8
  Out: 3.0
  In : 5.4 % 1.8
  Out: 2.220446049250313e-16
  虽然Python中浮点数也支持取模运算,但是对浮点数进行运算的时候要格外注意精度的问题。5.4 / 1.8 =3 正好整除,按理应该是0,可是这里的值不为0,很奇怪???
  幂运算(乘方) 比一元取反运算优先级要高
  In : -2 ** 4
  Out: -16
  In : (-2) ** 4
  Out: 16
  In : -(2 ** 4)
  Out: -16
  -2 ** 4 = -(2 ** 4)
  python 2 和 python 3 对于整形数字超出范围的自动转换为长整型,不需要过分的关注整型的取值范围,因为python能够处理很长的数字。可以手动的数字后面加一个大写的L就表示这是一个长整型。当然小写的l也可以只是这里看起来很像数字1,所以建议使用大写的L
  注意python 2.2 及以前整型值超出范围会报错。-2147483648 -- 2147483647
  python 2
  In : 214748364789987
  Out: 214748364789987
  In : 214748364789987568978945465 * 4654654167897893212313212312313212313132 + 5213
  Out: 999579371218972816926339919579932791950025118287985996498731351593L
  In : 4654654167897893212313212312313212313132
  Out: 4654654167897893212313212312313212313132L
  Python 3
  In : 214748364789987
  Out: 214748364789987
  In : 214748364789987568978945465
  Out: 214748364789987568978945465
  In : 214748364789987568978945465 * 4654654167897893212313212312313212313132 + 5213
  Out: 999579371218972816926339919579932791950025118287985996498731351593
  1.2 python 2.x 和 python 3.x print 的区别
  python print。python 2 直接输出即可,python 3 print 变成了函数
  python 2
  In : print 3 + 3
  6
  python 3
  In : print (2 + 3)
  5
  In : print 2 + 3
  File "<ipython-input-15-b59db2781faf>", line 1
  print 2 + 3
  ^
  SyntaxError: Missing parentheses in call to 'print'
  1.3 python 2.x 和 python 3.x input 的区别
  获取用户的输出 python 2 和 Python 3 有区别。input函数接受用户的输入python 2输入的数字会当成整型,而python 3 输入的数字当成字符型。输入字符串的时候python 2 需要使用引号括起来,而python 3 可以不用引号括起来
  python 2
  In : s = input("The meaning of life:")
  The meaning of life:45
  In : s
  Out: 45
  In : type(s)
  Out: int
  In : s = input("The meaning of life:")
  The meaning of life:abc
  ---------------------------------------------------------------------------
  NameError Traceback (most recent call last)
  <ipython-input-27-e2b30580ea33> in <module>()
  ----> 1 s = input("The meaning of life:")
  <string> in <module>()
  NameError: name 'abc' is not defined
  In : x = input("x: ")
  x: 34
  In : y = input("y: ")
  y: 23
  In : x * y
  Out: 782
  python 3
  In : s = input("The meaning of life:")
  The meaning of life:67
  In : s
  Out: '67'
  In : type(s)
  Out: str
  In : s = input("The meaning of life:")
  The meaning of life:'980'
  In : type(s)
  Out: str
  In : s = input("The meaning of life:")
  The meaning of life:abc
  In : s
  Out: 'abc'
  In : y = int(input("y: "))
  y: 23
  In : x = int(input("x: "))
  x: 34
  In : x * y
  Out: 782
  1.4 基础数学函数
  内嵌
  pow(x) 求幂函数
  abs(x) 求绝对值函数
  round(number,ndigits) 对浮点数进行四舍五入
  math
  floor(x) 向下取整,将小数部分抹掉
  ceil(x) 这个函数向上取整,当x为小数的时候去掉小数部分,整数部分+1
  sqrt(x) 求平方根
  求幂的函数pow()
  python 内建函数
  pow(x,y[,z]) = x ** y%z
  x的y次方所得的值对z取模
  In : pow(2,3)
  Out: 8
  In : 2 ** 3
  Out: 8
  In : 10 + pow(2,3*5)/3.0
  Out: 10932.666666666666
  注意python的版本不同精度有所不同,本实验所使用的两个版本的精度一致
  内建函数 abs 求绝对值
  In : abs(-10)
  Out: 10
  In : abs(10)
  Out: 10
  round(number,ndigits)
  此内建函数:对浮点数进行四舍五入。对number这个浮点数进行四舍五入,ndigits 这个数是精度,必须是整型。可以是负数
  两个版本的区别
  python2 输出始终是浮点数
  python 3 输出始终是整型
  python 2 大于0.49999999999999999这个数输出值位1.0,小于这个输出值为0.0
  python 3 大于0.5000000000000001 输出值为1,小于这个数输出值为0
  python 2
  In : b = "0.49999999999999999"
  In : len(b)
  Out: 19
  In : round(0.49999999999999999)
  Out: 1.0
  In : round(0.4999999999999999)
  Out: 0.0
  python 3
  In : round(0.50000000000000001)
  Out: 0
  In : round(0.5000000000000001)
  Out: 1
  In : b = '0.5000000000000001'
  In : len(b)
  Out: 18
  两个版本相同。注意这里应该是个陷阱,四舍五入是逢五进一
  列一
  In : round(5.005,2)
  Out: 5.0
  In : round(5.006,2)
  Out: 5.01
  列二
  In : round(5.32,2)
  Out: 5.32
  In : round(5.325,2)
  Out: 5.33
  In : round(5.324,2)
  Out: 5.32
  列三
  In : round(1.675,2)
  Out: 1.68
  In : round(2.675,2)
  Out: 2.67
  列四
  In : round(-1.436,2)
  Out: -1.44
  In : round(-1.433,2)
  Out: -1.43
  这两个例子,第一个例子是逢六进一,第二个例子是逢五进一。还有一个问题需要注意:round(5.001,2) 输出5.0而不是5.00.这个问题也很奇怪这样精度就是1而不是2。第三个列子说明有时候是逢五进一,有时候是逢六进一
  math.floor() 这个函数需要导入math模块 import math 才可以使用。
  作用:向下取整,将小数部分抹掉,但是注意这里有个精度的问题
  python 2 返回的是浮点数,python 3 返回的是整型
  python 2
  In : math.floor(32.999999999999999)
  Out: 33.0
  In : math.floor(32.99999999999999)
  Out: 32.0
  In : a = '32.999999999999999'
  In : len(a)
  Out: 18
  大于 或等于32.999999999999997,小于33 math.floor返回33.0
  大于32,小于或等于32.999999999999996 math.floor 返回32.0
  python 3
  In : math.floor(32.99999999999999)
  Out: 32
  In : math.floor(32.999999999999997)
  Out: 33
  In : b = '32.999999999999997'
  In : len(b)
  Out: 18
  大于 或等于32.999999999999997,小于33 math.floor返回33
  大于32,小于或等于32.999999999999996 math.floor 返回32
  math.ceil(x) 这个函数向上取整,当x为小数的时候去掉小数部分,整数部分+1
  python 2 返回的浮点值
  In : math.ceil(4.0000000000000004)
  Out: 4.0
  In : math.ceil(4.0000000000000005)
  Out: 5.0
  4.0000000000000005 <= x < 5 math.ceil(x) 返回5.0
  4 <= x < 4.0000000000000004 math.ceil(x) 返回4.0
  python 3 返回的是整型
  In : math.ceil(4.0000000000000004)
  Out: 4
  In : math.ceil(4.0000000000000005)
  Out: 5
  4.0000000000000005 <= x < 5 math.ceil(x) 返回5
  4 <= x < 4.0000000000000004 math.ceil(x) 返回4
  sqrt(x) 对x求平方根,x>0,为整数和浮点数。返回的是浮点数
  from math import sqrt 这样写可以避免当多个包中有sqrt函数的时候出现混淆的情况,而且这样就可以不用写math.这个前缀
  In : from math import sqrt
  In : sqrt(9)
  Out: 3.0
  In : sqrt(15)
  Out: 3.872983346207417
  In : help(sqrt)
  In : sqrt(-9)
  ---------------------------------------------------------------------------
  ValueError Traceback (most recent call last)
  <ipython-input-145-4abb6f6b3805> in <module>()
  ----> 1 sqrt(-9)
  ValueError: math domain error
  使用变量引用的方式来使用函数,这样的方式更精简,适用。
  In : foo = math.sqrt
  In : foo(36)
  Out: 6.0
  math.sqrt() 这个函数只能处理浮点数,而负数的平方根是虚数。而虚数(以及复数,虚数和实数之和)是完全不同的,这里需要使用使用另外一个包中函数。cmath.sqrt()
  In : import cmath
  In : square_root = cmath.sqrt
  In : square_root(-36)
  Out: 6j
  In : square_root(-49)
  Out: 7j
  注意这里没有使用from ... import ... 语句,如果使用 from cmanth import sqrt 那么不能直接使用sqrt 因为前面有使用 from math import sqrt。所以一般情况下最好还是使用import语句,不要使用from .. import ...。如果不想写前置,可以使用变量引用的方式简化函数调用
  后缀带j表示虚数,python本身也支持虚数
  In : (1+3j) * (9+4j)
  Out: (-3+31j)
  1.5input 与 raw_input 的区别
  input 和 raw_input 区别
  1、input 接受标准的python表达式,如果是字符串需要使用引号括起来。而raw_input 接受任意类型的输入
  2、对于数字来讲,raw_input 将数字当做字符串,而input把输入的数字当做int,float 等类型
  列1
  In : c = raw_input(3 + 5)
  8
  In :
  In : input(1+3)
  4
  File "<string>", line unknown
  ^
  SyntaxError: unexpected EOF while parsing
  input 直接敲回车会报错,因为没有输入内容
  列2
  In : raw_input("what number is: ")
  what number is: 56
  Out: '56'
  In : input("what number is: ")
  what number is: 56
  Out: 56
  列3
  In : raw_input("what is your name: ")
  what is your name: zhangsan
  Out: 'zhangsan'
  In : input("what is your name: ")
  what is your name: zhangsan # 要加引号
  ---------------------------------------------------------------------------
  NameError Traceback (most recent call last)
  <ipython-input-203-aa9bc168e8eb> in <module>()
  ----> 1 input("what is your name: ")
  <string> in <module>()
  NameError: name 'zhangsan' is not defined
  In : input("what is your name: ")
  what is your name: 'zhangshan'
  Out: 'zhangshan'
  input() 本质上还是使用 raw_input() 来实现的,只是调用完 raw_input() 之后再调用 eval() 函数,所以,你甚至可以将表达式作为 input() 的参数,并且它会计算表达式的值并返回它。
  除非有特殊需要,一般都不使用input() 这个函数,使用raw_input()
  python 3 中没有raw_input() 这个内嵌函数,只有input() 这个函数
  1.6 python 单引号,双引号,三引号以及单反引号,转义符”\”的使用
  输出一个字符串,里面包含一个整型变量
  In : temp = 43
  In : print 'The tempreature is ‘ + temp
  File "<ipython-input-209-8542dd1140c6>", line 1
  print 'The tempreature is " + temp
  ^
  SyntaxError: EOL while scanning string literal
  In : print 'The tempreature is ' + `temp`
  The tempreature is 43
  In : print 'The tempreature is ' + 'temp'
  The tempreature is temp
  #使用反引号将temp括起来
  In : print 'The tempreature is ' + repr(temp)
  The tempreature is 43
  注意python 3 不支持反引号
  print '''This is a very long string.
  It continues here.
  And it's not over yet
  Hello, world!"
  Still here.'''
  字符串中既有双引号也有单引号,可以考虑使用三个单引号或者3个双引号将这个字符串括起来。而且三引号支持字符串跨行。同样也可以用于注释多行代码
  如果字符串中既有双引号也有单引号不想使用三引号,那么单双引号需要转义(使用反斜线\)
  原始字符串
  In : print 'C:\\Program Files\\fnord\\foo\\bar\\baz\\frozz\\bozz'
  C:\Program Files\fnord\foo\bar\baz\frozz\bozz
  In : print r'C:\\Program Files\\fnord\\foo\\bar\\baz\\frozz\\bozz'
  C:\\Program Files\\fnord\\foo\\bar\\baz\\frozz\\bozz
  In : print r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz'
  C:\Program Files\fnord\foo\bar\baz\frozz\bozz
  原始字符串以r开头,输入什么就输出什么,一些python特有的字符串在原始字符串哪里没有特殊意义。最尾部不能以反斜线结尾
  pytho 2
  In : print r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz\'
  File "<ipython-input-221-755d0826b88d>", line 1
  print r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz\'
  ^
  SyntaxError: EOL while scanning string literal
  In : print r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz' + '\\'
  C:\Program Files\fnord\foo\bar\baz\frozz\bozz\
  python 3
  In : print (r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz\')
  File "<ipython-input-151-4570817c0148>", line 1
  print (r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz\')
  ^
  SyntaxError: EOL while scanning string literal
  In : print (r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz' + '\\')
  C:\Program Files\fnord\foo\bar\baz\frozz\bozz\
  在原始字符串中单引号,双引号和三引号可以输出,注意原始字符串r后面的字符需要用引号括起来,所以开始和结尾处使用的引号中间是不能出现的。如开始和结尾使用单引号扩起来,那么中间的字符串中就不能包含单引号.除非使用反斜线\,虽然这里的反斜线\不是转义,但是可以隔开引号的范围
  python 2
  In : print r'''"hello python",'hi python','''
  "hello python",'hi python',
  In : print r"test,'''python''',hello"
  test,'''python''',hello
  In : print r"test,'''python''',hello,\"kong\""
  test,'''python''',hello,\"kong\"
  In : print r"test,'''python''',hello,"kong""
  File "<ipython-input-240-ce337e291739>", line 1
  print r"test,'''python''',hello,"kong""
  ^
  SyntaxError: invalid synta
  python 3
  In : print (r'''Hello python,"test",'hi python'.''')
  Hello python,"test",'hi python'.
  In : print (r'''Hello python,"test",'hi python'''')
  File "<ipython-input-155-cd93259d7f5d>", line 1
  print (r'''Hello python,"test",'hi python'''')
  ^
  SyntaxError: EOL while scanning string literal
  In : print (r'''Hello python,"test",'hi python',\'''kong\'''''')
  Hello python,"test",'hi python',\'''kong\'
  In : print (r'''Hello python,"test",'hi python',\'''kong\'''.''')
  Hello python,"test",'hi python',\'''kong\'''.
  python 默认情况下普通字符串在内部以ascII码存储。
  Unicode编码前面加u
  In : print u'Hello, world!'
  Hello, world!
  In : u'Hello, world!'
  Out: u'Hello, world!'
  注意python3 普通字符串在内部已Unicode编码存储
页: [1]
查看完整版本: python 基础教程 笔记 一