zoukankan      html  css  js  c++  java
  • 10-1 Python 学习笔记

    1. 项目

    在文本编辑器中新建一个文件,写几句话来总结一下你至此学到的 Python 知识,其中每一行都以“In Python you can”打头。

    将这个文件命名为learning_python.txt,并将其存储到为完成本章练习而编写的程序所在的目录中。

    编写一个程序,它读取这个文件,并将你所写的内容打印三次:

    第一次打印时读取整个文件;

    第二次打印时遍历文件对象;

    第三次打印时将各行存储在一个列表中,再在 with 代码块外打印它们。

     

    2. 代码

    创建一个learning_Python.txt的文件,存储内容:

     编写learning_python.py代码

    filename = 'learning_python.txt'
    print("************************1st*************************")
    with open(filename) as file_object:
    
        contents = file_object.read()
        print(contents.rstrip())
    
    print("************************2nd*************************")
    with open(filename) as file_object:
        for line in file_object:
            print(line.rstrip())
    
    print("************************3rd*************************")
    with open(filename) as file_object:
        lines = file_object.readlines()
    
    for line in lines:
        print(line.rstrip())
    

      

    3. 执行结果

    D:python编程:从入门到实践venvScriptspython.exe "D:/python编程:从入门到实践/第10章 文件和异常/learning_python.py"
    ************************1st*************************
    In Python you can learn string
    In Python you can learn tuple
    In Python you can learn sequence
    In Python you can learn dictionary
    ************************2nd*************************
    In Python you can learn string
    In Python you can learn tuple
    In Python you can learn sequence
    In Python you can learn dictionary
    ************************3rd*************************
    In Python you can learn string
    In Python you can learn tuple
    In Python you can learn sequence
    In Python you can learn dictionary
    
    Process finished with exit code 0
    

      

  • 相关阅读:
    python中F/f表达式优于format()表达式
    java8新特性-foreach&lambda
    Java实现多线程的四种方式
    Java中的字符串常量池,栈和堆的概念
    java对象只有值传递,为什么?
    面向对象和面向过程程序设计理解及区别
    String对象为什么不可变
    mybatis_plus插件——生成器
    基于grpc的流式方式实现双向通讯(python)
    Django使用DataTables插件总结
  • 原文地址:https://www.cnblogs.com/kevin-hou1991/p/14968435.html
Copyright © 2011-2022 走看看