id()函数,是python内置函数,查看每一个对象的地址。
>>> help(id);
Help on built-in function id in module builtins:
id(...)
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)
>>> id(3);
505986504
>>> id(3);
505986504
>>> id(3.0);
20256768
type();函数查看对象的类型。
>>> type(3);>>> type(3.0); >>> type('str'); >>> type([1,2,3]); >>> type((1,2,3));
python中对象有类型,变量无类型,和js很想,这让我们c系类的语言者有些难受。
>>> x=66; >>> x 66 >>> x="dflx"; >>> x 'dflx'
python支持大整数运算,学过计算机组成原理的 都知道,计算机进行浮点数运算的时候会出现溢出现象。
>>> 2**64; 18446744073709551616 >>> 2**173; 11972621413014756705924586149611790497021399392059392
python只是解决了大整数运算,但是浮点数和整数不同,它存在上下限,超过会溢出。
>>> 1000.0**8000 Traceback (most recent call last): File "", line 1, in 1000.0**8000 OverflowError: (34, 'Result too large')
除法运算,在python3中,要取整,需要 \
>>> 2/5; 0.4 >>> 2//5; 0 >>> 5//2; 2
异常的计算,不必纠结。
>>> 10.0/3 3.3333333333333335 >>> 0.1+0.2 0.30000000000000004
出现这种问题,主要在十进制和二进制的转换,比如0.1想要转换成二进制,会是一个循环的二进制表示不出来。
解决上面的问题,可以采用轮子,python中的模板来解决。
>>> import decimal;
>>> a=decimal.Decimal("10.0");
>>> b=decimal.Decimal("3");
>>> a/b;
Decimal('3.333333333333333333333333333')
四舍五入
>>> round(3.1415926,3);
3.142
>>> help(round);
Help on built-in function round in module builtins:
round(...)
round(number[, ndigits]) -> number
Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.
math库,是我最喜欢的函数库,可以进行数学运算,如果在小屁孩面前展示其功能一定会惊讶它们,作为一个数学老师,会几何画板,已经可以让学生们认为你很厉害了,如果会mathematica,甚至python会让它们更加崇拜;。
>> import math; >>> dir(math); ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
展示一下其功能
>>> math.pi; 3.141592653589793 >>> math.e; 2.718281828459045 >>> pow(2,3); 8 >>> math.sqrt(4); 2.0
编写了一个helloworld.py的python文件
#!/user/bin/env python #coding:utf-8 str_a="hello world"; print(str_a)
在dos中运行。
Microsoft Windows XP [版本 5.1.2600] (C) 版权所有 1985-2001 Microsoft Corp. C:documents and SettingsAdministrator>cd c:python34 C:Python34>python helloworld.py hello world
行了,今天就水到这里,别问我,为什么用dos,为什么用xp。 作为一个很早就接触 计算机的人,xp和dos充满着曾经的回忆。windows系统其实一直都是差不多的。



