前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python笔记一 入门py

python笔记一 入门py

作者头像
热心的社会主义接班人
发布2018-04-27 14:43:48
7340
发布2018-04-27 14:43:48
举报
文章被收录于专栏:cscscs

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系统其实一直都是差不多的。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.03.26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档