• Python学习笔记(04)


    第六章 输入与输出

    1.标准输出函数print()

    (1)格式:print(*objects,sep=’’,end=’\n’,file=sys.stdout)

    (2)参数:

    <1> *objects:标识一次输出多个对象,输出多个对象需要使用逗号作为间隔符

    <2> sep:间隔符,默认为一个空格

    <3> end:用什么来结尾,默认\n,可替换

    <4> file:需要写入的文件名

    (3)例:

    1. print(1,2,3,4,5)
    2. #print(1 2 3 4 5)报错
    3. print(1,2,3,4,5,sep='')
    4. print('hello world')
    5. print('www.baidu.com')
    6. print('www.','baidu.','com',sep='')
    7. for i in range(100):
    8. print(i,end=' ')
    9. if(i%10==0): # 逢10换行
    10. print()
    11. print()
    12. str='hello'
    13. print(str*2)
    14. print(2*str) #*表示复制几份
    15. print(str+'world') # + 表示连接
    16. # 在d:新建test文件
    17. fp=open(r'd:\test.txt','w') #r 去掉转义字符的功能
    18. # 打开文件,写入
    19. print('我爱你',file=fp)
    20. fp.close() # 关闭文件

    (4)格式化输出

    <1> 无符号八进制%o,无符号十六进制%x,无符号%u,整数%d

    1. num1 = 10
    2. num2 = 20
    3. print('八进制输出:0o%o,0o%o'%(num1,num2))
    4. print('十六进制输出:0x%X,0x%X'%(num1,num2))
    5. print('%u'%(-10))
    6. print('%d'%(-10))
    7. # python3中 %U 等同于 %d

     <2> %f:显示小数后6位,第7位四舍五入

    print('%f'%12.21224242)

    <3> %e:科学计数法表示方式

    print('%e'%123.1234567890)
    

    <4> %g:在保留6位有效数字的前提下,使用小数方式,否则使用科学计数法表示

    <5> %s:字符串

    print('%s'%'china')

    (5)带有控制功能的格式化输出

    <1> %md:表示实处m位宽度的十进制整数,m为最小宽度(默认右对齐),若小于m列这左起补空格,大于m列原样输出

    1. a=1234
    2. b=678
    3. print('%5d'%a)
    4. print('%2d'%b)
    5. print('%05d'%a) #左起补空格改为左起补0
    6. print('%-5d%d'%(a,b)) # - 默认右对齐改为默认左对齐
    7. print('%-05d%d'%(a,b)) #无效

    <2> %m.nf:表示输出float型数据,m为总宽度(包含小数位),n为小数位,小于n表示小数位不够右起补0,大于n位则在n+1位进行四舍五入,m若省略则值同n。

    1. num=1234.12345
    2. print('%10.4f'%num)
    3. print('%9.2f'%num)
    4. print('%.2f'%num) # 常用四舍五入显示算法,只做显示,原值不变
    5. str='hello'
    6. print('%10.3s'%str) # 字符串截取2位,右对齐补空格

    2.输入

    (1)格式:变量名=input('提示文字’)

    (2)返回值:字符串

    (3)PS:input()函数返回值一律为字符串类型,可以使用int()和float()函数进行强制类型转换

    3.图形化程序设计

    (1)Turtle是Python内嵌的绘制 线、圆以及其他形状(包括)文本的 图形模块。

     

    (2)程序实例:

    <1> 例1:画出一个三角形

    1. import turtle as t
    2. t.pensize(10)
    3. t.color('red')
    4. t.right(60)
    5. t.forward(100)
    6. t.right(120)
    7. t.forward(100)
    8. t.right(120)
    9. t.forward(100)
    10. t.done()

    <2> 画出一个五角星

    1. import turtle as t
    2. t.pensize(5) # 笔的粗度
    3. t.color('red')
    4. t.right(72)
    5. t.forward(100)
    6. t.right(144)
    7. t.forward(100)
    8. t.right(144)
    9. t.forward(100)
    10. t.right(144)
    11. t.forward(100)
    12. t.right(144)
    13. t.forward(100)
    14. t.right(144)
    15. t.done()

    <3> 绘制奥运五环

    1. import turtle as t
    2. t.pensize(6)
    3. t.color('blue')
    4. t.penup()
    5. t.goto(-110, -25)
    6. t.pendown()
    7. t.circle(45)
    8. t.penup()
    9. t.color('black')
    10. t.goto(0, -25)
    11. t.pendown()
    12. t.circle(45)
    13. t.penup()
    14. t.color('red')
    15. t.goto(110, -25)
    16. t.pendown()
    17. t.circle(45)
    18. t.penup()
    19. t.color('yellow')
    20. t.goto(-55, -75)
    21. t.pendown()
    22. t.circle(45)
    23. t.penup()
    24. t.color('green')
    25. t.goto(55, -75)
    26. t.pendown()
    27. t.circle(45)
    28. t.done()

  • 相关阅读:
    笔记(三)传统图机器学习的特征工程-节点
    卷积网络识别好莱坞明星
    pandas基础-pandas之Series+ 读取外部数据+dataframe+dataframe的索引
    【NoSQL数据库】Redis简介
    【概率论与数理统计(研究生课程)】知识点总结8(假设检验)
    java阻塞队列
    9、定义错误页
    数据脱敏的功能与技术原理【详解】
    文本识别论文CRNN
    安装搭建PHP域名授权验证系统、盗版可以追踪双重鉴权、在线加密功能二次开发部署
  • 原文地址:https://blog.csdn.net/weixin_62443409/article/details/127649651