• 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)

    运行效果如图

  • 相关阅读:
    二叉排序树(B-Tree)-c实现
    队列(Queue)-c实现
    栈(stack)--c实现(使用双链表)
    链表(list)--c实现
    c 和 指针读书笔记(1)
    c traps and pitfalls reading notes(2)
    js 控制
    正则表达式
    Android 笔记
    Android 布局方式学习
  • 原文地址:https://www.cnblogs.com/zzliu/p/10156658.html
Copyright © 2020-2023  润新知