1 / 17

Chapter 4 Numbers

Chapter 4 Numbers. CSC1310 Fall 2008. volume.py:. Python Program Structure. Program Module Statement Expression. import math r=int(raw_input()) print 4.0/3*math.pi*r**3. Python Built-in types. Numbers 3.1415, 1234, 999L, 3+4j Strings “Hello World”, ‘spam’

soyala
Télécharger la présentation

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 CSC1310 Fall 2008

  2. volume.py: Python Program Structure • Program • Module • Statement • Expression import math r=int(raw_input()) print 4.0/3*math.pi*r**3

  3. Python Built-in types • Numbers 3.1415, 1234, 999L, 3+4j • Strings “Hello World”, ‘spam’ • Lists [1,2,3,4], [1,2,[3,4]] • Dictionaries {‘test’: ‘yum’} • Tuples (1, 2, 3, 4) • Files input=open(‘file.txt’, ‘r’).read()

  4. Numbers • Integer 222, -24, 0 (32 bit) [-231 , 231- 1] • Long integer 99999999L ,33657l • Floating-point 1.23, 123e-2, 0.123e+2 • Octal literals 0177, 01234, 0456 (0-8) • Hex literals 0x9ff, 0XFf (0-9, A,B,C,D,E,F) • Complex number literals 3+4j, 3j, 4J+3, complex(3,4)(real,imag) realpart+imaginarypart j realpart+imaginarypart J

  5. Assignment • Variables – a name that is used to keep some information • To create variable, assign a value to it. • Variable in expression is replaced by its value. • Variable should be assigned before it is used in expression. >>> a=3 #Name Created >>> b=4 >>>s=d=0 What will happen if I ask to print variable c at this point?

  6. Mathematical Operators • Addition (a + b) • Subtraction (a - b) • Multiplication (a * b) • Division (a / b, a // b) (integer, floating-point; floor division) (from 3.0: true and floor division) • Remainder/format (a % b) • Binary power (a ** b) • Unary negation (- b) • Identity (+ b)

  7. Variable _ • In interactive mode, the last printed expression is assigned to the variable _. >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax >>> price + _ >>>_*tax

  8. Comparison and Value Equality Operators • Value equality (a == b, a <> b, a != b) • Comparison operators (a < b, a > b, a <=b, a >= b)

  9. Mixed Operators, Types • Operator Precedence • unary negation, identity, binary power • multiplication, remainder/ format, division • addition, subtraction • comparison and value equality operators • Parentheses to group sub-expressions • Different numeric types in expression: • Python converts operand up to the type of the most complicated operand and then calculates result. • Integer -> Long integer-> Floating-point numbers-> Complex numbers.

  10. Numeric Representation • Interactive prompt shows more digits than print >>> b / (2.0 + a) >>> print b/(2.0+a) • String formatting >>> num=0.33333333 >>> “%e” % num >>> “%2.2f” % num >>> str(num) >>> repr(num)

  11. Long Integers • Arbitrarily big • Convert integer to long integer if overflow occurs (after 2.2) • More precision vs lower performance

  12. Hexadecimal and Octal Notations • Octal notation (base 8) • leading zero (0) (!!!!!) • octal digits 0-7 (each represents 3 bits) >>> oct(64) • Hexadecimal notation (base 16) • leading 0X or 0x • hex digits 0-9 (each represents 4 bits) • upper- or lowercase A-F (each represents 4 bits) >>> hex(64) • String to integer and vise-verse >>> int(’64’), int (‘0xf’,16), int (‘034’,8) >>> eval(’64’), eval(‘0xf’), eval(‘034’) #slower >>> “%o %x %X” % (64,64,255)

  13. Built-in functions and modules >>>import math >>>math.pi, math.e >>>math.cos(2*math.pi/3) #sin, tan, acos, asin, atan >>>math.exp(4) #math.e**4 >>>math.pow(math.e,4), pow(math.e,4) >>>math.log (math.e**4) >>>math.sqrt(4) >>>math.ceil(2.0/3), math.floor(2.0/3) >>>int(2.567), round(2.567), round(2.4) #float,long >>>print round(2.567,2) >>>abs(-2), abs(2.0) #available without import >>>divmod(3,4)

  14. Dynamic Typing • Types are determined at runtime (no need for declaration as in C++, JAVA) • Creation: variable is created when it is first assigned a value. • Types: there is no type information associated with variable. Variable refers to some object of a particular type. • Use: when a variable is in expression, it is immediately replaced with a referenced object. Unassigned variable results in error.

  15. >>>a = 3 • Create an object • to represent the value of 3 2. Create the variable a, if it does not yet exist 3. Link the variable a to the new object 3 • Variables always link to objects • Larger objects may be link to other objects.

  16. Shared Reference • Objects are allocated memory • Variables are entries in search table with space for a link to an object (pointers, not labels) >>>a=3 >>>b=a >>>a=“change” “change”

  17. Garbage Collection >>>a=3 >>>a=5 5

More Related