深夜提醒

现在是深夜,建议您注意休息,不要熬夜哦~

🏮 🏮 🏮

新年快乐

祝君万事如意心想事成!

2024 桐庐半程马拉松
00:00:00
时间
0.00
距离(公里)
--:--
配速
--
步频
--
心率 (bpm)
--
配速
步频
|
share-image
ESC

基础:Python 数字类型变量

温故而知新

Python 可以自动转换类型,我们称之为动态类型,本文主要是介绍数字类型的变量和方法。

➜  www python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a=34
>>> type(a) # a 是 init 型
<type 'int'>
>>> a=3.4
>>> type(a) # a 是 float 型
<type 'float'>
>>> 0.3*3 # 会丢失精度
0.8999999999999999
>>> print 0.3*3 # print 会把结果人性化的显示
0.9
>>> 0.3/3
0.09999999999999999
>>> print 0.3/3
0.1
>>> from decimal import Decimal as D #调用 decimal 来解决精度问题
>>> D('0.3')*D(3)
Decimal('0.9')
>>> print D('0.3')*D(3)
0.9
>>> D('0.3')/D(3)
Decimal('0.1')
>>> print D('0.3')/D(3)
0.1

其中from decimal import Decimal as D 是指调用 decimal 库并把 decimal 设置为别名 D

>>> 1/2**10000 # 数字太大无法显示
0L
>>> 1.0/2**10000 #直接报错了
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float
>>>

要表示1的10000次方

>>> D(1)/D(2**10000)
Decimal('5.012372749206452009297555934E-3011')

decimal 的缺点就是执行时间比 float 长

➜  www python -mtimeit -s "from decimal import Decimal as D" "D('1.2')+D('3.4')"
10000 loops, best of 3: 24.7 usec per loop # decimal 的时间最长
➜ www python -mtimeit -s "from decimal import Decimal as D" "1.2+3.4"
100000000 loops, best of 3: 0.0157 usec per loop #两个 float 直接相加时间最段
➜ www python -mtimeit -s "from decimal import Decimal as D" "float('1.2')+float('3.4')"
1000000 loops, best of 3: 0.338 usec per loop # 加了字符串转换也会增加时间

和数字相关的库

1.math


>>> import math
>>> math.pi
3.141592653589793
>>> math.sqrt(80)
8.94427190999916
>>> math.log10(2**1000)
301.0299956639812
>>> math.pow(1,5)
1.0
>>> math.factorial(5)
120

2.random

>>> import random
>>> random.random()
0.1797989878351397
>>> random.choice([1,2,3])
2
>>> random.randint(2,5)
2
>>> random.uniform(4,5)
4.374124742261563
>>> random.gauss(2,34)
87.84062004735456

3.dir

