学习资料:笨方法学Python
准备:
- 安装环境----请自行网络搜索(Windows安装很简单,和其他安装程序一样)
- 找一个自己习惯的编辑器(比如:sublime text 3)
- 创建一个专门的目录,按着资料把所有的代码敲一遍 -- 可了解下DOS基本命令
1、第一个程序
文件命名方式: 文件.py
执行方式:python 文件.py (在该文件路径下执行)
若执行错误,会有报错提示,看不懂可以从搜索引擎中查询
正确代码及结果:
错误代码及结果:可以根据提示进行搜索错误原因,并可通过提示查找对应的错误代码行进行检查
习题2:注释与井号----程序里的注释是很重要的
代码:
# A comment,this is so you can read your program later.
# Anything after the # is ingnored by python.
print "I could have code like this." # and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run"
运行:
习题3:数字和数学计算
• + plus 加号
• - minus 减号
• / slash 斜杠
• * asterisk 星号
• % percent 百分号
• < less-than 小于号
• > greater-than 大于号
• <= less-than-equal 小于等于号
• >= greater-than-equal 大于等于号
代码:建议养成良好的代码习惯,加空格
print "I will count my chickens:"
print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4
print "Now I will count the eggs:"
print 3 + 2 + 1 - 5 + 4 % 2 - 1 /4 + 6
print "Is it true that 3 + 2 < 5 - 7?"
print 3 + 2 < 5 - 7
print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7
print "Oh, that's why it's False."
print "How about some more."
print "Is it greater?", 5 > -2
print "Is it greater or egual?", 5 >= -2
print "Is it less or egual?", 5 <= -2
结果: