龙空技术网

Python系列基础 每天跟我学 玩转Python数据分析技术(2)

优悠严选 140

前言:

今天小伙伴们对“python引入pi”都比较着重,同学们都需要分析一些“python引入pi”的相关文章。那么小编在网络上搜集了一些有关“python引入pi””的相关知识,希望各位老铁们能喜欢,小伙伴们一起来了解一下吧!

Python笔记2

10.动态引用,强类型

python中的对象引用没有与之关联的类型信息。

a = 5

type(a)

int

b = 'foo'

type(b)

str

对象的类型信息是保存在它自己内部的。

In [14]: '5' + 5

TypeError: cannot concatenate 'str' and 'int' objects

pyhton可以是一种强类型语言。像'5' + 5('5'-->5或者5-->'5')这种隐式转换只在很明显的情况下才会发生。

In [15]: a = 4.5

In [16]: b = 2

In [17]: print 'a is %s, b is %s' %(type(a), type(b))

a is <type 'float'>, b is <type 'int'>

In [18]: a / b

Out[18]: 2.25

11.isinstance(),可以检查一个对象是否是某个特定类型的实例。

In [26]: a = 5

In [27]: isinstance(a, int)

Out[27]: True

In [28]: isinstance(a, (int, float)) ##可以是元组

Out[28]: True

In [29]: isinstance('a', (int, float))

Out[29]: False

In [30]: isinstance(2.3, (int, float))

Out[30]: True

12.属性和方法

属性attribute 存储在该对象‘内部’的其他Python对象

方法method 与该对象有关的 能够访问其内部数据的函数

访问语法 obj.attribute_name

In [40]: a = 'hello'

In [41]: a.count('l')

Out[41]: 2

In [42]: a.isupper()

Out[42]: False

13.iter()函数

In [47]: def isiterable(obj):

...: try:

...: iter(obj)

...: return True

...: except TypeError: # 不能迭代

...: return False

...:

#对于字符串和大部分Python集合类型,该函数会返回True

In [48]: isiterable('abcdefg')

Out[48]: True

In [49]: isiterable([1, 2, 3])

Out[49]: True

In [50]: isiterable(5)

Out[50]: False

编写可以接受任何序列(列表、元组...)或迭代器的函数。可以先检查对象是不是列表(或者Numpy),如果不是,转换一下

if not isinstance(x, list) and isiterable(x):

x = list(x)

14.导入 import

在Python中,模块(module)就是一个含有函数和变量定义 以及从其他.py文件引入的此类东西的.py文件

[hello_python.py]

# hello_python.py

PI = 3.14159

print 'hello world'

def f(x):

return x + 2

def g(a, b):

return a + b

##第一种方式

In [3]: import hello_python #不加.py

In [4]: result = hello_python.f(5)

In [5]: result

Out[5]: 7

In [6]: pi = hello_python.PI

In [7]: pi

Out[7]: 3.14159

##第二种方式

In [8]: from hello_python import f, g, PI

In [9]: result = g(5, PI)

In [10]: result

Out[10]: 8.14159

In [12]: import hello_python as hp

In [13]: from hello_python import PI as pi, g as gf

In [14]: r1 = hp.f(pi)

In [15]: r1

Out[15]: 5.14159

In [17]: r2 = gf(6, pi)

In [18]: r2

Out[18]: 9.14159

15.二元运算符合比较运算符

In [21]: 3 - 8

Out[21]: -5

In [22]: 11 + 13

Out[22]: 24

In [23]: 5 <= 2

Out[23]: False

16.判断两个引用是否指向同一个对象,用 is 关键字 相反的是 is not:

In [24]: a = [1, 2, 3]

In [25]: b = a

In [26]: c = list(a)

In [27]: a is b

Out[27]: True

In [28]: a is c

Out[28]: False

In [29]: a is not c

Out[29]: True

##a和c的内容是相等的,引用是不一样的。

In [34]: a == c

Out[34]: True

In [30]: a

Out[30]: [1, 2, 3]

In [31]: b

Out[31]: [1, 2, 3]

In [32]: c

Out[32]: [1, 2, 3]

is 和 is not常常用于判断变量是否为None,None的实例只有一个:

In [35]: a is None

Out[35]: False

In [37]: d = None

In [38]: d is None

Out[38]: True

17.二元运算符

+ - * / & | ^ == != <= >=

is not 、is

a ** b ##a的b次方

a // b ##a除以b后向下圆整,丢弃小数部分

##10 // 3 结果是3

##30 // 8 结果是3 30 / 8 = 3.75

18.严格和懒惰(延迟计算)

##像这一类的就是实时计算的。

In [48]: a = b = c = 3

In [49]: d = a + b * c

In [50]: d

Out[50]: 12

像Python技术中的迭代器和生成器可以用于实现延迟计算(执行高负荷的时刻)。

19.可变和不可变的对象

大部分Python对象是可变(mutable)的,像列表、字典、Numpy数组、自定义类型(类)

In [51]: l = ['f', 2, [3,6]]

In [52]: l[2]

Out[52]: [3, 6]

In [53]: l[2] = (3, 4)

In [54]: l

Out[54]: ['f', 2, (3, 4)]

其他的(如字符串和元组等)则是不可变的(immutable)

In [55]: t = (3, 5, (4,7))

In [56]: t[1]

Out[56]: 5

In [57]: t[1] = 6

TypeError: 'tuple' object does not support item assignment

建议:尽量使用不变性。

20.标量类型

python有一些用于处理数值数据、字符串、布尔值(True或False),以及日期/时间的内置类型

标准的Python标量类型:

类型说明

NonePython的null值只有一个实例

str字符串类型

unicodeUnicode字符串类型

float双精度浮点数。没有double类型

boolTrue或False

int有符号整数,其最大值由平台决定

long任意精度的有符号整数。大的int值会被自动转换为long

标签: #python引入pi