python笔记一 入门py

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);
<class 'int'>
>>> type(3.0);
<class 'float'>
>>> type('str');
<class 'str'>
>>> type([1,2,3]);
<class 'list'>
>>> type((1,2,3));
<class 'tuple'>

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 "<pyshell#18>", line 1, in <module>
    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 Settings\Administrator>cd c:\python34
C:\Python34>python helloworld.py
hello world

行了,今天就水到这里,别问我,为什么用dos,为什么用xp。 作为一个很早就接触 计算机的人,xp和dos充满着曾经的回忆。windows系统其实一直都是差不多的。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第2章 基本语法 2.1 概述 基本句法和变量 语句 JavaScript程序的执行单位为行(line),也就是一...
    悟名先生阅读 4,199评论 0 13
  • 0. 序言 python 是一门脚本语言:封装性强,语法简洁,开发效率高,只用函数open就可以打开一个txt文件...
    付凯强阅读 1,262评论 0 5
  • 其实很久以来就想动笔写刀郎了,但总是怕不够了解写的太片面。所以便一直拖着,偶然的机会让那份冲动愈加强烈,于是终于还...
    逸蓂阅读 1,442评论 0 1
  • 中午弟弟在睡午觉,哥哥骑着滑板车在屋里穿来穿去,一会弄出一点声响。我让他保持安静!然后我抽空去洗衣服和打扫卫生没再...
    小咕咚和小太阳的麻麻阅读 198评论 0 0
  • 论瘾君子们!世人只知大麻病毒上瘾! 不知瘾君子无数,瘾行之人百分之九十九!五十总笑百步,可笑之极! 文人瘾于文字!...
    纵情嬉戏天地间阅读 209评论 2 3