Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Numbers CSC1310 Fall 2008. Python Program Structure import math r=int(raw_input()) print 4.0/3*math.pi*r**3 volume.py: Program Program Module.

Similar presentations


Presentation on theme: "Chapter 4 Numbers CSC1310 Fall 2008. Python Program Structure import math r=int(raw_input()) print 4.0/3*math.pi*r**3 volume.py: Program Program Module."— Presentation transcript:

1 Chapter 4 Numbers CSC1310 Fall 2008

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

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) [-2 31, 2 31 - 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 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)

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. 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) Octal notation (base 8)  leading zero (0) (!!!!!)  octal digits 0-7 (each represents 3 bits) >>> oct(64) Hexadecimal notation (base 16) 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 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 runtime Types are determined at runtime (no need for declaration as in C++, JAVA) Creation Creation: variable is created when it is first assigned a value. Types: Types: there is no type information associated with variable. Variable refers to some object of a particular type. Use Use: when a variable is in expression, it is immediately replaced with a referenced object. Unassigned variable results in error.

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

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

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


Download ppt "Chapter 4 Numbers CSC1310 Fall 2008. Python Program Structure import math r=int(raw_input()) print 4.0/3*math.pi*r**3 volume.py: Program Program Module."

Similar presentations


Ads by Google