Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Numbers. Python Program Structure Python programs consist of: Modules Statements Expressions Objects.

Similar presentations


Presentation on theme: "Chapter 4 Numbers. Python Program Structure Python programs consist of: Modules Statements Expressions Objects."— Presentation transcript:

1 Chapter 4 Numbers

2 Python Program Structure Python programs consist of: Modules Statements Expressions Objects

3 Python Built-in types Why Use Python built-in types? Make simple programs easy to write Provides objects and supports extensions Components of extensions More efficient than custom data structures

4 Python Built-in types Built-in objects: Numbers 3.1415, 1234, 999L, 3+4j Strings ‘spam’, “guido’s” Lists [1,2,3,4], [1, [2, ‘three’], 4] Dictionaries {‘taste’: ‘yum’, ‘food’: spam} Tuples (1, ‘spam’, 4, ‘U’) Files input=open(‘file.txt’, ‘r’)

5 Numbers Python supports: Integer Floating point Literal for creating numbers, and expressions for processing them

6 Numbers Numeric literals: 1234, -24, 0 Normal integers 999999999L Long integers 1.23, 3.14e-10 Floating point 0177, 0x9ff, 0XFF Octal and hex literals 3+4j, 3.0+4.0j, 3J Complex number literals

7 Built-in Tools and Extensions Tools for processing number objects Expression operators +, *, >>, ** Built-in math functions pow, abs Utility modules random, math

8 Python Expression Operators

9 Mixed Types: Converted Up Mix numeric types: Example: 40+3.1415 Rule: first converts operands up to the type of the most complicated operand, and then performs the math on same-type operand Complexity order: integer, long integer, floating point numbers, complex numbers

10 Number in Action Basic operations and Variables: Variables are created when first assigned a value Variables are replaced with their values when used in expressions Variables must be assigned before they can be used in expressions Variables refer to objects, and are never declared ahead of time >>> a=3 #Name created >>> b=4

11 Number in Action >>> a+1, a-1 # (3+1), (3-1) (4, 2) >>> b*3, b/2 (12, 2) >>> a%2, b ** 2 # modulus(remainder), power (1, 16) >>> 2+4.0, 2.0 ** b #mixed-type conversions (6.0, 16.0)

12 Number in Action >>> c*2 Traceback(most recent call last): File “ ”, line 1, in? NameError: name ‘c’ is not defined >>> b / 2 + a >>> b/(2.0+a) >>> b/(2+a)

13 Number in Action Numeric representation: >>> b / (2.0 + a) #auto echo output 0.80000000000004 >>> print b/(2.0+a) #print rounds off digits 0.8

14 Number in Action Division: Classic, Floor, and True

15 Bitwise Operations >>> x=1 #0001 >>> x<<2 #shift left 2 bits: 0100 4 >>>x|2 #bitwise or: 0011 3 >>>x&1 #bitwise AND: 0001 1

16 Long Integers >>> 2L ** 200 >>> 2 ** 200

17 Other Numeric Tools >>> import math >>> math.pi >>> math.e >>> int(2.567), round(2.567), round(2.567,2)

18 Other Numeric Tools >>> abs(-42), 2**4, pow(2,4) (42, 16, 16) >>>int(2.567), round(2.567), round(2.4) (2, 3.0, 2.0) >>> round(2.567, 2) 2.569999999999998

19 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

20 Share Reference >>> a=3 >>> b=a

21 Share Reference >>>a=3 >>>b=a >>>a=5


Download ppt "Chapter 4 Numbers. Python Program Structure Python programs consist of: Modules Statements Expressions Objects."

Similar presentations


Ads by Google