python基础入门----标准输出,输入和导包

原文:https://www.programiz.com/python-programming/input-output-import

Python Input, Output and Import

Table of Contents

Python provides numerous built-in functions that are readily available to us at the Python prompt.

Some of the functions like input() and print() are widely used for standard input and output operations respectively. Let us see the output section first.

输出,输入和导包

表格内容

  • python输出使用print()函数

输出格式

  • 输入
  • 导包

python提供了众多的内置函数,他们在python提示符下容易获得。

有些函数像input()和print()分别被标准输出和输入操作使用。首先让我们看看输出的部分。


 

Python Output Using print() function

We use the print() function to output data to the standard output device (screen).

We can also output data to a file, but this will be discussed later. An example use is given below.

print('This sentence is output to the screen')
# Output: This sentence is output to the screen

a = 5

print('The value of a is', a)
# Output: The value of a is 5

 

输出使用print()函数

我们使用print()函数将数据去输出到标准输出设备(屏幕)。

我们还可以输出数据到文件,但这个我们稍后讨论。下面我们给出一个例子。

In [1]: print('This sentence is output to the screen')
This sentence is output to the screen

In [2]: a = 5

In [3]: print("The value of a is",a)
The value of a is 5

In the second print() statement, we can notice that a space was added between the stringand the value of variable a.This is by default, but we can change it.

The actual syntax of the print() function is

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

Here, objects is the value(s) to be printed.

在这第二个print()状态中,我们注意到,一个空格被添加到字符串变量和值之间。这个默认的,但是我们可以改变。

print()函数的实际语法是

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

这里,对象是值并被打印。


The sep separator is used between the values. It defaults into a space character.

After all values are printed, end is printed. It defaults into a new line.

The file is the object where the values are printed and its default value is sys.stdout(screen). Here are an example to illustrate this.

print(1,2,3,4)
# Output: 1 2 3 4

print(1,2,3,4,sep='*')
# Output: 1*2*3*4

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

两个值用分割符sep分割。它默认为一个空格字符。

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

文件是值输出的对象,它的默认值是sys.stdout(屏幕)。这里有一个例子去说明它。

In [1]: print(1,2,3,4)
1 2 3 4

In [2]: print(1,2,3,4,sep="*")
1*2*3*4

In [3]: print(1,2,3,4,sep="*",end="&")
1*2*3*4&

Output formatting

Sometimes we would like to format our output to make it look attractive. This can be done by using the str.format() method. This method is visible to any string object.

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

规定输出格式

有时候,我们g规定的输出格式使他看起来更吸引人。我们可以用str.format()方法实现。

这个方法是对任何字符串对象可视的。

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

Here the curly braces {} are used as placeholders. We can specify the order in which it is printed by using numbers (tuple index).

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

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

这里的大括号{}是用来作为占位符。我们我们使用数字(元组 索引)来指定打印的顺序。

In [3]: print("I love {0} and {1}".format('bread','butter'))
I love bread and butter

In [4]: print("I love {1} and {0}".format('bread','butter'))
I love butter and bread

We can even use keyword arguments to format the string.

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

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

In [5]: print('Hello {name},{greeting}'.format(greeting = 'Goodmorning',name = 'John'))
Hello John,Goodmorning

We can even format strings like the old sprintf() style used in C programming language. We use the % operator to accomplish this.

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

我们甚至可以在C语言使用规定字符串格式像老的sprintf()样式。我们可使用%操作符去完成。

In [17]: x = 12.3456789

In [18]: print('The value of x is %3.2f' %x)
The value of x is 12.35

In [19]: print('The value of x is %3.4f' %x)
The value of x is 12.3457

Python Input

Up till now, our programs were static. The value of variables were defined or hard coded into the source code.

To allow flexibility we might want to take the input from the user. In Python, we have theinput() function to allow this. The syntax for input() is

input([prompt])

输入

直到现在,我们的程序是静态的。变量的值被定义或硬编码变成源编码。

为了遵从灵活性,我们可以从用户获取输入。在python中,我们有input()函数去实现它。对于input()的语法是

intput([prompt])

where prompt is the string we wish to display on the screen. It is optional.

>>> num = input('Enter a number: ')
Enter a number: 10
>>> num
'10'

这里的的提示是字符串,我们希望展示在屏幕上。它是可选的。

>>> num = input('Enter a number: ')
Enter a number: 10
>>> num
'10'

Here, we can see that the entered value 10 is a string, not a number. To convert this into a number we can use int() or float() functions.

>>> int('10')
10
>>> float('10')
10.0

这里,我们可以看到输入的值10是一个字符串,不是一个数字,去转换为一个字数我们可以能使用int()或flloat()函数。


This same operation can be performed using the eval() function. But it takes it further. It can evaluate even expressions, provided the input is a string

>>> int('2+3')
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2+3'
>>> eval('2+3')
5

 这相同的操作使用eval()函数去执行。但是可以更进一步。它甚至可以计算一个表达式,如果它是表达式。

>>> int('2+3')
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2+3'
>>> eval('2+3')
5

Python Import

When our program grows bigger, it is a good idea to break it into different modules.

A module is a file containing Python definitions and statements. Python modules have a filename and end with the extension .py.

Definitions inside a module can be imported to another module or the interactive interpreter in Python. We use the import keyword to do this.

For example, we can import the math module by typing in import math.

import math
print(math.pi)

导入

当我们程序变大时,把它分成不同模块是个好主意。

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

在python中可以把另一个模块导入定义的模块里或交互式解释器。我们使用import关键字去完成这些。

例如:我们可以通过import math导入math。

In [1]: import math

In [2]: print(math.pi)
3.141592653589793

Now all the definitions inside math module are available in our scope. We can also import some specific attributes and functions only, using the from keyword. For example:

>>> from math import pi
>>> pi
3.141592653589793

While importing a module, Python looks at several places defined in sys.path. It is a list of directory locations.

>>> import sys
>>> sys.path
['', 
 'C:\\Python33\\Lib\\idlelib', 
 'C:\\Windows\\system32\\python33.zip', 
 'C:\\Python33\\DLLs', 
 'C:\\Python33\\lib', 
 'C:\\Python33', 
 'C:\\Python33\\lib\\site-packages']

We can add our own location to this list as well.

现在所有定义在math模板里面的都是我们可用范围内。我们还可以导入指定的属性和函数,使用from关键字,例如:

>>> from math import pi
>>> pi
3.141592653589793

当导入模块, python可以查看定义的位置在sys.path.在本地目录是一个列表

>>> import sys
>>> sys.path
['', 
 'C:\\Python33\\Lib\\idlelib', 
 'C:\\Windows\\system32\\python33.zip', 
 'C:\\Python33\\DLLs', 
 'C:\\Python33\\lib', 
 'C:\\Python33', 
 'C:\\Python33\\lib\\site-packages']

我们可以添加自己的位置到列表中。 

>>> from math import pi
>>> pi
3.141592653589793

Check out these examples to learn more:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值