`
friendsys
  • 浏览: 338813 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

Learning.Python.3rd.Edition 笔记[4]

阅读更多
第四章中很大概的介绍了下Python的core 数据类型,后续章节将会详细介绍个个数据类型

CHAPTER 5
Number

Python支持无限大的数字,不过需要内存支持

具体的Number类型包括以下几种
1234, -24, 0 Normal integers (C longs)
9999999999999999999L Long integers (unlimited size)
1.23, 3.14e-10, 4E210, 4.0e+210 Floating-point numbers (C doubles)
0177, 0x9ff, 0XFF Octal and hex literals for integers
3+4j, 3.0+4.0j, 3J Complex number literals

Python会自动对数字进行转换,将带小数点的转换成 floating-point类型

十六进制与八进制
八进制 以0开头,后面跟随0-7的数子
十六进制 以0x或者0X开头,后面包含0-9和A-F

用于处理Number的工具

Expression operators  表达式操作
+, *, >>, **, etc.

Built-in mathematical functions  内置数字操作函数
pow, abs, etc.

Utility modules    工具模块
random, math, etc.

如果需要更多的Number操作,可以使用第三方的NumPy,在3.0中,将会只有一个integer类型,提供了高精度


Python中的运算符和优先级

yield x Generator function send protocol (new in Release 2.5)
lambda args: expression Anonymous function generation
x if y else z Ternary selection expression (new in Release 2.5)
x or y Logical OR (y is evaluated only if x is false)
x and y Logical AND (y is evaluated only if x is true)
not x Logical negation
x < y,x <= y,x > y,x >= y,x == y,x <> y,x != Comparison operators, value equality operators,a object
y, x is y, x is not y, x in y, x not in y identity tests, sequence membership
x | y Bitwise OR
x ^ y Bitwise eXclusive OR
x & y Bitwise AND
x << y, x >> y Shift x left or right by y bits
-x + y, x – y Addition/concatenation, subtraction
x * y, x % y, x / y, x // y Multiplication/repetition, remainder/format, divisionb
-x, +x, ~x, x ** y Unary negation, identity, bitwise complement, binary power
x[i], x[i:j], x.attr, x(...) Indexing, slicing, qualification, function calls
(...), [...], {...}, `...` Tuple, list,c dictionary, conversion to stringd


其中包括三元运算符
c=5 if False else 6
其中False部分可以使用不同的bool表达式进行替换,其他情况非0的 均为true

上表中,运算优先级 有低到高排序,如同Java一样,可以使用()进行优先级运算

当混合Integer与浮点类型时,Python首先会自动将Integer转换成浮点,然后进行计算,与Long类型混合时,同样也会进行自动

的类型转换

处理数字时,Python解释器所使用的数字与使用print函数打印出来的数字并不相同,可以使用repr()进行查看,也可以在

common line下进行查看,同样也可以使用str()进行转换字符串,不过直接使用变量进行赋值即可
print 0.5/0.3
print repr(0.5/0.3)


/运算与//的区别, 后者将返回一个整数,前者则能带小数点

bitwise 位操作  << | &等

Long Integer, Python同样可以使用L,声明一个变量为Long类型

Complex Numbers 使用j或者J结尾的数字表达,具体用途未知..需进一步了解

Hexadecimal and Octal Notation  16进制和8进制
8进制使用0开头,后面包含0-7的数字,长度为3位
16进制使用0x或0X开头,后面跟随0-9或A-F(忽略大小写),长度为4位

可以使用hex()和oct()进行进制转换的操作,参数为int类型, 也可以使用int('',16) 等进行逆向转换

也可以使用 eval('0x40') 进行转换成int类型
在使用%进行字符串格式化时, 使用%o代表 8进制,x(忽略大小写)代表16进制:
"%o %x %X" % (64, 64, 255),

注意在进制操作时,Python都会将其转换成10进制进行显示

其他内置数字工具
math模块,包含了大多C下的math类库, 还有一些方法已经被内置,如: abs 绝对值, pow 幂运算,round,和int 等

random模块,提供随机数相关的操作,random( ) 返回0-1之间的浮点随机数, randint(1,10)返回指定区间的整数,
choice(),返回随机索引的元素,其中参数可以为List

Python的核心数字类型包含 integer, long integer,floating point, and complex(j)
在Python下提供了一种新的核心数字类型 decimal对象,需要导入该模块才可使用,看起来与浮点类型相似,不过提供
了更高的精度,常用于货币计算
声明格式如:
Decimal('0.1')

Sets Python2.4提供的一种新的集合容器,不过因为支持数学上的集合操作,所在这里也进行了介绍
创建的方式
x = set('abcde')
engineers = set(['bob', 'sue', 'ann', 'vic'])

支持 - | & 等操作,传统的数学上的集操作
Python3.0中 {}提供和sets一样的功能

Booleans 类型,所有非0的数字均为true,也许包括所有其他的对象 (None为false)
使用True and False 代替原来的 1 and 0


照旧凑字数,不过下面的问题的确不错..也很简单的单词
Chapter Quiz

1. What is the value of the expression 2 * (3 + 4) in Python?
2. What is the value of the expression 2 * 3 + 4 in Python?
3. What is the value of the expression 2 + 3 * 4 in Python?
4. What tools can you use to find a number’s square root, as well as its square?
5. What is the type of the result of the expression 1 + 2.0 + 3?
6. How could you truncate and round a floating-point number?
7. How can you convert an integer to a floating-point number?
8. How would you display an integer in octal or hexadecimal notation?
9. How might you convert an octal or hexadecimal string to a plain integer?

Quiz Answers

1. The result value will be 14, the result of 2 * 7, because the parentheses force the addition to happen before the multiplication.

2. Here, the result will be 10, the result of 6 + 4. Python’s operator precedence rules are applied in the absence of parentheses, and multiplication has higher precedence than (i.e., happens before) addition, per Table 5-2.

3. This expression yields 14, the result of 2 + 12, for the same precedence reasons as in the prior question.

4. Functions for obtaining the square root, as well as pi, tangents, and more, are
available in the imported math module. To find a number’s square root, import
math and call math.sqrt(N). To get a number’s square, use either the exponent
expression X** 2, or the built-in function pow(X, 2).

5. The result will be a floating-point number: the integers are converted up to floating point, the most complex type in the expression, and floating-point math is used to evaluate it.

6. The int(N) function truncates, and the round(N, digits?) function rounds.

7. The float(I) function converts an integer to a floating point; mixing an integer with a floating point within an expression will result in a conversion as well.
8. The oct(I) and hex(I) built-in functions return the octal and hexadecimal string forms for an integer. The % string formatting expression also provides targets for doing this.

9. The int(S, base?) function can be used to convert from octal and hexadecimal
strings to normal integers (pass in 8 or 16 for the base). The eval(S) function can be used for this purpose too, but it’s more expensive to run and can have security issues. Note that integers are always stored in binary in computer memory;these are just display string format conversions.








































分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics