python 基础1

python 基础教程(笔记)

说明 所有的代码都会在python 2.7.5 python 3.6.1 测试。如果效果相同只给一份,如果因语法不同会分别给出

目录

第一章 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 参数也可以实现普通除法运算

[root@sit ~]# python -Qnew

Python 2.7.5 (default, Nov 6 2016, 00:28:07)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> 1 / 2

0.5

注意Python3 默认是执行普通除法运算

[root@sit ~]# python3

Python 3.6.1 (default, May 31 2017, 18:02:43)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] 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 [1]: 5.4 // 1.8

Out[1]: 3.0

In [2]: 5.4 // 1.8

Out[2]: 3.0

 

In [5]: 5.4 % 1.8

Out[5]: 2.220446049250313e-16

 

虽然Python中浮点数也支持取模运算,但是对浮点数进行运算的时候要格外注意精度的问题。5.4 / 1.8 =3 正好整除,按理应该是0,可是这里的值不为0,很奇怪???

 

幂运算(乘方) 比一元取反运算优先级要高

In [10]: -2 ** 4

Out[10]: -16

 

In [11]: (-2) ** 4

Out[11]: 16

 

In [12]: -(2 ** 4)

Out[12]: -16

-2 ** 4 = -(2 ** 4)

python 2 和 python 3 对于整形数字超出范围的自动转换为长整型,不需要过分的关注整型的取值范围,因为python能够处理很长的数字。可以手动的数字后面加一个大写的L就表示这是一个长整型。当然小写的l也可以只是这里看起来很像数字1,所以建议使用大写的L

注意python 2.2 及以前整型值超出范围会报错。-2147483648 -- 2147483647

python 2

In [15]: 214748364789987

Out[15]: 214748364789987

 

In [16]: 214748364789987568978945465 * 4654654167897893212313212312313212313132 + 5213

Out[16]: 999579371218972816926339919579932791950025118287985996498731351593L

 

In [17]: 4654654167897893212313212312313212313132

Out[17]: 4654654167897893212313212312313212313132L

 

Python 3

In [11]: 214748364789987

Out[11]: 214748364789987

 

In [12]: 214748364789987568978945465

Out[12]: 214748364789987568978945465

 

In [13]: 214748364789987568978945465 * 4654654167897893212313212312313212313132 + 5213

Out[13]: 999579371218972816926339919579932791950025118287985996498731351593

 

1.2 python 2.x 和 python 3.x print 的区别

python print。python 2 直接输出即可,python 3 print 变成了函数

python 2

In [18]: print 3 + 3

6

python 3

In [14]: print (2 + 3)

5

 

In [15]: 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 [20]: s = input("The meaning of life:")

The meaning of life:45

 

In [21]: s

Out[21]: 45

 

In [22]: type(s)

Out[22]: int

In [27]: 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 [31]: x = input("x: ")

x: 34

 

In [32]: y = input("y: ")

y: 23

 

In [33]: x * y

Out[33]: 782

 

python 3

In [17]: s = input("The meaning of life:")

The meaning of life:67

 

In [18]: s

Out[18]: '67'

 

In [19]: type(s)

Out[19]: str

 

In [23]: s = input("The meaning of life:")

The meaning of life:'980'

 

In [24]: type(s)

Out[24]: str

 

In [25]: s = input("The meaning of life:")

The meaning of life:abc

 

In [26]: s

Out[26]: 'abc'

 

In [33]: y = int(input("y: "))

y: 23

 

In [34]: x = int(input("x: "))

x: 34

 

In [35]: x * y

Out[35]: 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 [34]: pow(2,3)

Out[34]: 8

 

In [35]: 2 ** 3

Out[35]: 8

 

In [1]: 10 + pow(2,3*5)/3.0

Out[1]: 10932.666666666666

注意python的版本不同精度有所不同,本实验所使用的两个版本的精度一致

 

内建函数 abs 求绝对值

In [1]: abs(-10)

Out[1]: 10

In [2]: abs(10)

Out[2]: 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 [59]: b = "0.49999999999999999"

In [60]: len(b)

Out[60]: 19

 

In [61]: round(0.49999999999999999)

Out[61]: 1.0

 

In [62]: round(0.4999999999999999)

