full-speed-python习题解答(一)

项目地址:网页地址

full-speed-python书籍地址:https://github.com/joaoventura/full-speed-python
3.1 Exercises with numbers


  • 1.Try the following mathematical calculations and guess what is happening: (3/2),(3//2), (3%2), (3**2).

      print(3/2)
      print(3//2)
      print(3%2)
      print(3**2)
    
  • 1.5
    1
    1
    9
    

  • 2.Calculate the average of the following sequences of numbers: (2, 4), (4, 8, 9), (12,14/6, 15)

      a = [2,4]+[4,8,9]+[12,14/6,15]
      sum = 0
      for each in a:
          sum+=each
      ave = sum/len(a)
      ave
    
  • 7.041666666666667
    

  • 3.The volume of a sphere is given by 4/3πr^3. Calculate the volume of a sphere of radius 5. Suggestion: create a variable named “pi” with the value of 3.1415

      pi = 3.1415
      r = 5
      volume =4/3*(pi*pow(r,3))
      volume
    
  • 523.5833333333333
    

  • 4.Use the module operator (%) to check which of the following numbers is even or odd: (1, 5, 20, 60/7).

      can = [1,5,20,60/7]
      for each in can:
          if each % 2 == 0:
              print(each,' is even')
          else:
              print(each,' is odd')
    
  • 1  is odd
    5  is odd
    20  is even
    8.571428571428571  is odd
    

  • 5.Find some values for x and y such that x < 1/3 < y returns “True” on the Python REPL. Suggestion: try 0 < 1/3 < 1 on the REPL.

      0<1/3<1
    
  • True
    

参考链接:网页链接

猜你喜欢

转载自blog.csdn.net/qq_33251995/article/details/83215052