今天安装了Debian5,没想到基础安装的情况下居然会有python,于是安装试试这个传说中的语言。
下面通过一系列简单示例来体验,代码可以直接粘贴保存,都通过测试。
我参照pdf学习的,大家可以从这个地址下载pdf文件.
案例1:
#用于注释,但第一行#!也给程序指出本代码是靠/usr/bin/python执行的,所以文件名可以不是py
如果你乐意,你可以给helloworld.py 增加属性X,这样你可以把它改成任意的文件名直接执行,如hello。
#!/usr/bin/python
#filename:helloworld.py
print '\n\tHello World! That\'s Python!\n'
#if you want the .py file to be executable:chmod a+x helloworld.py
#filename:helloworld.py
print '\n\tHello World! That\'s Python!\n'
#if you want the .py file to be executable:chmod a+x helloworld.py
案例2:
变量不用声明可以直接赋值;if/elif/else语句不用包括判断语句且以:结尾;
#!/usr/bin/python
#filename:raw_input.py
number = 23 #here no ; but you can add ; either
guess = int(raw_input("Enter an Integer:")); #string,can use ' or "
if guess == number: #notice: no () and there's a :
print 'Congratulations, you guessed it.'
print '(but you do not win any prizes!)'
elif guess < number:
print "No, It's a little higher than what you input."
else:
print 'No,It\'s a little lower than what you input.'
print "Done!";
#filename:raw_input.py
number = 23 #here no ; but you can add ; either
guess = int(raw_input("Enter an Integer:")); #string,can use ' or "
if guess == number: #notice: no () and there's a :
print 'Congratulations, you guessed it.'
print '(but you do not win any prizes!)'
elif guess < number:
print "No, It's a little higher than what you input."
else:
print 'No,It\'s a little lower than what you input.'
print "Done!";