数学运算符号:
+ plus 加号
- minus 减号
/ slash 斜杠
* asterisk 星号
% percent 百分号
< less-than 小于号
> greater-than 大于号
<= less-than-equal 小于等于号
>= greater-than-equal 大于等于号
照着教程输入:
[root@lc test]# vi ex3.py
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
"ex3.py" 23L, 474C written
[root@lc test]#
然后执行这个脚本:
[root@lc test]# python ex3.py
File "ex3.py", line 15
print "What is 5 - 7?" 5 - 7
^
SyntaxError: invalid syntax
[root@lc test]#
啊?!出错了,看看错误原因,提示第15行 print "What is 5 - 7?" 5 - 7 引号外的5附近有语法错误;
仔细对照自己敲的内容和原教程的内容,发现少了一个逗号,加上逗号:
[root@lc test]# vi ex3.py
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."
"ex3.py" 23L, 475C written
[root@lc test]#
执行该代码,成功:
[root@lc test]# 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 greater or equal? True
Is it less or equal? False
[root@lc test]#
加分习题:
1.使用 # 在代码每一行的前一行为自己写一个注解,说明一下这一行的作用。
[root@lc test]# vi ex3-1.py
#This will print "I will now count my chickens:"
print "I will now count my chickens:"
#This will print Hens and a space and the result of "25 + 30 / 6 = 25 + 5 = 30"
print "Hens", 25 + 30 / 6
#This will print Roosters and a space and the result of " 100 - 25 * 3 % 4 = 100 - 75 % 4 = 100 - 3 = 97"
print "Roosters", 100 - 25 * 3 % 4
#This will print "Now I will count the eggs:"
print "Now I will count the eggs:"
#This will print the result of "3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 = 6 - 5 + 0 - 0 + 6 = 7"
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
#This will print " Is thar true 3 + 2 < 5 - 7?"
print "Is it true that 3 + 2 < 5 - 7?"
#This is print the result of 3 + 2 < 5 - 7
print 3 + 2 < 5 - 7
#This will print "What is 3 + 2?" and a space and the result of "3 + 2 = 5"
print "What is 3 + 2?", 3 + 2
#This will print "What is 5 - 7?" and a space and the result of "5 - 7 = -2"
print "What is 5 - 7?", 5 - 7
#This is print "Oh, that's why it's False."
print "Oh, that's why it's False."
#This will print "How about some more."
print "How about some more."
#This will print "Is it greater?" and a space and the excute result of "5 > -2 : True"
print "Is it greater?", 5 > -2
#This will print "Is it greater or equal?" and a space and the excute result of "5 >= -2 : True"
print "Is it greater or equal?",5 >= -2
#This will print "Is it less or equal?" and a space and the excute resultl of "5 <= -2 :False"
print "Is it less or equal?", 5 <= -2
"ex3-1.py" 28L, 1446C written
[root@lc test]#
2.记得开始时的 <练习 0> 吧?用里边的方法把 Python 运行起来,然后使用刚才学到的运算符号,把Python当做计算器玩玩。
>>> 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
7
>>> 100 - 25 * 3 % 4
97
>>> 3.0 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
7.0
>>> 3.0 + 2 + 1 - 5 + 4 % 2 - 1.0 / 4 + 6
6.75
>>>
3.自己找个想要计算的东西,写一个 .py 文件把它计算出来。
[root@lc test]# vi ex2-3.py
print "1 + 2 =",1 + 2
"ex2-3.py" 1L, 22C written
[root@lc test]# python ex2-3.py
1 + 2 = 3
[root@lc test]#
4.有没有发现计算结果是”错”的呢?计算结果只有整数,没有小数部分。研究一下这是为什么,搜索一下“浮点数(floating point number)”是什么东西。
浮点数是属于有理数中某特定子集的数的数字表示,在计算机中用以近似表示任意某个实数。具体的说,这个实数由一个整数或定点数(即尾数)乘以某个基数(计算机中通常是2)的整数次幂得到,这种表示方法类似于基数为10的科学计数法。
5.使用浮点数重写一遍 ex3.py,让它的计算结果更准确(提示: 20.0 是一个浮点数)。
[root@lc test]# vi ex3-5.py
print "I will now count my chickens:"
print "Hens", 25.0 + 30.0 / 6
print "Roosters", 100.0 - 25.0 * 3.0 % 4
print "Now I will count the eggs:"
print 3.0 + 2.0 + 1.0 - 5.0 + 4.0 % 2 - 1.0 / 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
"ex3-5.py" 23L, 497C written
执行该代码:
[root@lc test]# python ex3-5.py
I will now count my chickens:
Hens 30.0
Roosters 97.0
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
[root@lc test]#
这一小节学习结束。注意到print中逗号的使用方法。