PROGRAMMING FUNDAMENTALS
WEEK 03 – PYTHON DATA TYPES, CLASSES AND OBJECTS & VALID VALUES FOR NUMBERS
Course Instructor :
Engr.Asma Khan
Assistant Professor,SE dept
CLASSES AND OBJECTS
So far seen how to use several types of values that Python supports:
int, float, bool, str, list, and tuple.
At this point, we step back for a moment to understand more
formally what we mean by a type, and by operators and methods
supported by the type.
In Python, every value, whether a simple integer value (such as 3)
or a more complex value (such as the string 'Hello, World!' or the list
['hello', 4, 5]) 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. 10/22/2018 2
The container idea captures the motivation behind objects.
TYPE OBJECT
Every object has associated with it a type and a value. Four objects: an integer
object with value 3, a floating point object with value 3.0, a string object with
value 'Hello World', and a list object with value [1, 1, 2, 3, 5, 8].
An object’s type indicates what kind of values the object can hold and what kind
of operations can be performed on the object. The types we have seen so far
include the integer (int), floating point (float), Boolean (bool), string (str), and list
(list) types. 10/22/2018 3
TYPE()
Python type() function can be used >>>type([1, 1, 2, 3, 5, 8])
to determine an object’s type:
<class 'list'>
>>>type(3) When used on a variable, the type()
<class 'int'> function will return the type of the object
>>>type(3.0) the variable refers to:
<class 'float'> >>> a = 3
>>>type( 'HelloWorld ') >>>type(a)
<class 'str'> <class 'int'>
10/22/2018 4
CONCEPT OF CLASS
The 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.
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.
10/22/2018 5
CLASS PERSON
10/22/2018 6
RELATIONS BETWEEN OBJECTS
instantiation
instantiation
COPY RIGHT - ASST. PROF. SYED FAISAL ALI 10/22/2018 7
CAR CLASS AND CAR OBJECTS
COPY RIGHT - ASST. PROF. SYED FAISAL ALI 10/22/2018 8
SHAPE CLASS AND SHAPE OBJECTS
10/22/2018 9
ANIMAL CLASS AND ANIMAL OBJECTS
COPY RIGHT - ASST. PROF. SYED FAISAL ALI 10/22/2018 10
ATTRIBUTES INSIDE DOG CLASS AND DOG OBJECTS
10/22/2018 11
VALID VALUES FOR NUMBER TYPES
Every object has a value that must be legal for the object’s type.
For example, an integer object can have value 3 but not 3:0 or 'three'.
The integer values can be arbitrarily large.
For example, we can create an integer object whose value is 21024:
x = 2**1024
print(x)
179769313486231590772930519078902473361797697894230657273430081157732675805500963132
708477322407536021120113879871393357658789768814416622492847430639474124377767893424
865485276302219601246094119453082952085005768838150682342462881473913110540827237163
350510684586298239947245938479716304835356329624224137216
Actually, there is a limit to how large the value stored in an integer object can be:
The value is limited by the available computer memory.
COPY RIGHT - ASST. PROF. SYED FAISAL ALI 10/22/2018 12
PYTHON FLOATING POINT - FLOAT TYPE
The Python floating point (float) type is used to represent real numbers as fractions with
finite decimal representations:
pi = 3.141592653589793
2.0**30
1073741824.0
from math import pi
print(pi)
3.141592653589793
10/22/2018 13
NUMBER TYPE OPERATIONS
Operation Description Type (if x and y are
integers)
x+y Sum Integer
x-y Difference Integer
x*y Product Integer
x/y Division Float
x//y Integer division Integer
x%y Reminder of x//y Integer
-x Negative x Integer
abs(x) Absolute value of x Integer
x**y x to power y integer
10/22/2018 14
ORDER IN WHICH OPERATORS ARE EVALUATED
The order in which operators are evaluated is defined either explicitly using parentheses or
implicitly using either the operator precedence rules or the left-to-right evaluation rule if
the operators have the same precedence.
The operator precedence rules in Python follow
COPY RIGHT - ASST. PROF. SYED FAISAL ALI 10/22/2018 15
OPERATOR PRECEDENCE
10/22/2018 16
ASSIGNMENT # 2
TO BE SUBMITTED BY SOLVING IT ON JUPYTER NOTEBOOK & EMAIL IT TO ME.
10/16/2018 17
PRACTICE PROBLEM 2.8
Evaluate the results of logical operations by explicitly and by implicitly.
Evaluate the results of operator orders by explicitly and by implicitly.
In what order are the operators in the following expressions evaluated?
(a) 2 + 3 == 4 or a >= 5
(b) lst[1] * -3 < -10 == 0
(c) (lst[1] * -3 < -10) in [0, True]
(d) 2 * 3**2
(e) 4 / 2 in [1, 2, 3]
COPY RIGHT - ASST. PROF. SYED FAISAL ALI 10/22/2018 18