Python 入门简明教程
1. 1. 与 Python 的第一次亲密接触-写给初学者 2. 2. Python 语法关键字 3. 3. Python 的注释和语法 4. 4. Python 基本数据类型 5. 5. Python 输入输出 6. 6. Python 运算符 7. 7. Python 变量 8. 8. Python 命名空间和变量生存范围 9. 9. Python 逻辑判断 10. 10. Python For 循环语法 11. 11. Python While 循环语法 12. 12. Python 循环语法中 break 与 continue的使用 13. 14. Python 三目运算符 14. 15. Python 循环语法中 pass 语句的使用 15. 16. Python 循环语法示例 16. 17. Python 函数 17. 18. Python main 函数 18. 19. Python print 函数 19. 20. Python print format 输出格式化 20. 21. Python print 输出到文件 21. 22. Python 输入 22. 23. Python import 导入模块 23. 25. Python 中递归的使用 24. 26. Python 匿名函数 25. 27. Python lambda 26. 28. Python PIP 包管理机制 27. 29. Python 命令行参数 28. 30. Python Numbers 包 29. 31. Python 随机数 30. 32. Python String 转 int 31. 34. Python 自定义异常 Exception 32. 35. Python 继承 33. 36. Python 父类 34. 38. Python 操作符重载 35. 39. Python __str__ and __repr__

5. Python 输入输出

在本教程中,我们将介绍Python输入/输出的基础知识,以及如何在Python程序中导入模块。前面我们学习了Python数据类型。你可以从中学到Python数据类型.

Python输入输出

I/O是指输入和输出。我们将学习python输入和输出的基本函数。

打印输出到屏幕

我们已经熟悉print() function. It is used to print something to the screen.


print("Print something to the screen")

此外,我们可以在print函数中传递两个或多个不同的字符串,这些字符串由逗号(,)或加号(+)分隔。这样地;


print("This is first String " + "Followed by this string");
#Or can be written like this also
print("This is another String " , "Followed by another string");

接受用户输入

有时我们的程序可能需要读取用户提供的键盘输入。因此,我们将使用input() function. The input() function reads one line from standard input. Or in other words input function reads from keyboard input until a line feed is given ( i.e. Pressed Enter).


#takes input from keyboard and stores in a string
str = input("Enter your input: ");

#then we can print the string
print(str)

如果你运行这个程序,你会看到一个像下图这样的提示,等待你输入。在按Enter键之前,它将使用与您键入的相同数量的字符。

Python I/O和#8211;文件操作

有时我们需要读写文件。因此,有一些功能可用。

文件打开

打开我们使用的文件打开()功能。这是Python中的一个内置函数。


f = open("input_file.txt")    # open "input_file.txt" file in current directory

f = open("C:/Python34/README.txt")  # specifying full path of a file

也可以指定打开该文件的模式或用途。


f = open("input_file.txt","r")    # open "input_file.txt" file to read purpose

f = open("input_file","w")  # open "input_file.txt" file to write purpose

f = open("input_file","a")  # open "input_file.txt" file to append purpose

文件关闭

当我们完成了一个文件,我们需要关闭它。为此,我们将使用关闭()功能。


f = open("input_file.txt")    # open "input_file.txt" file
#do file operation.
f.close()

从文件读取

对于读取文件,有几个函数。在下面的程序中,我们将探讨这些函数。

  1. 您可以使用read() function.
    
    f = open("input.txt")    # open "input.txt" file
    
    str=f.read(10) #read first 10 bytes from the file.
    print(str)     #print first 10 bytes from the file.
    
    f.close()
    
  2. 你可以用逐行读取文件readline() function.

    
    f = open("input.txt")    # open "input.txt" file
    
    str=f.readline() #read first line from the file.
    print(str)     #print the first line.
    
    str=f.readline() #read second line from the file.
    print(str)     #print the second line.
    
    f.close()
    
  3. 您还可以一次读取所有行并将这些行存储在字符串列表中。

    
    f = open("input.txt")    # open "input.txt" file
    
    str=f.readlines() #read all the lines from the file at once. and store as a list of string
    print(str)     #print list of all the lines.
    
    f.close()
    

写入文件

因为在文件中写入内容非常简单,与文件读取类似。我们将使用write() function.


f = open("output.txt","w")    # open "output.txt" file

#write something in the file.
f.write("this is my first line
")
f.write("this is my second line
")

f.close()

Python导入

每当我们想在Python程序中使用一个包或模块时,首先我们需要使其可访问。因此,我们需要在程序中导入该包或模块。

我们有一个号码。我们要打印它的平方根。所以如果我们在下面写这个程序,它应该可以正常工作。


#get a variable having value 16
number=16

#square root this number.
number=sqrt(number)

print(number)

但是如果我们运行这个程序,它会显示这样的错误-

这是因为,sqrt() function is under module name “math”. So if we want to use this function, we need to make this module accessible by importing the “math” module. So the correct code will be like this –


#first import math module
import math
#get a variable having value 16
number=16

#square root this number.
number=math.sqrt(number)

print(number)

因此,这就是Python输入输出和Python导入。有关这些主题的更多信息,您可以从下面的链接中查看正式的Python文档。 Python文档I/O Python导入