• 【python】入门学习(一)


    主要记录一下与C语言不同的地方和特别需要注意的地方:

    // 整除

    ** 乘方

    整数没有长度限制,浮点数有长度限制

    复数: 

    >>> 1j*1j
    (-1+0j)

    导入模块:

    import

    import math     #导入math中所有函数 使用时要用 math.sqrt()的形式

    from math import *  #导入math中的所有函数, 可直接使用sqrt()

    from math import sqrt, tan  #只导入sqrt和tan两个函数 推荐使用

    >>> import math
    >>> math.sqrt(5)
    2.23606797749979
    >>> math.sqrt(2)*math.tan(22)
    0.012518132023611912
    >>> from math import *
    >>> log(25+5)
    3.4011973816621555
    >>> sqrt(4)*sqrt(100)
    20.0
    >>> from math import sqrt, tan
    >>> tan(20)*sqrt(4)
    4.474321888449484

    字符串: ‘ ’, “ ”, “““ ”””

    len(): 求字符串长度 返回的是整形 不像C有个‘’ 返回的是字符串本身的长度

    +:字符串拼接

    *:多次拼接

    >>> len("""No No No""")
    8
    >>> "No"*10
    'NoNoNoNoNoNoNoNoNoNo'
    >>> "No"+' Yeah!'
    'No Yeah!'
    >>> 

    帮助:

    dir():括号中是导入模块后的模块名,列出模块的所有函数

    dir(__builtins__):查看Python内置函数清单

    help():括号中是函数名,查看函数的文档字符串

    print():打印函数文档字符串

    如:print(math.tanh.__doc__)

          print(bin.__doc__)

    类型转换:

    float(): 把整数和字符串转换为浮点数

    str(): 把整数和浮点数转换为字符串

    int():把浮点数和字符串转换为整数 舍弃小数部分  字符串必须长得像整数 “123.5”是不可以

    round(): 浮点数转整数 四舍六入五成双 不支持字符串

    >>> str(3.1415)
    '3.1415'
    >>> float('3')
    3.0
    >>> int("123")
    123
    >>> int("123.5")
    Traceback (most recent call last):
      File "<pyshell#30>", line 1, in <module>
        int("123.5")
    ValueError: invalid literal for int() with base 10: '123.5'
    >>> int(123.5)
    123
    >>> round("123"ArithmeticError)
    SyntaxError: invalid syntax
    >>> 

    多重赋值:

    >>> x,y,z=1,"r",1.414
    >>> x
    1
    >>> y
    'r'
    >>> z
    1.414

    交换变量的值

    >>> y,x=x,y
    >>> x
    'r'
    >>> y
    1
  • 相关阅读:
    正则表达式
    浅谈xss攻击
    四舍五入[银行家算法]
    POJ-2442-Sequence(二叉堆)
    Spring MVC 启动报错
    WebMagic 抓取图片并保存至本地
    spring 定时任务
    jquery validate 自定义校验方法
    位图
    二叉树(线索化)
  • 原文地址:https://www.cnblogs.com/dplearning/p/3949412.html
Copyright © 2020-2023  润新知