zoukankan      html  css  js  c++  java
  • Python-数据类型

    数据类型和格式化

    去掉解释即可运行

    求幂(科学计算),整数

    高精度浮点运算类型

    复数

    运算符//取整数商,/取商

    内置函数

    类型转换

    math库的使用

    天天向上

    字符串的操作

    打印周几

    内置字符串处理函数

    进制转换

    凯撒密码

    内置字符串处理方法

    format的使用

    format的格式控制

    输出类型

    文本进度条

    单行动态刷新

    带刷新的文本进度条

    # 数据类型
    
    
    '''
    # 求幂(科学计算),整数
    print(pow(2,100))
    
    print(pow(2,pow(2,15)))
    
    '''
    
    '''
    # 浮点数,python支持无限且准确的整数,若想有更高的计算,往往不用浮点型(有长度限制),而采用整形
    
    # sys.float_info详细列出了Python解释器所运行的浮点数各参数
    
    import sys
    print(sys.float_info)
    print(sys.float_info.max)
    
    '''
    
    
    '''
    
    # 高精度浮点运算类型
    
    import decimal
    a= decimal.Decimal('3.14159365324')
    b= decimal.Decimal('1.23564648545')
    decimal.getcontext().prec=20 # 自定义浮点数精度的位数
    print(a*b)
    
    
    '''
    
    
    '''
    # 复数
    
    # 可以用z.real获取实部,z.imag获取虚部
    
    a = 1.23e-4+5.67e+89j
    print(a.real)
    print(a.imag)
    
    '''
    
    
    '''
    # 运算符//取整数商,/取商
    print(10/3)
    print(10//3)
    
    # **求次幂
    print(2**3)
    
    '''
    
    
    '''
    # 内置函数
    
    # abs求复数绝对值,值为坐标轴相对于原点的长度
    
    print(abs(-3+4j))
    
    # pow 的第三个参数可选,该参数使模运算和幂运算同时进行,速度很快
    
    print(pow(3,pow(3,999),10000)) # 求3的999次幂的后四位
    
    '''
    
    
    
    '''
    # 类型转换
    
    print(int(10.99))
    
    print(complex(10.99))
    
    # print(float(10+99j)) # 会报错
    
    print(float((10+99j).imag)) #  正确
    
    '''
    
    
    # math库的使用
    # 第一种
    
    '''
    import math
    
    print(math.ceil(10.2))
    
    '''
    # 第二种
    
    '''
    import math import floor
    print(floor(10.2))
    
    
    '''
    
    '''
    # math库解析
    import math
    
    print(math.fsum([0.1,0.2,0.3])) # math.fsum([x,y,....])
    
    print(math.pow(10,1/3))
    
    
    '''
    
    
    
    
    # 天天向上
    
    '''
    import math
    dayfactor = 0.01
    dayup = math.pow((1.0 + dayfactor), 365) # 每天提高0.01
    daydown = math.pow((1.0 - dayfactor), 365) # 每天荒废0.01
    print("向上: {:.2f}, 向下: {:.2f}.".format(dayup, daydown))
    
    '''
    
    '''
    dayup, dayfactor = 1.0, 0.01
    for i in range(365):
        if i % 7 not in [6, 0]:
            dayup = dayup * (1 + dayfactor)
        else:
            dayup = dayup * (1 - dayfactor)
    print("向上5 天向下2 天的力量: {:.2f}.".format(dayup))
    
    '''
    
    
    '''
    
    # 字符串的操作
    
    # 可使用单引号,双引号,三引号,可以互相嵌套,只要不发生冲突,+用来了链接两个字符串
    # 也可运算符号使用
    print("hello"*3)
    
    
    # 打印周几
    
    weekstr = "星期一星期二星期三星期四星期五星期六星期日"
    weekid = eval(input("请输入星期数字(1-7): "))
    pos = (weekid - 1)*3
    print(weekstr[pos: pos+3])
    
    '''
    
    
    
    # 内置字符串处理函数
    
    '''
    a="sdfghjk"
    l = len(a) # 以Unicode编码计算
    print(l)
    num=45614523
    print(str(num))
    
    # Unicode编码的转换
    print(chr(10004))
    print(str(ord("&")))
    
    
    # 进制转换
    
    print(hex(255)) # 十六进制
    
    print(oct(-255)) # 八进制
    
    '''
    
    # 凯撒密码
    
    
    '''
    
    plaincode = input("请输入明文: ")
    for p in plaincode:
        if ord("a") <= ord(p) <= ord("z"):
            print(chr(ord("a") + (ord(p) - ord("a") + 3)%26), end='')
        else:
            print(p, end="")
    
    
    
    '''
    
    
    # 内置字符串处理方法
    
    '''
    
    print("python is a ok language.".split())
    
    print("python".center(40,'+'))
    
    print("123".zfill(40))
    
    print("-123".zfill(40))
    
    
    '''
    
    
    # format的使用
    
    '''
    print("{}{}{}".format("123","asd","+++"))
    
    print("result is {{ {1} {2} }}and {0}".format("000","111","222"))
    
    s="result is {{ {1} {2} }}and {0}"
    print(s.format("123",3.1415926,"...."))
    
    '''
    
    
    # format的格式控制
    
    '''
    
    s = "python"
    
    print("{0:30}".format(s))
    print("{0:>30}".format(s))
    print("{0:*^30}".format(s))
    print("{0:-^30}".format(s))
    print("{0:3}".format(s))
    
    print("{0:-^20,}".format(1234567890))
    print("{0:-^20}".format(1234567890)) # 对比输出
    print("{0:-^20,}".format(12345.67890))
    
    
    
    print("{0:.2f}".format(12345.67890))
    
    print("{0:H^20.3f}".format(12345.6789))
    
    print("{0:.4}".format("python"))
    
    
    '''
    
    # 输出类型
    
    '''
    # 下面分别为: 二进制,Unicode字符,十进制,八进制,小写十六进制,大写十六进制
    
    print("{0:b},{0:c},{0:d},{0:o},{0:x},{0:x},{0:X}".format(425))
    
    print("{0:e},{0:E},{0:f}{0:%}".format(3.14)) # e的指数形式,E的指数形式,标准浮点形式,百分数形式
    
    print("{0:.2e},{0:.2E},{0:.2f}{0:.2%}".format(3.14))
    
    '''
    
    # 文本进度条
    
    '''
    import time
    scale = 10
    print("------执行开始------")
    for i in range(scale+1):
        a, b = '**' * i,'..' * (scale - i)
        c = (i/scale)*100
        print("%{:^3.0f}[{}->{}]" .format (c, a, b))
        time.sleep(0.1)
    print("------执行结束------")
    
    '''
    
    
    
    # 单行动态刷新
    
    '''
    import time
    for i in range(101):
        print("
    {:2}%".format(i), end="")
        time.sleep(0.05)
    
    '''
    
    
    # 带刷新的文本进度条
    
    
    # 命令后执行才可看见正确的效果
    import time
    scale = 50
    print("执行开始".center(scale//2,'-'))
    t = time.clock()
    for i in range(scale+1):
        a = '*' * i
        b = '.' * (scale - i)
        c = (i/scale)*100
        t -= time.clock()
        print("
    {:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,-t),end='')
        time.sleep(0.05)
    print("
    "+"执行结束".center(scale//2,'-'))
    
    
    ♪(^∇^*)♪(^∇^*)(~ ̄▽ ̄)~有没有感觉很棒呀!!!(#^.^#),(*^▽^*)O(∩_∩)O哈哈~
  • 相关阅读:
    Mysql
    JavaScript常用事件
    css
    HTML
    判断pc还是手机打开跳转到别的网页
    queue 队列
    兼容firstChild和firstElementChild
    总结各种width,height,top,left
    原生js提取非行间样式
    ie8 不支持media
  • 原文地址:https://www.cnblogs.com/ygjzs/p/12066669.html
Copyright © 2011-2022 走看看