Menu Close

Python 输入、输出和导入

本教程重点介绍Python 中执行 I/O 任务两个内置函数 print() 和 input()  。此外,还介绍如何导入模块并在程序中使用它们。

Python 提供了许多内置函数,我们可以在 Python 编程中随时使用这些函数。

input() 和 print() 等一些函数分别广泛用于标准输入和输出操作。让我们先看看输出部分。

1. Python 输出内置函数print()

print() 函数将数据输出到标准输出设备(屏幕),也可以将数据输出到文件中,输出到文件中将在后面讨论。

例1.1 下面给出了print()使用示例:

print('This sentence is output to the screen')

输出

This sentence is output to the screen

例1.2 另外一个例子

a = 5
print('The value of a is', a)

输出

The value of a is 5

在第二个 print() 语句中,我们可以注意到在字符串和变量 a 的值之间添加了空格。这是默认设置,但我们可以更改它。

print() 函数的实际语法是:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

这里,objects 是要打印的值。

sep 分隔符用于值之间。它默认为空格字符。

打印完所有值后,将打印 end。它默认为一个新行。

file=是在什么地方打印出对象的值,其默认值为 sys.stdout(屏幕)输出。

例1.3 print()函数的输出

print(1, 2, 3, 4)
print(1, 2, 3, 4, sep='*')
print(1, 2, 3, 4, sep='#', end='&')

 

输出格式

如果使程序更有吸引力,可以采用格式化输出,可以通过使用 str.format() 方法来完成。此方法对任何字符串对象都是适应的。

例1.4 print()格式化输出

x = 5; y = 10
print('The value of x is {} and y is {}'.format(x,y))

在这里,花括号 {} 用作占位符。我们可以使用数字(元组索引)指定它们的打印顺序。

例1.5 print()格式化输出

print('I love {0} and {1}'.format('bread','butter'))
print('I love {1} and {0}'.format('bread','butter'))

结果:

I love bread and butter
I love butter and bread

我们甚至可以使用关键字参数来格式化字符串

例1.6 关键字参数格式化字符串

print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name = 'John'))

结果

Hello John, Goodmorning

也可以像 C 编程语言中使用的 sprintf() 函数格式化字符串,使用 % 运算符来完成此操作。

例1.7 使用 % 运算符

x = 12.3456789
print('The value of x is %3.2f' %x)
The value of x is 12.35
x = 12.3456789
print('The value of x is %3.4f' %x)

结果:

The value of x is 12.3457

2.Python 输入

到目前为止,我们的程序是静态的。没有人机对话,变量的值已经被在程序中赋值。

但大多数程序中,我们需要从用户那里获取数据输入。在 Python 中,我们用 input() 函数来实现这一点。

input() 的语法是:

input([prompt])

其中prompt 是我们希望在屏幕上显示的字符串,它是可选的。

例2.1 用户输入一个数并打印出来

num = input('Enter a number: ')
print(num)

在这里,我们可以看到输入的值 15 是一个字符串,而不是一个数字。要将其转换为数字,我们可以使用 int() 或 float() 函数。

例2.2 转换字符串

num = input('Enter a number: ')
print(num)
print(int(num))
print(float(num))
Enter a number: 15
15
15
15.0

例2.3 使用eval()表达式

可以使用 eval() 函数执行相同的操作。但 eval 更进一步,可以计算表达式。

print(eval('2+3'))

结果:

5

但是:

print(int('2+3'))

出现错误:

3. Python 导入

当我们的程序变得越来越大的时候,我们不得不将其分解为不同的模块。

模块是包含 Python 定义和语句的文件。 Python 模块有一个文件名并以扩展名 .py 结尾。

模块内的定义可以导入到另一个模块或 Python 中的交互式解释器。我们使用 import 关键字来做到这一点。

例如,我们可以通过键入以下行来导入math模块:

import math

例3.1 导入数学函数

import math
print(math.pi)

结果

3.141592653589793

现在数学模块中的所有定义都在我们的范围内可用。

我们也可以使用 from 关键字只导入一些特定的属性和函数。例如:

例3.2使用 from 关键字导入一些特定的属性和函数

from math import pi
print(pi)

结果:

3.141592653589793

在导入模块时,Python 会查看 sys.path 中定义的几个位置,它是目录位置的列表。

import sys
print(sys.path)

结果

['C:\\0python\\venv', 'C:\\0python', 'C:\\Users\\steve\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip', 'C:\\Users\\steve\\AppData\\Local\\Programs\\Python\\Python310\\DLLs', 'C:\\Users\\steve\\AppData\\Local\\Programs\\Python\\Python310\\lib', 'C:\\Users\\steve\\AppData\\Local\\Programs\\Python\\Python310', 'C:\\0python\\venv', 'C:\\0python\\venv\\lib\\site-packages']

wer

除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!

Posted in Python教程

Related Posts