#9 Class Instance Python/

Python allow to object oriented programming (OOP). That is make a code writing so fast. Because object oriented programming reuse another code that containt formula and data.

As OOP, main emphasis of Python is on object. Another progamming paradigm, procedure oriented programming stress on function.

Object is set of data (variable) and method (functions) that can work on data. To create an object, programmer create a blueprint, first, then create object. The blueprint is named a class. Then we can make multiple object based on a classs. That is make coding faster, easier to write and read.

Class is reusable object as blue print that contain attribut and methods. A class is a description of an object’s characteristics.

Some book say that class is also an object. So, to prevent confusion, some writer give different name for object, it is instance. Instance is an object that is create based on class/ or bluprint.

Create Class

Usually, people called create class as class definiton. Inside the class, there are data attributes and procedures. 

The syntax of class definition is:

class MyClass:
    statement or instruction

That is formula to crate class. Example of simple class is :

class MyClass:
  x = 5
  def func(self):
      print (hello)

That example is a class that have a attribute x = 5 and also method or function. The function is func(). 

Parameter and argument

Class have (1) data or property /attribute and also (2) methods or function. Function have characteristics in python. It have parentheses (). Inside parentheses, there is parameters. Parameter are specified value after the function name. Parameter in inside the parentheses. We can give empty parameter, one, two and more. We can add as many parameters as you want. To create multiple parameter we can just separate them with a comma

Value of parameter is argument. If your parameter is “name”, the argument is “John”, “Mary” and so on.

We can also give default value parameter. For example,

def my_function(country= "United States"):
  print("I am from " + country)

my_function("Indonesia")
my_function("Australia")
my_function("Italia")
my_function()

If the argument of function is empty, It will show default argument or parameter value.

Create an instance

To reuse a class, we can create instance of class. To reuse a class, we can import the class first, if the class in different file. If a class in same file with an instance, import is not necessary.

import MyClass

newinstance = classname.function(parameter)
newinstance.function(parameter)

Each instance of a class has characteristics of class. Each instance have clone datas and methods of class. Instance also can modify data without destroy class. So, class can be starting point to create new instance. 

Why this blog talk about Python?

After reading Jamal Khashoggi case, I aware that economic and political ambition sometimes is blind to another consideration outside the ambition. The consideration includes a love of human and environment. Ambition makes human to be packman that cost on another human and environment.

That is not just coinsidence, but logical consequencies if they think that ambition is has to be reach at all cost.

So, I didn’t want to only talk about business in this blog. I want to talk about multiple beautiful things in our world. Not just business. I will to also to talk about programming that connects to the internet, I choose python. Python have capabilities to manage big data.

To Monitor Bot and Big Data

Why I talk about programming and big data. Big data and the bot can help the packman to manipulate and violate human and environment. So I need to monitor and update about big data and the bot. It help to monitor bad effect of big data.

To Reduce Excessive Ambition

The second point is it is good for the human to reduce the priority of one side ambition. Political and economic ambition only is one side ambition. I think it is good to find another simple pleasure. So we can recode one side ambition. Python is one of simple pleasure.

That is why I will talk about learning python and big data. Python is one of simple pleasure that I hope reduce excessive ambition.

To Handle Business Task

The third point is programming can help to handle the business task. Programming also helps us with getting information that matters. I have an idea to make an easier business task, for example: create a business plan, monitor opportunity, and threat.

Python is one of programming language on a computer. If we write in python language, we can give instruction to a computer. The computer can do our job, include big or repetitive jobs. Without programming language, the computer cannot obey our instruction. Python is one of programming language among another programming language like C+, Java, Visual Basic, R, Ruby and so on.

#7 Conditional Statement in Python

Why Auto driver that python makes can make an adjustment to an unpredictable situation. For example, if a car faces another stopped car in front of it, the auto driver can also stop accordingly. That is amazing.

Python can do that, because python have conditional statement or If statement.

The conditional statement is useful for creating a decision structure. Decision depends on condition. This allows a program to create more than one scenario. The if statement has an instruction for one ,, and another different instruction for another condition.

If statement have component, boolean and relational expression to check condition. Beside that, this if statement also have instruction to execute if condition as request.

Boolean and Relational Expression

A Boolean value has two value, True or False. Statement that use this value is Check Statement. Check Statement is statement that evaluate whether the statement true or false. In English language, is A is same with B? in Python language, A==B. faces statement tests an expression to determine whether it is true or false.
Boolean value is named after George Boole formulate some rules for reasoning about and combining this binary value (True and False). It is suit for computer that have binary value, 0 and 1.


We can get list of boolean value in Python by type : dir(True).

