第一个Python程序
2020-05-21 本文已影响0人
皮卡丘的电光一闪
1、命令行模式(Python交互模式)
在上一章中讲到,Window系统中打开命令提示符(win + r cmd),进入命令行模式,输入python后进入python的交互模式,可以在交互模式下直接输入python代码运行。输入exit()并回车推出python的交互模式,返回到命令行模式,再次进入只需要再输入python即可。
>>> print('hello, world')
hello, world
>>> 100 + 200
300
在python交互模式中也可以直接运行python文件,即.py结尾的文件。
test.py
print(100 + 200 + 300)
python交互模式下运行test.py文件
c:> python test.py
600
2、输入和输出
输出
用print()在括号中加上字符串,就可以在屏幕上输出指定的文字。
print()函数也可以接受多个字符串,用逗号“,”隔开,就可以连成一串输出:
>>> print('The quick brown fox', 'jumps over', 'the lazy dog')
The quick brown fox jumps over the lazy dog
print()函数会一次打印每个字符串,遇到逗号会输出一个空格,因此,输出的字符串就是这样拼起来的。
也可以打印整数,或者计算结果,也可以将运算公式打印的漂亮点。
>>> print('100 + 200 = ', 100 + 200)
100 + 200 = 300
输入
input()函数,可以让用户输入字符串,并存放到一个变量中。
>>> name = input()
Michael
>>> print(name)
'Michael'
>>> print('hello,', name)
hello, Michael