>>> #python数字
>>>
>>> I = 10
>>> hex(I)
'0xa'
>>> oct(I)
'0o12'
>>> bin(I)
'0b1010'
>>> #上述是吧整数转化为十六进制、八进制、二进制
>>>
>>> #创建复数
>>> complex(1,2)
(1+2j)
>>>
>>>
>>> #内置数学工具和扩展
>>> #为操作
>>>
>>> x = 1
>>> x << 2
4
>>> x | 2
3
>>> x & 1
1
>>>
>>> #math模块
>>> import math
>>> math.pi, math.e
(3.141592653589793, 2.718281828459045)
>>> math.sin(2*math.pi/180)
0.03489949670250097
>>> pow(2,3),2**3
(8, 8)
>>> abs(-42.0), sum((1,2,3,4,5))
(42.0, 15)
>>> min(2,3,4), max(3,4,7)
(2, 7)
>>>
>>>
>>> #random模块
>>> import random
>>> random.random()
0.6041750198332752
>>> random.randint(1,20)
9
>>> random.randint(1,20)
10
>>> random.randint(1,20)
2
>>> random.choice(['life of me', 'life of you', 'life of he or she'])
'life of me'
>>> random.choice(['life of me', 'life of you', 'life of he or she'])
'life of me'
>>>
>>> #decimal模块 小数
>>>
>>> #分数
>>> from fractions import Fraction
>>> x = Fraction(1,3)
>>> y = Fraction(2,4)
>>> x ,y
(Fraction(1, 3), Fraction(1, 2))
>>> print(x),print(y)
1/3
1/2
(None, None)
>>> Fraction('0.25')
Fraction(1, 4)
>>> Fraction('.25')+Fraction('1.25')
Fraction(3, 2)
>>>
>>> dir(2.5)
['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']
>>> help(2.0.as_integer_ratio)
Help on built-in function as_integer_ratio:
as_integer_ratio(...) method of builtins.float instance
float.as_integer_ratio() -> (int, int)
Return a pair of integers, whose ratio is exactly equal to the original
float and with a positive denominator.
Raise OverflowError on infinities and a ValueError on NaNs.
>>> (10.0).as_integer_ratio()
(10, 1)
>>> (0.0).as_integer_ratio()
(0, 1)
>>> (-.25).as_integer_ratio()
(-1, 4)
>>> (0.7).as_integer_ratio()
(3152519739159347, 4503599627370496)
>>> #限制分母的大小
>>> a
(3152519739159347, 4503599627370496)
>>> a=(0.7).as_integer_ratio()
>>> a
(3152519739159347, 4503599627370496)
>>> a = Fraction(*(0.7).as_integer_ratio())
>>> a
Fraction(3152519739159347, 4503599627370496)
>>> a.limit_denominator()
Fraction(7, 10)
>>> #集合(set)
>>>
>>> x = set('asdddf')
>>> x
{'d', 'a', 'f', 's'}
>>> y = set('abc')
>>> y
{'a', 'c', 'b'}
>>> 'e' in y
False
>>> x -y
{'d', 's', 'f'}
>>> x | y
{'c', 'b', 's', 'd', 'a', 'f'}
>>> x & y
{'a'}
>>> x ^ y
{'d', 'f', 's', 'c', 'b'}
>>> #set method
>>> dir(x)
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
>>> z = x.intersection(y)
>>> z
{'a'}
>>> #上述相当于 x&y
>>> z.add('asd')
>>> z
{'asd', 'a'}
>>> z.update(set(['x','y']))
>>> z
{'asd', 'y', 'a', 'x'}
>>> z.remove('a')
>>> z
{'asd', 'y', 'x'}
>>>
>>>
>>> for item in set('abc'):print(item*3)
aaa
ccc
bbb
>>> s = set([1,2,,3])
SyntaxError: invalid syntax
>>> s = set([1,2,3])
>>> s
{1, 2, 3}
>>> s | set([3,4])
{1, 2, 3, 4}
>>> s
{1, 2, 3}
>>> s.union([3,4])
{1, 2, 3, 4}
>>> s.issubset(range(-5,5))
True
>>>
>>>
>>> L= [1,2,3,4,2,1,3,4,6]
>>> set(L)
{1, 2, 3, 4, 6}
>>> L = list(set(L))
>>> L
[1, 2, 3, 4, 6]
>>>
>>>
>>> #bool
>>> type(True)
<class 'bool'>
>>> type(2)
<class 'int'>
>>>
>>>
>>> #动态类型的介绍
>>> #在Python中,白能量类型是在运行过程中自动决定的,而不是通过声明
>>>
>>> a=3
>>> #该过程会执行三个步骤
>>> #1、创建一个对象来代表值3
>>> #2、创建一个变量a,如果它还没有的会啊
>>> #3、将变量与新的对象相连接
>>> #注意:Python中的类型是与对象相关,而不是和变量关联
>>>
>>> #Python的垃圾回收:每当一个变量名被赋予了一个新的对象,之前的那个对象所占的空间就会被收回
>>> #每个对象的头信息包含的类型信息
>>>
>>> #变量和对象之间的关系是引用,所以多个变量引用一个对象时,当一变量去修改该对象的一些东西时,所以变量都被修改。*(描述有问题)
>>> L1 = [1,2,3]
>>> L2=L1
>>> L2
[1, 2, 3]
>>> L1 = 24
>>> L1 ,L2
(24, [1, 2, 3])
>>> L1=L2
>>> L1,L2
([1, 2, 3], [1, 2, 3])
>>> L1[0]=24
>>> L1,L2
([24, 2, 3], [24, 2, 3])
>>>
>>>
>>> L2=L1[:]
>>> L1,L2
([24, 2, 3], [24, 2, 3])
>>> L1[0]=1
>>> L1,L2
([1, 2, 3], [24, 2, 3])
>>>
>>> #还可以使用copy模块
>>> #检验是否相等 M==L ;L is M
>>> # == 是比较变量所指向的对象值是否相等 is是比较变量是否指向同一个对象。
>>> #但是值得注意的是:小的整数和字符串被缓存并复用
>>>
>>> #查看对象复用的次数
>>> import sys
>>> sys.getrefcount(1)
1285
Python入门之(数字,set,动态类型)
最新推荐文章于 2024-01-28 16:34:13 发布