>>> dir(True)
       ['abs', 'add', 'and', 'class', 'cmp', 'coerce', 'delattr', 'div', 'divmod', 'doc', 'float', 'floordiv', 'format', 'getattribute', 'getnewargs', 'hash', 'hex', 'index',
       'init', 'int', 'invert', 'long', 'lshift', 'mod', 'mul', 'neg', 'new', 'nonzero', 'oct', 'or', 'pos', 'pow', 'radd', 'rand', 'rdiv', 'rdivmod', 'reduce', 'reduce_ ex', 'repr', 'rfloordiv', 'rlshift', 'rmod', 'rmul', 'ror', 'rpow', 'rrshift', 'rshift', 'rsub', 'rtruediv', 'rxor', 'setattr', 'sizeof', 'str', 'sub', 'subclasshook', '_
       truediv_', 'trunc', 'xor', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real'] "
      

That is a list of several function related to boolean type data.

Another alternative, we can read the documentation by type “help(True) ” or “help(False) in python command line.

>>> help(True)
       Help on bool object:class bool(int)
  |  bool(x) -> bool
  |
  |  Returns True when the argument x is true, False otherwise.
  |  The builtins True and False are the only two instances of the class bool.
  |  The class bool is a subclass of the class int, and cannot be subclassed.
  |
  |  Method resolution order:
  |      bool
  |      int
  |      object"
...
 l

Usually, Boolean operator use equal (==), greater(>) and less to determine whether a specific relationship True or False.

SymbolMeaning
> Greater than
>= Greater than or equal to
< Less than
<=Less than or equal to
== Equal to
!=Not equal to

Boolean Value in Conditional Expression

How boolean value in conditional expression. In Python language, the syntax of Conditional is like this

>>> If condition :
       statement

Condition in the syntax is boolean statement. That is something that computer can evaluate whether that is True or False. True or False in the term of meet condition. That is not about morality or epistemology.

In flowchart, Conditional Statement use symbol like this.

Logical Operator


Python also accommodate more complicated expression in boolean mode. There are logical operator to combine simpler boolean operator. The logical operator is” and”, “or” and “not”.

“and” Operator


The and and or combine two boolean expression. “And” operator evaluate expression as true only if the both expression is true. The truth table is like this.

XYX and Y
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

“or” Operator

or operator evaluate two statement. Then if one or more statement is true, the or operator return to True.

XYX or Y
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

“not” Operator

not operator can apply to one statement or more.
if the not operator apply to True value, it flowcharts to False.

Xnot X
TrueFalse
FalseTrue

Conditional Execution

A conditional statement has a ability to check condition and execute instruction or statement based on condition.
The syntax is like below.

>>> if True: # This is always True,
        pass # so this is always executed, but it does nothing
>>>  else:
        pass"

Chained conditionals/ nested decision

if we need more than two condition and more than two instruction, python also accommodate. Chained or nested decision structure check more than one condition.

Notation of chained conditional statement is like this.


That is the conditional statement inside another conditional statement.

# 6 Function in Python

Function is also the most used element in Python programming language. It is a sequence of statements. So, Function combine variable, data, and operator. The function is one of the most used element because it will reduce working time by reduce repeated statement and formula.

If we familiar about function in math and spreadsheet, you can understand easily.

The spreadsheet gives us a ready use function. For example average(A1:A10). This function will sum all numbers within A1:A10 cell, then divide it by count of numbers. Spreadsheet hide detail process of the average function. Only show name and its parameters. This will reduce complexity, and

In mathematics, a function show of how a varying quantity of variable depends on another quantity. … If the f is a name of function , the notation is y = f (x) (read f of x). Function will manipulate x based a formula.

Python has many functions. There are a built-in function in python, functions in modules, and we can make new Built-in.

Built-in Function

A built-in function is a function that Python already provides if we install it. We can see the built-in function here.

Then, to call built-in function is simple. Syntax of built-in function is functionname(parameter). Example, we use the sum function below.

>>> x =[2,4,5,6]
>>> sum(x)
17
>>> len(x)
4

len(parameter)” is also built-in function in python. It mean quantity of data series.

Function in Standard Library

Python also has a hidden function at modules in the standard library folder. We have to call its modules to use it. This is example to use random modules

>>> import random
>>> random.randint(0, 20)
7
>>> random.randint(500, 50000)
18601

We can read the notation is randit function of (0,20) in the ‘random’ modules. Random module have randint function, or random integer.

This is list of modules standard libray.

  • os
  • shutil
  • glob
  • sys
  • re
  • math
  • urllib2
  • smtplib
  • date
  • zlib
  • Timer
  • unittest

Third Party Library

As I talk in the introduction, Python have big community. They create tons of library and always growing. We can use their modules by install them previously. It is website that store library by community: https://pypi.org. To install them, we can go to command prompt or terminal.

pip install modulename

After install third party modules successfully, we can import to use it.

import modulename

modulename is name of modules that you want to install. Dont type module name. If you install BeautifulSoup modules, you can type pip install BeautifulSoup

pip install BeautifulSoup

User Defined Function

If we want to make our own function, we can make it in python.

Syntax to create our create from scratch function is:

def NAME( PARAMETERS ):
STATEMENTS

For example, if we want to create average function. We can make function like in code below.

>>>def average(xx):
       av = sum(xx)/len(xx)
       return av
y = [1,2,3,4,5]
print average(y)

That is example of user-defined function.