INTEGRATIVE noticeably is the way the print
statement works, as in Python 3.0
PROGRAMMING the print statement has been
Python Reviewer replaced with a print () function.
(Galing sa kabilang section)
Integrative Programming
Brief History
• Programming w/ purpose of
• designed by Guido van Rossum in
combining and coordinating
1991 as a member of the National
separate elements as to construct
Research Institute of Mathematics
an interrelated whole
and Computer Science (NRIMCS)
• developed by Python Software
Syntax
Foundation.
• collection of rules of validity,
• Initially designed as a response to
sequence, and symbols.
the ABC programming language that
• structure/form of the code that a
was also foregrounded in the
specific programming
Netherlands.
language specifies.
• Main features (compared to ABC) :
Python had exception handling and
Semantics
was targeted for the Amoeba
• refers to the interpretation of the
operating system
syntax/ code or the associated
• It was mainly developed for
meaning of the symbols, characters
emphasis on code readability, and
or any part of a program.
its syntax allows programmers to
express concepts in fewer lines of
Machine Language
code.
• 0 and 1.
Python Versions
Assembly Language
• Python 0.9.0 (1991) : In addition to
• low-level programming language
exception handling, Python included
designed for a specific type of
classes, lists, and strings. More
processor.
importantly, it included lambda, map,
filter and reduce.
• Python 2.0 (2000): More of an High Level Language
open-source project from members
• C, FORTRAN, or Pascal that
of the NRIMCS. This version of
enables a programmer to write
Python included list
programs that are more or less
comprehensions, a full garbage
independent of a particular type of
collector, and it supported Unicode.
computer.
• Python 3.0 (Dec 2008) (the latest
• Closer to human language.
version of Python is 3.6.4): Python
2 and 3 are similar but have subtle
differences. Perhaps most
Object Oriented
• Better control of structure of long
programs.
Compiled Language
• converted directly into machine code
that the processor can execute.
• you need to “rebuild” the program
• List: Changeable order [1,2,3,4]
every time you need to make a
• Tuple: Unchangeable order (1,2,3,4)
change
• eg: C, C++, Erlang, Haskell, Rust,
and Go. Identifier
• run through a program line by line
• name used to identify a variable,
and execute each command.
function, class, module or other
• eg: PHP, Ruby, Python, and
object. An identifier starts with a
JavaScript.
letter A to Z or a to z or an
Variable underscore (_) followed by zero or
more letters, underscores and digits
• used to store information to be
(0 to 9).
referenced and used by programs.
• Labels data. Standard Data Types
Expression • primitive data types
• built in
• combination of one or more
• provided by compiler
constants, variables, operators, and
functions that the programming Function
language interprets
• Block of organized usable code.
• group of related statements that
Data Type perform a specific task.
• an attribute of data which tells the
compiler or interpreter how Object
the programmer intends to use
the data. • Basic building blocks.
• has state and behavior.
State
▪ characteristic of object
▪ fields, properties.
Behavior Constructor Method
• can be called methods, class
• special method that is used to
functions.
initialize a newly created object and
• actions you can do w/ objects.
is called just after the memory is
allocated for the object.
Declaration • If no user-defined constructor is
provided for a class, compiler
• statement that describes an
initializes member variables to its
identifier, such as the name of a
default values.
variable or a function.
Initialization (Galing kay Sir Ely)
• process of locating and using the Python
defined values for variable data that
• Portable, extendable, typeless
is used by a computer program
• import: Library
Class • database: sqlite
• GUI: Hard-coding
• Template or blueprint
• Keywords: input, print
Object Oriented Programming Language
Conditional Statements
• Program based on objects
• Elif: The elif keyword is pythons way
of saying "if the previous conditions
were not true, then try this
Instantiation
condition".
• Instantiating a class is creating a
eg:
copy of the class which inherits all
class variables and methods. a= 33
• To instantiate a class, simply call the b= 33
class as if it were a function, passing if b>a:
print("b is greater than a")
the arguments that the __init__
elif a==b:
method defines. The return value will
print("a and b are equal")
be the newly created object.
• eg: • Else: catches anything which isn't
caught by the preceding conditions.
class Foo():
def __init__(self,x,y): You can also have an else without
print x+y the elif
f = Foo(3,4)
eg:
a= 200
b= 33
if b> a: - The range() function defaults to
print("b is greater than a") increment the sequence by 1,
elif a == b: however it is possible to specify the
print("a and b are equal") increment value by adding a third
else: parameter: range(2, 30, 3):
print("a is greater than b")
Operators
• Arithmetic: +,-/,*,%,//,**
Looping • Relational: <,<=, >, >=, not=, ==
• Logical: not, and, or
Python has two primitive loop commands:
• While Loop: can execute a set of Clipboard
statements as long as a condition is
true. • is a buffer (data that is stored for a
short amount of time) that some
eg: operating systems provide for short
term storage and transfer between
i = 1 application programs.
while i < 6:
• Usually temporary and unnamed. Its
print(i)
i += 1 contents reside in the computer's
RAM.
• For Loop: used for iterating over a • Sometimes called paste buffer.
sequence (that is either a list, a
tuple, a dictionary, a set, or a string).
List Functions
1. Looping through a string example
(Print each fruit in a fruit list):: • len: returns length of object
• max/min: returns largest or smallest
fruits = ["apple", "banana", "cherry"]
for x in fruits: item
print(x) • lst.append: adds single element at
end of list
2. The range() Function: • lst.reverse: returns reverse list
- The range() function returns a
• list -> tuple to list
sequence of numbers, starting
from 0 by default, and increments list(tuple.name)
by 1 (by default), and ends at a t = {1,2,3}
specified number. (to change values of tuple)
- defaults to 0 as a starting value, list(listname) -> list to tuple
however it is possible to specify the
• lst.pop: removes element at given
starting value by adding a
parameter: range(2, 6), which index
means values from 2 to 6 (but not • lst.sort: to sort values
including 6): • lst.count(): counts how many (i)
• lst.extend: Add Elements of a List to
for x in range(2, 6):
print(x) Another List
• lst.remove : removes specific item
Object Oriented Features Child Class
• Module: allows to logically organize • To create a class that inherits the
the Python codes. functionality from another class,
send the parent class as a
- a file consisting & defining parameter when creating the child
functions, classes, objects, variables. class.
• Import statement: used to execute
• eg: Create a class named Student,
all the codes of the source file. which will inherit the properties
• from.....import statement: used to and methods from the Person
execute certain part of the source class:
file.
class Student(Person):
pass
(Pointers to review from other section) • use the pass keyword when you do
not want to add any other properties
INHERITANCE or methods to the class.
• allows us to define a class that super() function
inherits all the methods and
properties from another class.
• makes the child class inherit all the
methods and properties from its
• Parent class is the class being parent. Same with JAVA.
inherited from, also called base
class.
• This is useful if you want to retain
methods from parent class, but
• Child class is the class that
also want to add a new property in
inherits from another class, also
the child class.
called derived class.
• eg:(full program)
Parent Class
class Person:
class Person:
def __init__(self, fname, lname): def __init__(self, fname, lname):
self.firstname = fname self.firstname = fname
self.lastname = lname self.lastname = lname
def printname(self): def printname(self):
print(self.firstname, self.lastname print(self.firstname, self.lastname
)
)
#Use the Person class to create an
object, and then execute the printname
class Student(Person):
method:
def __init__(self, fname, lname,
x = Person("John", "Doe") year):
x.printname() super().__init__(fname, lname)
self.graduationyear = year
def welcome(self): Multilevel inheritance
print("Welcome", self.firstname,
self.lastname, "to the class of", • When we have child and grand child
self.graduationyear) relationship.
x = Student("Mike", "Olsen", 2019) eg:
x.welcome()
class Base(object):
Single Inheritance # Constructor
def __init__(self, name):
• When a child class inherits from only self.name = name
one parent class, it is called as
single inheritance. (Example above) # To get name
def getName(self):
Multiple Inheritance return self.name
• When a child class inherits from
# Inherited or Sub class (Note
multiple parent classes, it is called Person in bracket)
as multiple inheritance. class Child(Base):
• eg: # Constructor
def __init__(self, name, age):
class Base1(object): Base.__init__(self, name)
def __init__(self): self.age = age
self.str1 = "Geek1"
print "Base1" # To get name
def getAge(self):
class Base2(object): return self.age
def __init__(self):
self.str2 = "Geek2" # Inherited or Sub class (Note
print "Base2" Person in bracket)
class GrandChild(Child):
class Derived(Base1, Base2):
def __init__(self): # Constructor
def __init__(self, name, age,
# Calling constructors of address):
Base1 Child.__init__(self, name,
# and Base2 classes age)
Base1.__init__(self) self.address = address
Base2.__init__(self) # To get address
print "Derived" def getAddress(self):
return self.address
def printStrs(self):
print(self.str1, self.str2) # Driver code
g = GrandChild("Geek1", 23,
"Noida")
print(g.getName(), g.getAge(),
ob = Derived() g.getAddress())
ob.printStrs()
ENCAPSULATION • A double underscore: Private
• It is the restriction of access to variable, harder to access but
methods and variables. This can still possible.
prevent the data from being
modified by accident. Setters and Getters
• uses private and public
• similar to Java's concept of
variables/methods. But does not
encapsulation and uses setters
have private/public keyword like in
and getters.
Java
• Private variables are intended to be
• Other programming languages
changed using getter and setter
have protected class methods too,
methods. These provide indirect
but Python does not. access to them.
• eg: • eg:
• class Robot(object):
• class Robot(object):
def __init__(self):
def __init__(self):
self.a = 123
self.__version = 22
self._b = 123
self.__c = 123
def getVersion(self):
print(self.__version)
obj = Robot()
print(obj.a)
def setVersion(self,
print(obj._b)
version):
print(obj.__c)
self.__version =
version
• if you print the results, you will see
that . obj.__c will not be printed
because it is a private variable. obj = Robot()
• A variable is private if it has double obj.getVersion()
underscores before it. obj.setVersion(23)
• A single underscore: Private obj.getVersion()
variable, it should not be print(obj.__version)
accessed directly. But nothing
this will print out:
stops you from doing that
(except convention).
22
23
POLYMORPHISM
• The word polymorphism means LAMBDA FUNCTION
having many forms. In
programming, polymorphism means • A lambda function is a small
same function name (but different anonymous function.
signatures) being used for different
types. • A lambda function can take any
• Sometimes an object comes in number of arguments, but can
only have one expression.
many types or forms. If we have a
button, there are many different Syntax
draw outputs (round button, check
button, square button, button with lambda arguments : expression
image) but they do share the
same logic: onClick(). We access Examples
them using the same method
• Example: We create two • A lambda function that adds 10 to
the number passed in as an
classes: Bear and Dog, both can
argument, and print the result:
make a distinct sound. We then
make two instances and call their x = lambda a : a + 10
action using the same method. print(x(5))
class Bear(object):
Output: 15
def sound(self):
print("Groarrr")
• A lambda function that multiplies
class Dog(object):
def sound(self):
argument a with argument b and
print("Woof woof!") print the result:
def makeSound(animalType): x = lambda a, b : a * b
animalType.sound() print(x(5, 6))
bearObj = Bear() Output: 30
dogObj = Dog()
makeSound(bearObj)
makeSound(dogObj)
Output:
Groarrr
Woof woof!