>>> dir(math)
['__doc__', '__file__', '__name__', '__package__', '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', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

4.help

>>> help(math)

random.random()可以生成一个0-1之间的随机数,比如说我们希望产生一个概率中奖概率20%,那么可以random.random()>0.8 当他是 true 就是中奖了,否则没中奖。

>>> random.random()
0.6586755279600274
>>> random.random()
0.7146088640324266
>>> random.random()
0.9007269952467781
>>> random.random()
0.9163585110005992
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
True
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
False
>>> random.random()>0.8
True
>>> random.random()>0.8
False
>>> random.random()>0.8
True
>>> random.random()>0.8
True
>>>

科学计算

1.numpy 产生数组和矩阵,正态分布的随机数,矩阵运算,举证求逆,转置等操作
2.scipy 拟合,线性插值,样条插值,积分,微分,触非线性方程,滤波器设计

文章作者:阿文
文章链接: https://www.awen.me/post/4951.html
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿文的博客
本文于 2017-09-04 15:49 发布,已超过半年(3114天),请注意甄别内容是否已过期。

评论

0 条评论
😀😃😄 😁😅😂 🤣😊😇 🙂🙃😉 😌😍🥰 😘😗😙 😚😋😛 😝😜🤪 🤨🧐🤓 😎🥸🤩 🥳😏😒 😞😔😟 😕🙁☹️ 😣😖😫 😩🥺😢 😭😤😠 😡🤬🤯 😳🥵🥶 😱😨😰 😥😓🤗 🤔🤭🤫 🤥😶😐 😑😬🙄 😯😦😧 😮😲🥱 😴🤤😪 😵🤐🥴 🤢🤮🤧 😷🤒🤕 🤑🤠😈 👿👹👺 🤡💩👻 💀☠️👽 👾🤖🎃 😺😸😹 😻😼😽 🙀😿😾 👍👎👏 🙌👐🤲 🤝🤜🤛 ✌️🤞🤟 🤘👌🤏 👈👉👆 👇☝️ 🤚🖐️🖖 👋🤙💪 🦾🖕✍️ 🙏💅🤳 💯💢💥 💫💦💨 🕳️💣💬 👁️‍🗨️🗨️🗯️ 💭💤❤️ 🧡💛💚 💙💜🖤 🤍🤎💔 ❣️💕💞 💓💗💖 💘💝💟 ☮️✝️☪️ 🕉️☸️✡️ 🔯🕎☯️ ☦️🛐 🆔⚛️🉑 ☢️☣️📴 📳🈶🈚 🈸🈺🈷️ ✴️🆚💮 🉐㊙️㊗️ 🈴🈵🈹 🈲🅰️🅱️ 🆎🆑🅾️ 🆘 🛑📛 🚫💯💢 ♨️🚷🚯 🚳🚱🔞 📵🚭 ‼️⁉️🔅 🔆〽️⚠️ 🚸🔱⚜️ 🔰♻️ 🈯💹❇️ ✳️🌐 💠Ⓜ️🌀 💤🏧🚾 🅿️🈳 🈂🛂🛃 🛄🛅🛗 🚀🛸🚁 🚉🚆🚅 ✈️🛫🛬 🛩️💺🛰️
您的评论由 AI 智能审核,一般1分钟内会展示,若不展示请确认你的评论是否符合社区和法律规范
加载中...

选择联系方式

留言反馈

😀😃😄 😁😅😂 🤣😊😇 🙂🙃😉 😌😍🥰 😘😗😙 😚😋😛 😝😜🤪 🤨🧐🤓 😎🥸🤩 🥳😏😒 😞😔😟 😕🙁☹️ 😣😖😫 😩🥺😢 😭😤😠 😡🤬🤯 😳🥵🥶 😱😨😰 😥😓🤗 🤔🤭🤫 🤥😶😐 😑😬🙄 😯😦😧 😮😲🥱 😴🤤😪 😵🤐🥴 🤢🤮🤧 😷🤒🤕 🤑🤠😈 👿👹👺 🤡💩👻 💀☠️👽 👾🤖🎃 😺😸😹 😻😼😽 🙀😿😾 👍👎👏 🙌👐🤲 🤝🤜🤛 ✌️🤞🤟 🤘👌🤏 👈👉👆 👇☝️ 🤚🖐️🖖 👋🤙💪 🦾🖕✍️ 🙏💅🤳 💯💢💥 💫💦💨 🕳️💣💬 👁️‍🗨️🗨️🗯️ 💭💤❤️ 🧡💛💚 💙💜🖤 🤍🤎💔 ❣️💕💞 💓💗💖 💘💝💟 ☮️✝️☪️ 🕉️☸️✡️ 🔯🕎☯️ ☦️🛐 🆔⚛️🉑 ☢️☣️📴 📳🈶🈚 🈸🈺🈷️ ✴️🆚💮 🉐㊙️㊗️ 🈴🈵🈹 🈲🅰️🅱️ 🆎🆑🅾️ 🆘 🛑📛 🚫💯💢 ♨️🚷🚯 🚳🚱🔞 📵🚭 ‼️⁉️🔅 🔆〽️⚠️ 🚸🔱⚜️ 🔰♻️ 🈯💹❇️ ✳️🌐 💠Ⓜ️🌀 💤🏧🚾 🅿️🈳 🈂🛂🛃 🛄🛅🛗 🚀🛸🚁 🚉🚆🚅 ✈️🛫🛬 🛩️💺🛰️