click below
click below
Normal Size Small Size show me how
Objects and Classes
Python
Question | Answer |
---|---|
object | In Python every value is stored in memory as an object. It is useful to think of an object as a container for the value that sits inside your computer's memory. |
object type | indicates kind of values object can hold and operations that can be performed on the object. Examples: integer (int), floating point (float), Boolean (bool), string (str), and list (list) type() function can be used to determine an object's type |
variable and object type | variables do not have types the objects variables refer to have types |
object-oriented | Python programming language is said to be object-oriented because values are always stored in objects. In programming languages other than Python, values of certain types are not stored in abstract entities such as objects but explicitly in memory. |
class | The term class is used to refer to types whose values are stored in objects. Because every value in Python is stored in an object, every Python type is a class. In this book, we will use class and type interchangeably. |
float | represents real numbers as fractions with finite decimal representations |
number type and algebraic expressions | If both operands x and y (are integers, the result is an integer. If one of the operands is a float value, the result is a float value. For division (/), the result is a float value, regardless of the operands. |
7 comparison operators for numbers | < Less than <= Less than or equal > Greater than >= Greater than or equal == Equal != Not equal |
constructor | explicitly instantiate object type x = int() float(), list(), and str() |
Implicit type conversion | float contains integer contains boolean If an algebraic or logical expression involves operands of different types, Python will convert each operand to the type that contains the others. |
Operator Precedence | List definition Indexing operator Exponentiation Positive, negative signs Product, division, integer division, remainder Addition, subtraction Comparisons, including membership and identity tests Boolean NOT Boolean AND Boolean OR |
Explicit Type Conversions | >>> int(3.4) 3 >>> float(3) 3.0 >>> str(2.72) ‘2.72’ |
methods | We have a list object, pets, followed by a dot (.), followed by the method (function) call. EX: pets.append(‘guinea pig’) The list method append() is called on the list object pets with string input ‘guinea pig’. |
Python Standard Library | Consists of thousands of functions and classes organized into components called modules that relate to particular application domain. More than 200 built-in modules together form the Python Standard Library. |
Module math | To use a math module function >>> import math sqrt(x) ceil(x) (i.e., the smallest integer = x) floor(x)(i.e., the largest integer = x) cos(x) cos(x) sin(x) sin(x) log(x, base) logbase(x) pi 3.141592653589793 e 2.718281828459045 |
Module fractions | >>> import fractions >>> a = fractions.Fraction(3, 4) >>> b = fractions.Fraction(1, 2) As with other numbers, Fraction objects can be added, and the result is a Fraction object: >>> c = a + b >>> c Fraction(5, 4) |
float vs fraction | the range of values that float objects can store is limited The range of values representable with fractions.Fraction objects is much, much larger and limited only by the available memory expressions involving float values evaluate much faster |