参考链接: Python输入,输出和导入

In this tutorial we will cover the basics of Python Input/Output and also how to import a module in python program. Previously we learned about Python Data types. You can learn that from Python Data types.

在本教程中,我们将介绍Python输入/输出的基础知识,以及如何在python程序中导入模块。 先前我们了解了Python数据类型。 您可以从Python数据类型中学到这一点。

Python输入输出 (Python Input Output)

I/O means Input and Output. We will learn about basic functions of python input and output.

I / O表示输入和输出。 我们将学习python输入和输出的基本功能。

将输出打印到屏幕 (Printing Output to the Screen)

We are already familiar with the print() function. It is used to print something to the screen.

我们已经熟悉print()函数。 它用于将某些内容打印到屏幕上。

print("Print something to the screen")

Moreover, we can pass two or more different strings in the print function separated by either commas (,) or plus (+) signs. Like this;

此外,我们可以在打印函数中传递两个或多个不同的字符串,并用逗号(,)或加号(+)分隔。 像这样;

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");

接受用户输入 (Taking User Input)

Sometimes our program may need to read keyboard inputs given by user. For that reason we will use 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).

有时我们的程序可能需要读取用户提供的键盘输入。 因此,我们将使用input()函数。 input()函数从标准输入读取一行。 或者换句话说,输入功能从键盘输入中读取,直到给出换行符为止(即,按下Enter键)。

#takes input from keyboard and stores in a string

str = input("Enter your input: ");

#then we can print the string

print(str)

If you run this program you will see a prompt like this below picture, waiting for you to give input. It will take as many characters as you type until Enter is pressed.

如果您运行此程序,则会在下图看到如下提示,等待您进行输入。 直到按Enter为止,它将占用您输入的尽可能多的字符。

Python I / O –文件操作 (Python I/O – File Operations)

Sometimes we need to read and write from files. For this reason there are some functions available.

有时我们需要从文件读取和写入。 因此,有些功能可用。

档案开启 (File Opening)

For opening a file we use open() functions. This is a built-in function in Python.

为了打开文件,我们使用open()函数。 这是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

You can also specify the mode or purpose for opening that 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

文件关闭 (File Closing)

When we are done with a file, we need to close it. For that purpose, we will use close() function.

处理完文件后,需要关闭它。 为此,我们将使用close()函数。

f = open("input_file.txt")    # open "input_file.txt" file

#do file operation.

f.close()

从文件读取 (Reading From a file)

For reading a file, there are several functions. In this below program, we will explore those functions.

为了读取文件,有几个功能。 在下面的程序中,我们将探索这些功能。

You can read a certain number of byte from the file with 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() 您可以使用read()函数从文件中read()一定数量的字节。  You can read file line by line with 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() 您可以使用readline()函数逐行读取文件。  You can also read all the lines at once and store the lines in a list of strings.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() 您还可以一次读取所有行,并将这些行存储在字符串列表中。

写入文件 (Writing to a file)

For writing something into a file is pretty simple and similar to file read. We will use write() function.

将某些内容写入文件非常简单,类似于读取文件。 我们将使用write()函数。

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

#write something in the file.

f.write("this is my first line\n")

f.write("this is my second line\n")

f.close()

Python导入 (Python Import)

Whenever we want to use a package or module in our Python program, firstly we need to make it accessible. For that reason we need to import that package or module in our program.

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

Suppose, we have a number. we want to print it’s square root. So if we write this below program, it should work fine.

假设我们有一个数字。 我们要打印它的平方根。 因此,如果我们在下面的程序中编写此程序,则应该可以正常工作。

#get a variable having value 16

number=16

#square root this number.

number=sqrt(number)

print(number)

But if we run this program, it will show errors like this-

但是,如果我们运行此程序,它将显示如下错误:

This is because, 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 –

这是因为sqrt()函数位于模块名称“ math”下。 因此,如果要使用此功能,则需要通过导入“数学”模块使此模块可访问。 因此正确的代码将如下所示–

#first import math module

import math

#get a variable having value 16

number=16

#square root this number.

number=math.sqrt(number)

print(number)

So, that’s it for Python Input Output and Python Import. For more information about these topics you can check the official Python documentation from these below links. Python Documentation I/O Python Import

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

翻译自: https://www.journaldev.com/14056/python-input-output-python-import

