首页 > 编程语言 > 详细

python 基础语法

时间:2017-12-08 20:21:06      阅读:205      评论:0      收藏:0      [点我收藏+]

1. print

3.x 中print作为函数

print("1", "2") 即输出 1 2 

2.x print不为函数,所以

print "1","2" 即输出1 2

 

2. input 与 raw_input

raw_input

raw_input() 将所有输入作为字符串看待,返回字符串类型。//姑且当成gets()

比如:

gender = raw_input("what‘s you gender?")
print(gender)

输入:male
输出:
male
<type str>

 

input

input() 在3.x版本下:

input() 在输入不为整形和浮点型时,相当于eval(raw_input(prompt))

其中 eval 将字符串str当成有效的表达式来求值并返回计算结果。比如:

字符串转换成列表
>>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>>type(a)
<type str>
>>> b = eval(a)
>>> print b
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
>>> type(b)
<type list>

如果输入为整数或者浮点数,那么直接返回该数字。//可以当成输入、输出模板的input用了。

如下:

c = input("please input number")
print c
print type(c)

输入:2.31
结果:
2.31 <type float>

 

python 基础语法

原文:http://www.cnblogs.com/chenhuan001/p/8006760.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!