Out[62]: 0.0

 

python 3

In [74]: round(0.50000000000000001)

Out[74]: 0

 

In [75]: round(0.5000000000000001)

Out[75]: 1

 

In [72]: b = '0.5000000000000001'

 

In [73]: len(b)

Out[73]: 18

 

两个版本相同。注意这里应该是个陷阱,四舍五入是逢五进一

列一

In [76]: round(5.005,2)

Out[76]: 5.0

 

In [77]: round(5.006,2)

Out[77]: 5.01

 

列二

In [78]: round(5.32,2)

Out[78]: 5.32

In [79]: round(5.325,2)

Out[79]: 5.33

In [83]: round(5.324,2)

Out[83]: 5.32

 

列三

In [120]: round(1.675,2)

Out[120]: 1.68

 

In [121]: round(2.675,2)

Out[121]: 2.67

 

列四

In [123]: round(-1.436,2)

Out[123]: -1.44

 

In [124]: round(-1.433,2)

Out[124]: -1.43

 

这两个例子,第一个例子是逢六进一,第二个例子是逢五进一。还有一个问题需要注意:round(5.001,2) 输出5.0而不是5.00.这个问题也很奇怪这样精度就是1而不是2。第三个列子说明有时候是逢五进一,有时候是逢六进一

 

math.floor() 这个函数需要导入math模块 import math 才可以使用。

作用:向下取整,将小数部分抹掉,但是注意这里有个精度的问题

python 2 返回的是浮点数,python 3 返回的是整型

python 2

In [67]: math.floor(32.999999999999999)

Out[67]: 33.0

In [68]: math.floor(32.99999999999999)

Out[68]: 32.0

In [70]: a = '32.999999999999999'

In [71]: len(a)

Out[71]: 18

大于 或等于32.999999999999997,小于33 math.floor返回33.0

大于32,小于或等于32.999999999999996 math.floor 返回32.0

python 3

In [103]: math.floor(32.99999999999999)

Out[103]: 32

 

In [104]: math.floor(32.999999999999997)

Out[104]: 33

 

In [105]: b = '32.999999999999997'

 

In [106]: len(b)

Out[106]: 18

大于 或等于32.999999999999997,小于33 math.floor返回33

大于32,小于或等于32.999999999999996 math.floor 返回32

 

math.ceil(x) 这个函数向上取整,当x为小数的时候去掉小数部分,整数部分+1

python 2 返回的浮点值

In [121]: math.ceil(4.0000000000000004)

Out[121]: 4.0

 

In [122]: math.ceil(4.0000000000000005)

Out[122]: 5.0

4.0000000000000005 <= x < 5 math.ceil(x) 返回5.0

4 <= x < 4.0000000000000004 math.ceil(x) 返回4.0

 

python 3 返回的是整型

In [121]: math.ceil(4.0000000000000004)

Out[121]: 4

 

In [122]: math.ceil(4.0000000000000005)

Out[122]: 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 [141]: from math import sqrt

 

In [142]: sqrt(9)

Out[142]: 3.0

 

In [143]: sqrt(15)

Out[143]: 3.872983346207417

 

In [144]: help(sqrt)

 

 

In [145]: sqrt(-9)

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

<ipython-input-145-4abb6f6b3805> in <module>()

----> 1 sqrt(-9)

 

ValueError: math domain error

 

使用变量引用的方式来使用函数,这样的方式更精简,适用。

In [151]: foo = math.sqrt

 

In [152]: foo(36)

Out[152]: 6.0

 

math.sqrt() 这个函数只能处理浮点数,而负数的平方根是虚数。而虚数(以及复数,虚数和实数之和)是完全不同的,这里需要使用使用另外一个包中函数。cmath.sqrt()

In [138]: import cmath

In [139]: square_root = cmath.sqrt

In [140]: square_root(-36)

Out[140]: 6j

In [141]: square_root(-49)

Out[141]: 7j

 

注意这里没有使用from ... import ... 语句,如果使用 from cmanth import sqrt 那么不能直接使用sqrt 因为前面有使用 from math import sqrt。所以一般情况下最好还是使用import语句,不要使用from .. import ...。如果不想写前置,可以使用变量引用的方式简化函数调用

