python入门(一)

目录

一、变量

(1)变量不能单独使用,必须与值绑定

(2)type(变量名) -- 可查看变量的类型。 

(3)dir()函数--查看某个模块下的所有方法

         (4)help()函数,查看具体用法

二、整数与浮点数

(1)整数类型的小数部分向下取整

(2)处理浮点数计算时的小bug

(3)科学计数法

(4)math库


一、变量

(1)变量不能单独使用,必须与值绑定

(2)type(变量名) -- 可查看变量的类型。 

用六个内置的类型举例。

a = 1
b = 1.0
str = "xiaow"
lst = ["xiaow"]
tup = ("xiaow",)
dic = {"name":"xiaow"}

# 输出类型
print(type(a))
print(type(b))
print(type(str))
print(type(lst))
print(type(tup))
print(type(dic))

输出为:

 (3)dir()函数--查看某个模块下的所有方法

print(dir(int))

显示结果为:['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

(4)help()函数,查看具体用法

help(int.to_bytes)

显示为:

Help on method_descriptor:

to_bytes(self, /, length, byteorder, *, signed=False)
    Return an array of bytes representing an integer.
    
    length
      Length of bytes object to use.  An OverflowError is raised if the
      integer is not representable with the given number of bytes.
    byteorder
      The byte order used to represent the integer.  If byteorder is 'big',
      the most significant byte is at the beginning of the byte array.  If
      byteorder is 'little', the most significant byte is at the end of the
      byte array.  To request the native byte order of the host system, use
      `sys.byteorder' as the byte order value.
    signed
      Determines whether two's complement is used to represent the integer.
      If signed is False and a negative integer is given, an OverflowError
      is raised.

help()函数可与dir()函数结合使用。

先dir获得所有方法名,再help获得具体用法。

注意:只需要方法名,不需要()

help(id)             √
help(id())         ×

(5)多个变量交换值

a = 1
b = 2
c = 3
a,b,c = b, c ,a
print(a,b,c)

显示:

2 3 1

相比于c++很方便,不需要中间值。

二、整数与浮点数

(1)整数类型的小数部分向下取整

print(int(3.6))

可以见到,整数类型的小数进位不是按照四舍五入来计算 。

(2)处理浮点数计算时的小bug

计算机在计算时是将十进制转化为了二进制,有些数在对应时出现了问题。所以会出现一些bug,比如:

print(0.1+0.2)

解决方法:round( ) 一个能够四舍五入的方法。

tem = round(0.1+0.2,1)
print(tem)

 

round(a,b)  b参数代表保留几位小数。 

(3)科学计数法

tem = 3.14e8
print(tem)

e后面跟数字几,就是10的几次幂 

(4)math库

import math
print(dir(math))

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

math库的导入及方法。

小技巧

print(2 * 3)
print(2 ** 3)

显示:

6
8

说明一个*是乘法

两个*是做幂指数运算

没有三个*的用法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值