>>> def to_celsius(t):
… return (t-32.0)*5.0/9.0
…
在函数名后用冒号,不是等号。函数实际定义在下一行,该行缩进4个空格,并以return 标记。
str(t),将t转换为字符串
>>>’12’+str(34)+’56’
‘123456’
在字符串两端的单引号或者双引号分别替代成3个,即可扩展为多行字符串。
用print语句来实现打印输出。
>>> line = 5
>>> print 'There is a error in line:', line,'!'
格式化打印输出则为>>> print 'There isa error in line: %d !' % line
>>> col = 8
>>> print 'There isa error in line: %d at column %d!' % (line,col)
There is a error in line: 5at column 8!
使用raw_input()来得到用户输入,还可以接收一个字符串参数以对用户进行提示
>>> line =raw_input()
gogogo
>>> line
'gogogo'
>>> name =raw_input("What is your name\n")
Python可以指定引入某个模块中所需的,如下是从math中引入所需的sqrt和pi
>>> from mathimport sqrt, pi
>>> sqrt(9)
Python在引入模块的时候就会将其执行一次,但只会在第一次引入时进行加载。
条件语句块
if condition:
Block
elif condition:
Block
else:
Block
for variable in ssss:
block
while condition:
block
一些有用的函数range, enumerate,