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. %号:表示取余数
  • 相关阅读:
    HTTP协议超级详解
    css面试题(一)
    win10开机启动项管理
    Notion快捷键
    [C++]std::sort()函数使用总结
    配置环境变量时java命令可以使用,javac不能使用
    安卓4:修改系统默认颜色
    vue 安装sass-loader 提示python等
    vue echarts 柱状图 变色 显示
    k8s-生产环境部署django项目k8s-dashboard管理系统
  • 原文地址:https://www.cnblogs.com/luoxun/p/13072718.html
Copyright © 2011-2022 走看看