PYTHON 输出
print:
打印字符类型:
>>> print "hello world!!!"
hello world!!!
打印数字类型:
>>> print 12
12
也可以把值给一个变量,然后再输出到屏幕:
>>> x = 12
>>> print x
12
>>> type(x)
<type 'int'>
格式化输出:
左对齐
>>> print(format(12.3456,'2.3f'))
12.346
右对齐
>>> print(format(12.3456,'8.3f'))
12.346
从以上两种输出方式可以看出,如果你想左对齐就把长度设置尽量的小,如果你想右对齐就把长度设置的大一些
注: 这里的长度是指m.nf 中的m.
输出带百分号的值:
>>> print(format(0.23456,'2.2%'))
23.46%
输入的方法
>>> str = raw_input("plz enter your name:")
plz enter your name:hxg
>>> print str
hxg
>>> help(raw_input)
Help on built-in function raw_input in module __builtin__:
raw_input(...)
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled. The prompt string, if given,
is printed without a trailing newline before reading.
可以看下这个函数的帮助, 返回值只能是字符类型的
>>> int = raw_input("plz enter your number:")
plz enter your number:123
>>> print int
123
>>> type(int)
<type 'str'>
如果我们想接受别的类型呢, 可以参考下转换的函数:
转换为数字:
>>> a = int(raw_input("plz input your number:"))
plz input your number:123
>>> print a
123
>>> type(a)
<type 'int'>
转换为FLOAT:
>>> b = float(raw_input("plz input your number:"))
plz input your number:12.3
>>> print b
12.3
>>> type(b)
<type 'float'>
如果想对数据进行计算呢?
>>> help(input)
Help on built-in function input in module __builtin__:
input(...)
input([prompt]) -> value
Equivalent to eval(raw_input(prompt)).
>>> c = input("plz enter your number:")
plz enter your number:1+2*3
>>> print(c)
7
这也就可以用PYTHON写成一个计算器的小脚本:
[root@localhost ~]# python 1.py
plz Enter your number:2*8*9
144
[root@localhost ~]# cat 1.py
#!/usr/bin/python
a = input("plz Enter your number:")
print(a)
一个简型的计算器。