您的位置:首页 > 编程语言 > Python开发

廖雪峰python3.6教程笔记2-Python输入和输出

2018-03-28 11:34 676 查看

廖雪峰python3.6教程笔记2-Python输入和输出

一 python的运行方式:

1: 在Python交互式模式下,可以直接输入代码,然后执行,并立刻得到结果。
(相当于启动了Python解释器,但是等待你一行一行地输入源代码,每输入一行就执行一行。)
2: 在命令行模式下,可以直接运行.py文件。
(相当于启动了Python解释器,然后一次性把.py文件的源代码给执行了,你是没有机会以交互的方式输入源代码的)
 

二 python代码运行助手:

网站提供了一个learning.py脚本,可以在线编写python脚本点击run运行。
可以查看学习下learning.py这个脚本。
 

三 输入输出:

输出字符串print(‘hello, world!’)
输出多个字符串: print(‘hello, world!’, ‘ thanks!’) 用,号隔开
输出整数或者计算结果: print(300)  print(200+300)  print(‘200+300=’,200+300)
Python提供了一个input(),可以让用户输入字符串,并存放到一个变量里。
注意: 计算机程序中,变量不仅可以为整数或浮点数,还可以是字符串,因此,name作为一个变量就是一个字符串。
例1:
>>> name =input()
xianyu
>>> name
'xianyu'
例2:
>>> name =input()
123
>>> name
'123'
例3:
input()可以让你显示一个字符串来提示用户:
>>> name =input('please input your name:')
please input yourname:123
>>>print('hello,', name)
hello, 123
例4:
>>>num1=input('please input the 1st number:')
please input the1st number:5
>>>num2=input('please input the 2nd number:')
please input the2nd number:6
>>>print('result of num1*num2=', num1*num2)
Traceback (mostrecent call last):
  File "<stdin>", line 1, in<module>
TypeError: can'tmultiply sequence by non-int of type 'str'
>>>print('result of num1*num2=', int(num1)*int(num2))
result ofnum1*num2= 30
 
 
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: