1 / 22

Chapter 4 Numbers

Chapter 4 Numbers. Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012. Python Build-in types. Numbers 3.1415 Strings “Hello World” Lists [1,2,3,4] Files input=open(‘file.txt’, ‘r’). Numbers.

Download Presentation

Chapter 4 Numbers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Numbers Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012

  2. Python Build-in types • Numbers 3.1415 • Strings “Hello World” • Lists [1,2,3,4] • Files input=open(‘file.txt’, ‘r’)

  3. Numbers Many different types, we only provide 3 here for now: • Integer • Floating-point • Long integer

  4. Number in Action • Perhaps the best way to understand numerical objects is to see them in action. >>> a=3 #Name Created >>> b=4

  5. Number in Action >>> a+1 # (3+1) 4 >>> a-1 #(3-1) 2

  6. Number in Action >>> b*3 12 >>>b/2 2

  7. Number in Action >>> b**2 #power 16 >>> a%2 #remainder 1

  8. Number in Action >>> 2+4.0, #mix type 6.0 >>> 2.0**b 16.0

  9. Number in Action >>> b / 2 + a >>> b/(2.0+a) >>> b/(2+a)

  10. Number in Action >>> 1 / 2.0 0.5 >>> 1/2 0

  11. Long Integers >>> 2L ** 200 >>> 2 ** 200

  12. Class Practice >>> num= 1/3.0 >>> num >>> 2L*200 >>> a=10 >>> b=20 >>> c=a+b >>> c

  13. Other Numeric Tools >>>int(2.567) 2 >>> round(2.567) 3.0 >>> round(2.567, 2) 2.57

  14. Other Numeric Tools Math library >>> import math >>> math.pi 3.141592653589793 >>> math.e 2.718281828459045 http://docs.python.org/library/math.html

  15. Other Numeric Tools • Random Library >>> import random >>> random.randint(1,100) 25 >>> random.randint(1,100) 45 >>> random.randint(1,100) 17 http://docs.python.org/library/random.html

  16. Dynamic Typing >>> a = 3 • Create an object to represent the value of 3 • Create the variable a, if it does not yet exist • Link the variable a to the new object 3

  17. a = 3

  18. Share Reference >>> a=3 >>> b=a

  19. Share Reference >>>a=3 >>>b=a >>>a=5

  20. Dynamic Typing >>>a=3 >>>b=a >>>a=5.0

  21. Class Practice >>> int(3.1415926) >>> round(3.1415926) >>> round(3.1415926, 3) >>> import math >>> round(math.pi, 3)

  22. Class Practice >>> a=10 >>> b=20 >>> a+b >>> a=a+10 >>> a >>> b=b+10.0+a >>> b

More Related