[关闭]
@yanglt7 2018-11-14T03:21:09.000000Z 字数 1312 阅读 671

Python01_输入和输出

Python


(1)运行 python:命令提示符-输入 python。

  1. C:\Users\Hope92>python
  2. Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>>

(2)在交互式环境的提示符>>>下,直接输入代码,按回车,就可以立刻得到代码执行结果。

  1. >>> 1+1
  2. 2
  3. >>>

(3)输出字符串,输入 print(),用单引号或双引号把字符串括起来,但是单引号和双引号不能混用。

  1. >>> print('Hello,world')
  2. Hello,world
  3. >>>

(4)退出 python:输入 exit()。

  1. >>> exit()
  2. C:\Users\Hope92>

(5)保存python文件只能以 .py 为后缀,文件名只能是英文字母、数字和下划线组合。绝对不能用 Word 和记事本保存。

(6)在命令提示符输入 py 文件所在目录即可运行程序。

(7)用文本编辑器如 Sublime 时,print 前面不能有空格。

(8)print函数可接受多个字符串,遇到逗号会输出一个空格。

  1. >>> print('The quick brown fox','jumps over','the lazy dog')
  2. The quick brown fox jumps over the lazy dog
  3. >>>

输出类型总结:

(1)print('字符串')

  1. >>> print('word')
  2. word
  3. >>>

(2)print('字符串1','字符串2','字符串n')

  1. >>> print('word1','word2','word3')
  2. word1 word2 word3
  3. >>>

(3)打印整数,print(300),或计算结果 print(100+200)

  1. >>> print(300)
  2. 300
  3. >>> print(100+200)
  4. 300
  5. >>>

(4)打印字符串和结果

  1. >>> print('100 + 200 =',100+200)
  2. 100 + 200 = 300
  3. >>>

输入:

(1)输入name = input() - 回车 - Tom ,查看变量,输入 name

  1. >>> name = input()
  2. Tom
  3. >>> name
  4. 'Tom'
  5. >>>

这里,name是一个变量

(2)打印变量内容,print(name)

  1. >>> name = input()
  2. Tom
  3. >>> print(name)
  4. Tom
  5. >>>

(3)新建 .py 文件 name=input() print('hello,',name)  在命令行运行程序

  1. C:\Users\Hope105>F:/Personal/ylt/python/input.py
  2. Tom
  3. hello, Tom
  4. name=input('please enter your name') print('hello,',name)  在命令行运行程序
  5. C:\Users\Hope105>F:/Personal/ylt/python/input.py
  6. please enter your nameTom
  7. hello, Tom
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注