print()里也可以算加减乘除
说给初学者的话:programmers frequently lie about being math geniuses when they really aren’t. If they were math geniuses, they would be doing math, not writing buggy web frameworks so they can drive race cars.
就是说,做程序不需要是数学天才。如果程序员都是数学天才,他们应该在研究数学,而不是被叫码农了。哈哈~~不过换句话说,做程序也并不高大上,无奈哈
以下数学符号,边打边说出它们的名字:(学英语时间)
- +:plus —— 加
- -:minus —— 减
- /:slash —— 除
- *:asterisk —— 乘
- %:percent —— 求余数 如:10 % 3 = 1
- <:less-than —— 小于
- >:greater-than —— 大于
- <=:less-than-equal —— 小于等于
- >=:greater-than-equal —— 大于等于
输入以下代码
print("I will now 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 equal?", 5 >= -2)
print("Is it less or equal?", 5 <= -2)
What You Should See
I will now count my chickens:
Hens 30.0
Roosters 97
Now I will count the eggs:
6.75
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 greater or equal? True
Is it less or equal? False
Study Drill 扩展练习
- 给每一行添加注释 ” # "
- 写一个新的 .py 文件,计算你想计算的事情
- 重写ex3的代码,用上浮点数 float(),使得出的数字更精确。20.123就是浮点数
1、懒得做
2、简单做
print("一太太问我:你知道我多少岁么?")
print("我看你这身材像14岁")
print("看你衣着口味像24岁")
print("看你的背景像28岁")
print("太太高兴地说:到底多少岁?")
print("你把我刚才说的数字加起来算一下")
print("太太数了后说",14 + 24 +28)
print("恭喜答对了,88~~撒退就跑")
3、把所有数字前都加上float即可,如
print("Hens", float(25 + 30 / 6))
print("Roosters", float(100 - 25 * 3 % 4))
在这一节,我们大量使用了print,这函数引号里面的是就是直接输出的结果,没引号的,如数字会计算出结果再输出。课外话,注意逗号和+的用法,我刚开始学经常搞混,下面举例:
print('abc','def') ==> abc def # 用逗号没用合并
print('abc'+'def') ==> abcdef # 用+号把两个字符串并起来了