Top Banner
Chapter 4 Numbers
21

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

Jan 12, 2016

Download

Documents

Lynn Simmons
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 4 Numbers. Python Program Structure Python programs consist of: Modules Statements Expressions Objects.

Chapter 4 Numbers

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

Python Program Structure

Python programs consist of: Modules Statements Expressions Objects

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

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

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

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’)

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

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

processing them

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

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

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

Built-in Tools and Extensions

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

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

Python Expression Operators

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

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

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

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

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

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)

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

Number in Action>>> c*2 Traceback(most recent call last): File “<stdin>”, line 1, in?NameError: name ‘c’ is not defined

>>> b / 2 + a

>>> b/(2.0+a)

>>> b/(2+a)

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

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

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

Number in Action

Division: Classic, Floor, and True

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

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

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

Long Integers

>>> 2L ** 200

>>> 2 ** 200

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

Other Numeric Tools

>>> import math

>>> math.pi

>>> math.e

>>> int(2.567), round(2.567), round(2.567,2)

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

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

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

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

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

Share Reference

>>> a=3

>>> b=a

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

Share Reference

>>>a=3

>>>b=a

>>>a=5