zoukankan      html  css  js  c++  java
  • 练习3--数字和数学计算

    1 常见的数学符号

    • + plus,加号
    • - minus,减号
    •  / slash,斜杠
    • * asterisk,星号
    • % percent,百分号
    • < less-than,小于号
    • > greater-than,大于号
    • <= less-than-equal,小于等于号
    • >= greater-than-equal,大于等于号

    2  一个例子:

    python2语法下的代码:

       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

    python3语法下的代码:

       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

    3 运行结果:

    python2语法下执行结果:

         $ python ex3.py
         I will now count my chickens:
         Hens 30
         Roosters 97
         Now I will count the eggs:
         7Is 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
         $
     
    python3语法下执行结果:
         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 is why it's False.
         How about some more.
         Is it greater? True
         Is it greater or equal? True
         Is it less or equal? False

    4 扩展

    1. python2里面”/“运算小数点后的数会被丢掉,python3里面”/“运算可以输出小数点后的值,所以例子里面“鸡蛋的数量”运算结果不一样,一个是7,一个是6.75。(注意:可能python3自己完成了整数和浮点数的转换?)
    2. 运算顺序:括号内优于(乘、除、取余%)优于(加、减),即PE(M&D)(A&S)
    3. %号:表示取余数
  • 相关阅读:
    docker学习笔记(一)-vagrant/docker machine安装docker,阿里云通过docker machine安装docker
    docker安装 centos7
    Robot Framework user guide
    Powershell 备忘
    如何在linux系统内用openssl 生成 过期的证书
    同时装了Python3和Python2,怎么用pip?
    python 基础笔记三
    python 基础笔记二
    python对文件的操作
    3-4 字典的嵌套
  • 原文地址:https://www.cnblogs.com/luoxun/p/13072718.html
Copyright © 2011-2022 走看看