Learn Python The Hard Way学习(3) - 数字和数学计算
来源:互联网 发布:棘轮设计软件下载 编辑:程序博客网 时间:2024/04/30 02:06
每种程序语言都有数学计算方法,程序员经常认为自己是数学天才,其实不是,如果是数学天才的话,就会去做数学相关的工作了,而不是写一下广告程序和社交游戏赚点小钱。
数学符号我们就不去认识,大家都知道。直接上代码吧:
print "I will now count my chickens:"print "Hens", 25 + 30 / 6print "Roosters", 100 - 25 * 3 % 4print "Now I will count the eggs:"print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6print "Is it true that 3 + 2 < 5 - 7?"print 3 + 2 < 5 - 7print "What is 3 + 2?", 3 + 2print "What is 5 - 7?", 5 - 7print "Oh, that's why it's False."print "How about some more."print "Is it greater?", 5 > -2print "Is it greatet or equal?", 5 >= -2print "is it less or equal?", 5 <= -2
运行结果应该是这样的:
root@he-desktop:~/mystuff# python ex3.py
I will now count my chickens:
Hens 30
Roosters 97
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5
What is 5 - 7? -2
Oh, that's why it's False.
How about some more.
Is it greater? True
Is it greatet or equal? True
is it less or equal? False
root@he-desktop:~/mystuff#
加分练习
1. 用#号给每行加个注释。
# 百分号是取余数的意思
print "Roosters", 100 - 25 * 3 % 4
2. 像练习(0)一样,在Terminal中输入python,把python运行起来,然后逐句执行上面的代码。
3. 自己找一个要计算的东西,写一个py文件。
4. 看看上面的程序是不是有错误,你会发现只有整数,没有小数,搜索并了解一下“浮点数”。
5. 用浮点数编写ex3.py,让结果更加正确。
修改第5句为:
print 3 + 2 + 1 - 5 + 4 % 2 - 1.0 / 4 + 6
运行后的结果是:6.75
- Learn Python The Hard Way学习(3) - 数字和数学计算
- 学《learn python the hard way》(3)
- 《Learn Python the hard way》学习记录
- 笨办法学 Python (Learn Python The Hard Way)
- 翻译学python---《Learn Python the hard Way》---第一章 绪论
- Learn Python The Hard Way-笨方法学Python
- 跟着《Learn Python The Hard Way》学Python(一)
- 跟着《Learn Python The Hard Way》学Python(二)
- Learn Python The Hard Way
- Learn Python The Hard Way
- 学《learn python the hard way》(1)
- 笨方法学习Python-习题3:数字和数学计算
- 学习笔记--python(1)--learn python the hard way
- 【Python】Learn Python the hard way, ex0 学习使用Terminal
- Learn Python The Hard Way学习(41) - 学习面向对象
- Learn Python The Hard Way学习(0) - 安装
- Learn Python The Hard Way学习(1) - 好的开始
- Learn Python The Hard Way学习(2) - 注释和#号
- vim
- 黑马程序员-Java语言基础– 多线程 第12天
- 新算法快速定位基因异常
- unix/linux多进程编程2-进程控制
- 正则表达式30分钟入门教程
- Learn Python The Hard Way学习(3) - 数字和数学计算
- 黑马程序员-Java语言基础– String类 第13天
- 配置vxsim虚拟网卡(tornado)
- Tap anywhere on the screen to continue
- ArcGIS Engine10.0轻松入门级教程(1)——必备基础知识
- shell扫盲篇:Shell常见知识(1)
- 老程序员的忠告:不要做浮躁的软件工程师
- 用RenderMonkey编写shader[2]
- 我学习安卓时候的新手入门资料,送你们了