后缀带j表示虚数,python本身也支持虚数

In [142]: (1+3j) * (9+4j)

Out[142]: (-3+31j)

 

1.5input 与 raw_input 的区别

input 和 raw_input 区别

1、input 接受标准的python表达式,如果是字符串需要使用引号括起来。而raw_input 接受任意类型的输入

2、对于数字来讲,raw_input 将数字当做字符串,而input把输入的数字当做int,float 等类型

 

1

In [201]: c = raw_input(3 + 5)

8

In [202]:

In [173]: input(1+3)

4

File "<string>", line unknown

 

^

SyntaxError: unexpected EOF while parsing

input 直接敲回车会报错,因为没有输入内容

 

2

In [205]: raw_input("what number is: ")

what number is: 56

Out[205]: '56'

 

In [206]: input("what number is: ")

what number is: 56

Out[206]: 56

 

3

In [202]: raw_input("what is your name: ")

what is your name: zhangsan

Out[202]: 'zhangsan'

 

In [203]: 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 [204]: input("what is your name: ")

what is your name: 'zhangshan'

Out[204]: 'zhangshan'

 

input() 本质上还是使用 raw_input() 来实现的,只是调用完 raw_input() 之后再调用 eval() 函数,所以,你甚至可以将表达式作为 input() 的参数,并且它会计算表达式的值并返回它。

除非有特殊需要,一般都不使用input() 这个函数,使用raw_input()

python 3 中没有raw_input() 这个内嵌函数,只有input() 这个函数

 

1.6 python 单引号,双引号,三引号以及单反引号,转义符”\”的使用

输出一个字符串,里面包含一个整型变量

In [208]: temp = 43

In [209]: print 'The tempreature is ‘ + temp

File "<ipython-input-209-8542dd1140c6>", line 1

print 'The tempreature is " + temp

^

SyntaxError: EOL while scanning string literal

In [212]: print 'The tempreature is ' + `temp`

The tempreature is 43

 

In [213]: print 'The tempreature is ' + 'temp'

The tempreature is temp

#使用反引号将temp括起来

In [214]: 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 [218]: print 'C:\\Program Files\\fnord\\foo\\bar\\baz\\frozz\\bozz'

C:\Program Files\fnord\foo\bar\baz\frozz\bozz

 

In [219]: print r'C:\\Program Files\\fnord\\foo\\bar\\baz\\frozz\\bozz'

C:\\Program Files\\fnord\\foo\\bar\\baz\\frozz\\bozz

 

In [220]: 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 [221]: 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 [222]: print r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz' + '\\'

C:\Program Files\fnord\foo\bar\baz\frozz\bozz\

 

python 3

In [151]: 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 [152]: print (r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz' + '\\')

C:\Program Files\fnord\foo\bar\baz\frozz\bozz\

 

在原始字符串中单引号,双引号和三引号可以输出,注意原始字符串r后面的字符需要用引号括起来,所以开始和结尾处使用的引号中间是不能出现的。如开始和结尾使用单引号扩起来,那么中间的字符串中就不能包含单引号.除非使用反斜线\,虽然这里的反斜线\不是转义,但是可以隔开引号的范围

python 2

In [235]: print r'''"hello python",'hi python','''

"hello python",'hi python',

 

In [236]: print r"test,'''python''',hello"

test,'''python''',hello

 

In [239]: print r"test,'''python''',hello,\"kong\""

test,'''python''',hello,\"kong\"

 

In [240]: 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 [154]: print (r'''Hello python,"test",'hi python'.''')

Hello python,"test",'hi python'.

 

In [155]: 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 [156]: print (r'''Hello python,"test",'hi python',\'''kong\'''''')

Hello python,"test",'hi python',\'''kong\'

 

In [157]: print (r'''Hello python,"test",'hi python',\'''kong\'''.''')

Hello python,"test",'hi python',\'''kong\'''.

 

python 默认情况下普通字符串在内部以ascII码存储。

Unicode编码前面加u

In [3]: print u'Hello, world!'

Hello, world!

 

In [4]: u'Hello, world!'

Out[4]: u'Hello, world!'

 

注意python3 普通字符串在内部已Unicode编码存储

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值