前言
学习一门编程语言,最基本的无非不过学习其变量规则、条件语句、循环语句和函数,接下来的几节将开始记录这些基本的语法,本节主要记录变量规则!
一、Python输入输出
在说Python变量之前,先补充一下Python的输入和输出语句,在Python的编写过程中,会遇到许多的错误,基本的调试方法就是将中间变量打印(输出)出来,所以说Python的输入输出语句十分重要,尤其是输出语句,一定要灵活掌握。
输出
前文可能已经接触到了输出语句 print,实际上在Python3中它是一个内置函数(关于函数的概念之后会讲),在Python常被成为打印,具体用法如下:
1.查看帮助信息
在IPyone中输入help(print),得到其帮助信息,如果你想要查看其他内置函数的帮助信息,也是用这种方法哦
In [1]: help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=‘ ‘, end=‘\n‘, file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
可以看到print函数的参数有value、sep、end、file、flush
其中sep、end、file、flush都已经被赋值了,也就是说,这几个参数都有默认值了,需不需要改要看自己的需求,而value没有被赋予默认值,也就是说,你必须要给value赋值才行,如果你还是不明白,来看例子:
2.value
由于value参数位于print函数的第一位置,所有给其赋值有两种方式: print(value=‘hi‘) 或者 print(‘hi‘) ,当然大家全部倾向于后者
可以传入多个参数,中间用逗号隔开:
In [2]: print(‘hello‘,‘hi‘,‘i am the best man in the world!‘)
hello hi i am the best man in the world!
可以是计算式,打印其结果:
In [3]: print(1+4)
5
学了上面两种方法后,组合起来