zoukankan      html  css  js  c++  java
  • python中 的意义及用法

    的意义

     表示将光标的位置回退到本行的开头位置

     表示将光标的位置回退一位

     在python里print会默认进行换行,可以通过修改参数让其不换行

    (1) python2中可以在print语句的末尾加上逗号,代码如下:

    print "hello",
    print "world"

    执行结果

    hello world
    
    请按任意键继续. . .

    (2)在python3里print是一个独立函数,可以通过修改它的默认值来让其不换行

    def print(self, *args, sep=' ', end='
    ', file=None): # known special case of print
        """
        print(value, ..., sep=' ', end='
    ', file=sys.stdout, flush=False)
        
        Prints the values to a stream, or to sys.stdout by default.
        Optional keyword arguments:
        file:  a file-like object (stream); defaults to the current sys.stdout.
        sep:   string inserted between values, default a space.
        end:   string appended after the last value, default a newline.
        flush: whether to forcibly flush the stream.
        """

    将end参数改为其他的字符可以让print不换行,来看代码

    print("Dream", "it", "possible", sep="-",end="/")
    print("Big big world")

    运行结果如下:

    Dream-it-possible/Big big world
    
    Process finished with exit code 0

    的应用

    利用 可以实现很多有趣的小功能

    在命令行实现倒计时功能

    1 # 显示倒计时
    2 import time
    3 for i in range(10):
    4     print("
    离程序退出还剩%s秒" % (9-i), end="")
    5     time.sleep(1)

    运行结果如图

    命令行实现转圈功能

    1 import time
    2 lst = ["\", "|", "/", "———"]
    3 for i in range(20):
    4     j = i % 4
    5     print("
    " + lst[j], end="")
    6     time.sleep(0.2)

    实现进度条功能

    1 # 进度条功能
    2 import time
    3 
    4 for i in range(10):
    5     print("
    " + ""*i, sep="", end="")
    6     time.sleep(0.2)
    7 print("
    下载完成")

     运行效果如下

    实现删除效果功能

    1 import time
    2 s = "枝上柳绵吹又少,天涯何处无芳草"
    3 l = len(s)
    4 for i in range(l):
    5     print("
    " + s[:l-1-i] + "|", end="")
    6     time.sleep(0.15)

    运行效果如图

  • 相关阅读:
    记第一场省选
    POJ 2083 Fractal 分形
    CodeForces 605A Sorting Railway Cars 思维
    FZU 1896 神奇的魔法数 dp
    FZU 1893 内存管理 模拟
    FZU 1894 志愿者选拔 单调队列
    FZU 1920 Left Mouse Button 简单搜索
    FZU 2086 餐厅点餐
    poj 2299 Ultra-QuickSort 逆序对模版题
    COMP9313 week4a MapReduce
  • 原文地址:https://www.cnblogs.com/zzliu/p/10156658.html
Copyright © 2011-2022 走看看