python 基础语法

1. print

3.x 中print作为函数

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

2.x print不为函数,所以

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

 补充一点,因为python2.7中print一定会带换行,需要输出而不带换行可以用下面这种方法。

import sys

sys.stdout.write('no cl')
sys.stdout.write('..')

 

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'>

 

posted @ 2017-12-08 18:53  chenhuan001  阅读(198)  评论(0编辑  收藏  举报