180 likes | 329 Vues
This chapter covers the foundational aspects of Python programming focusing on numbers, variables, and expressions. It explains built-in types including integers, long integers, floating-point numbers, octal, hexadecimal, and complex numbers. The chapter also introduces essential mathematical operators, assignment principles, and variable usage in expressions, emphasizing Python’s dynamic typing. Additional topics include mathematical functions from the math module and representations of numbers in different bases. This knowledge forms a basis for more advanced programming concepts in Python.
E N D
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’ • Lists [1,2,3,4], [1,2,[3,4]] • Dictionaries {‘test’: ‘yum’} • Tuples (1, 2, 3, 4) • Files input=open(‘file.txt’, ‘r’).read()
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
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?
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)
Variable _ • In interactive mode, the last printed expression is assigned to the variable _. >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax >>> price + _ >>>_*tax
Comparison and Value Equality Operators • Value equality (a == b, a <> b, a != b) • Comparison operators (a < b, a > b, a <=b, a >= b)
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.
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)
Long Integers • Arbitrarily big • Convert integer to long integer if overflow occurs (after 2.2) • More precision vs lower performance
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)
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)
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.
>>>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.
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”
Garbage Collection >>>a=3 >>>a=5 5