[转载] Python输入,输出,Python导入相关推荐

  1. python输入语句-Python中的模块导入和读取键盘输入的方法

    导入模块 import 语句 想使用Python源文件,只需在另一个源文件里执行import语句,语法如下: ? 1 import module1[, module2[,... moduleN] 当解 ...

  2. python输入名字-Python基础篇--输入与输出

    在任何语言中,输入和输出都是代码最基础的开始, so,先来聊一聊输入和输出 输出 在python中,我们一般用print() 输出,在括号里输入你想输出的信息,用引号包裹起来(单双三都可以),例如我们 ...

  3. python输入语句-python输入,python基本输入输出语句

    input函数,运行之后,就需要用键盘输入: a=input() print(a) 运行之后,用键盘输入内容,按下回车键,就可以打印输入的内容. 但是,如果运行之后有所提示,会不会好一点呢? prin ...

  4. python输入语句-python输入语句

    广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. python条件语句目录:1. 分支语句(if...else...)2. ...

  5. python输入,Python中的基本输入和输出

    从<安装Python和写出第一个Python程序>开始,我们就在屏幕上面开始输出了"Hello World",这个就使用到了print()函数向屏幕输出一些字符,这就是 ...

  6. python文件输出-python 文件的输入输出

    一:文件的输入: 在前面python基础命令中,我们已经介绍过输入,在此举几个例子解释一下两者的区别 raw_input()函数从标准输入读取一个行,并返回一个字符串(去掉结尾的换行符): input ...

  7. python文件输出-python将控制台输出保存至文件的方法

    很多时候在Linux系统下运行python程序时,控制台会输出一些有用的信息.为了方便保存这些信息,有时需要对这些信息进行保存.这里介绍几种将控制台输出保存到文件中的方式: 1 重定向标准输出流 重定 ...

  8. python文件输出-Python 文件和输入输出小结

    1.打开和关闭文件(open(),file(),close()) 有两种内建函数可以获取文件对象:open和file.他们的用法完全一样.下面只以open()为例子讲解.获取一个文件对象(打开文件)的 ...

  9. python输入公式,Python公式

    例子:1+2〈三个部分.两个常量,一个运算符,分别把他们分成三个量,不如:1=a,+=b,2=c〉 a=input('第一个数') b=input('第二个运算符') c=input('第三个数')一 ...

  10. python文件输出-python文件流

    打开文件 文件的基本方法 迭代文件内容 打开文件 打开文件,可以使用自动导入的模块io中的函数open.函数open将文件名作为唯一必不可少的参数,并返回一个文件对象.如果只指定一个文件名,则获得一个 ...

最新文章

  1. PYTHON学习笔记-DAY-16
  2. 一起学nRF51xx 22 -  实现一个具体SVC调用功能的demo
  3. Oracle 数据表的管理
  4. dede 验证码不显示 vdimgck.php,Dede后台验证码不显示解决方法详解(dedecms 5.7)
  5. 单边指数信号的特点_测试技术课后题答案1信号描述
  6. context-param和init-param的区别
  7. 前端学习(1126):递归求数学题
  8. 【机器学习】Bagging和Boosting的区别(面试准备)
  9. 清空临时表oracle,【Oracle相关】Oracle中如何清空临时表空间
  10. cocos2d-x CCArray用法 遍历和删除元素
  11. Unity 通过代码修改材质球属性
  12. 屏蔽CSDN广告插件 - Chrome
  13. iOS内测平台fir.im,发布内测版本
  14. 3D艺术家推荐——4款最佳3D建模软件
  15. 虚幻4地形怎么增加层_虚幻周报20200602 | 我等的东西还没来……
  16. 服务器老被攻击,该如何解决?
  17. 你在加密市场能走多远 取决于你的思维认知
  18. Java23种设计模式 适配器模式【Adapter Pattern】
  19. 双向长短期记忆网络模型_基于深度双向长短期记忆网络的空气质量预测方法与流程...
  20. c语言程序设计猜拳小游戏答辩,C语言课程设计猜拳游戏.doc

热门文章

  1. 网页中引用两个css冲突怎么办
  2. LeetCode 热题 HOT 100 完整题解笔记知识点分类 C++代码实现
  3. 【HDOJ6992】Lawn of the Dead(线段树×, 模拟大法好√)
  4. NYOJ458 - 小光棍数
  5. 河北520分理科计算机专业,河北最幸运考生,520分“捡漏”考上中国人民公安大学,网友:铁饭碗...
  6. 用yacc编写的算术运算计算器_10天学会四则运算小计算器设计之第5天
  7. Python入门--with语句
  8. C++11常见新特性
  9. 试用 P、V操作描述下列理发师和顾客之间的同步问题
  10. 如何使用Burp Suite代理