zoukankan      html  css  js  c++  java
  • 笨办法学Python(三)

    习题 3: 数字和数学计算

        每一种编程语言都包含处理数字和进行数学计算的方法。不必担心,程序员经常撒谎说他们是多么牛的数学天才,其实他们根本不是。如果他们真是数学天才,他们早就去从事数学相关的行业了,而不是写写广告程序和社交网络游戏,从人们身上偷赚点小钱而已。

        这章练习里有很多的数学运算符号。我们来看一遍它们都叫什么名字。你要一边写一边念出它们的名字来,直到你念烦了为止。名字如下:

      + plus  加号

      - minus  减号  

      / slash  斜杠  

      * asterisk  星号

      % percent  百分号

      < less-than  小于号

      > greater-than  大于号

      <= less-than-equal  小于等于号  

      >= greater-than-equal  大于等于号

        有没有注意到以上只是些符号,没有运算操作呢?写完下面的练习代码后,再回到上面的列表,写出每个符号的作用。例如 + 是用来做加法运算的。

     1 #-- coding: utf - 8 --
     2 print "I will now count my chickens:"
     3 
     4 print "Hens",25 + 30 / 6
     5 print "Roosters", 100 - 25 * 3 % 4
     6 
     7 print "Now I will count the eggs:"
     8 
     9 print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 +6
    10 
    11 print "Is it true that 3 + 2 < 5 - 7?"
    12 
    13 print 3 + 2 < 5 - 7
    14 
    15 print "What is 3 + 2?", 3 + 2
    16 print "What is 5 - 7?", 5 - 7
    17 
    18 print "Oh, that's why it's False."
    19 
    20 print "How about some more."
    21 
    22 print "Is it greater?", 5 > -2
    23 print "Is it greater or equal?", 5 >= -2
    24 print "Is it less or equal?", 5 <= -2
    View Code

    加分习题

      1. 使用 # 在代码每一行的前一行为自己写一个注解,说明一下这一行的作用。

      2. 记得开始时的 <练习 0> 吧?用里边的方法把 Python 运行起来,然后使用刚才学到的运算符号,把Python当做计算器玩玩。

      3. 自己找个想要计算的东西,写一个 .py 文件把它计算出来。

      4. 有没有发现计算结果是”错”的呢?计算结果只有整数,没有小数部分。研究一下这是为什么,搜索一下“浮点数(floating point number)”是什么东西。

      5. 使用浮点数重写一遍 ex3.py,让它的计算结果更准确(提示: 20.0 是一个浮点数)。

    习题练习

    1.

     1 #-- coding: utf - 8 --
     2 # 打印 I will now count my chickens:
     3 print "I will now count my chickens:"
     4 
     5 # 打印 Hens ,显示 (25 + 30 / 6) 的值
     6 print "Hens",25 + 30 / 6
     7 # 打印 Roosters ,显示(100 - 25 * 3 % 4)的值
     8 print "Roosters", 100 - 25 * 3 % 4
     9 
    10 # 打印 Now I will count the eggs:
    11 print "Now I will count the eggs:"
    12 
    13 # 打印 (3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 +6)的值
    14 print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 +6
    15 
    16 # 打印 Is it true that 3 + 2 < 5 - 7?
    17 print "Is it true that 3 + 2 < 5 - 7?"
    18 
    19 # 打印 (3 + 2 < 5 - 7)的结果
    20 print 3 + 2 < 5 - 7
    21 
    22 # 打印 What is 3 + 2? ,显示(3 + 2)的值
    23 print "What is 3 + 2?", 3 + 2
    24 # 打印 What is 5 - 7? ,显示(5 - 7)的值
    25 print "What is 5 - 7?", 5 - 7
    26 
    27 # 打印 Oh, that's why it's False.
    28 print "Oh, that's why it's False."
    29 
    30 # 打印 How about some more.
    31 print "How about some more."
    32 
    33 # 打印 Is it greater? ,显示(5 > -2)的结果
    34 print "Is it greater?", 5 > -2
    35 # 打印 Is it greater or equal? ,显示(5 >= -2)的结果
    36 print "Is it greater or equal?", 5 >= -2
    37 # 打印 Is it less or equal? ,显示(5 <= -2)的结果
    38 print "Is it less or equal?", 5 <= -2
    View Code

    运算优先级是怎样的?

        在美国,我们用 PEMDAS 这个简称来辅助记忆,它代表的是Parentheses(括号)、Exponents(指数)、Multiplication(乘)、Division(除)、Addition(加)、

    Subtraction(减),这就是Ruby里的运算优先级。

    4.

    #-- coding: utf - 8 --
    print "Hens",25.0 + 30 / 4, 25 + 30.0 / 4, 25 + 30.00 /4

  • 相关阅读:
    springboot+https+http
    3.kettle-定时执行任务
    sqlserver清空删除日志
    C++学习(二)
    随笔(二) 安装Code::Blocks遇到的问题
    随笔(一) tensorflow环境的搭建
    C++学习(一)
    前端学习日记 (三)
    前端学习日记 (二)
    前端学习日记 (一)
  • 原文地址:https://www.cnblogs.com/yllinux/p/7048345.html
Copyright © 2011-2022 走看看