python 第一课-----输入输出

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)
一个简型的计算器。

 

转载于:https://www.cnblogs.com/o-to-s/articles/4153262.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值