Download presentation
Presentation is loading. Please wait.
Published byChristopher Bates Modified over 6 years ago
1
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012
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)
10
Number in Action >>> 1 / 2.0 0.5 >>> 1/2
11
Long Integers >>> 2L ** 200 >>> 2 ** 200
12
Class Practice >>> num= 1/3.0 >>> num
>>> 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 >>> math.e
15
Other Numeric Tools Random Library >>> import random
>>> random.randint(1,100) 25 45 17
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( ) >>> round( , 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.