From f94a3811504981855a304d710122eac0839cbccb Mon Sep 17 00:00:00 2001 From: Prayogi Notonegoro <31283111+Prayoginotonegoro@users.noreply.github.com> Date: Fri, 8 Nov 2019 19:15:50 +0700 Subject: [PATCH 1/2] Update README.md --- README.md | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/README.md b/README.md index 0955397..139c13f 100644 --- a/README.md +++ b/README.md @@ -32,3 +32,114 @@ a = 'some text' # declare a string variable b = 123 # an integer c = True # a boolean ``` +### Input User +```python +input("Nama ku Adalah : ") +``` +###Looping For +```python +def Looping(): + for i in range (1,5): + print("nilai i : ", i) +testLooping() +``` +### Global Variabel +```python +globalVar = 5 +def testName(): + privateVar = 1 + globalVar = 2 + + print("Privat Variable : ", privateVar) + print("Global Variable : ", globalVar) + +testName() +print("Global Variable 2 : ", globalVar) +``` +### String Operator +```python +def testStringOperator(): + fullString = "this is full string" + tempString = "temp string" + upperString = "THIS IS UPPER" + lowerString = "this is lower" + + print(fullString + tempString) #concanation + print(fullString[3:7]) #print karakter antara kurang dari 3 dan kurang dari 7 + print(upperString.lower()) + print(lowerString.upper()) + print("len : ", len(fullString)) + print(fullString.strip("t")) + print(fullString.replace("full", "some")) + print(fullString.split("is")) + +testStringOperator() +``` +### While Looping +```python +def whileLoop(): + x = 1 + while x < 5: + print(x) + x+=1 +whileLoop() +``` +### Cara Membuat Fungsi pada Python +```python +def nama_fungsi(): + print "Hello ini Fungsi" +``` +### Membuat fungsi dengan parameter +```python +def luas_segitiga(alas, tinggi): + luas = (alas * tinggi) / 2 + print "Luas segitiga: %f" % luas; +luas_segitiga(4, 6) # Pemanggilan fungsi +``` +### fungsi untuk menampilkan semua data +```python +def show_data(): + if len(buku) <= 0: + print "BELUM ADA DATA" + else: + for indeks in range(len(buku)): + print "[%d] %s" % (indeks, buku[indeks]) +``` +### fungsi untuk menambah data +```python +def insert_data(): + buku_baru = raw_input("Judul Buku: ") + buku.append(buku_baru) +``` +### fungsi untuk edit data +```python +def edit_data(): + show_data() + indeks = input("Inputkan ID buku: ") + if(indeks > len(buku)): + print "ID salah" + else: + judul_baru = raw_input("Judul baru: ") + buku[indeks] = judul_baru +``` +### fungsi untuk menhapus data +```python +def delete_data(): + show_data() + indeks = input("Inputkan ID buku: ") + if(indeks > len(buku)): + print "ID salah" + else: + buku.remove(buku[indeks]) +``` +### Fungsi Membaca File Perbaris di python +```python +# buka file +file_puisi = open("puisi.txt", "r") + +# baca isi file +print file_puisi.readlines() + +# tutup file +file_puisi.close() +``` From 89d33244e9d281ec7d4f1dbc51d16b83a211be54 Mon Sep 17 00:00:00 2001 From: Prayogi Notonegoro <31283111+Prayoginotonegoro@users.noreply.github.com> Date: Wed, 20 Nov 2019 22:37:10 +0700 Subject: [PATCH 2/2] OOP_week2_Using Python --- encapsulation_oop.py | 26 ++++++++++++++++++++++++++ inheritance_oop.py | 36 ++++++++++++++++++++++++++++++++++++ method_oop.py | 27 +++++++++++++++++++++++++++ oop1.py | 29 +++++++++++++++++++++++++++++ overloading_oop.py | 21 +++++++++++++++++++++ polymorphism_oop.py | 28 ++++++++++++++++++++++++++++ 6 files changed, 167 insertions(+) create mode 100644 encapsulation_oop.py create mode 100644 inheritance_oop.py create mode 100644 method_oop.py create mode 100644 oop1.py create mode 100644 overloading_oop.py create mode 100644 polymorphism_oop.py diff --git a/encapsulation_oop.py b/encapsulation_oop.py new file mode 100644 index 0000000..1e25054 --- /dev/null +++ b/encapsulation_oop.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Nov 20 21:22:28 2019 + +@author: DELL +""" +class Computer: + def __init__(self): + self._maxprice=900 + + def sell(self): + print("Selling price: {}".format(self._maxprice)) + + def setMaxPrice(self,price): + self._maxprice=price + +c= Computer() +c.sell() + +# Change The Price +c._maxprice=1000 +c.sell() + +#Using Setter Function +c.setMaxPrice(1500) +c.sell() \ No newline at end of file diff --git a/inheritance_oop.py b/inheritance_oop.py new file mode 100644 index 0000000..2366135 --- /dev/null +++ b/inheritance_oop.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Nov 20 21:36:14 2019 + +@author: DELL +""" +#Parent class + +class bird: + def __init__(self): + print("Bird Is ready ") + + def whoisthis (self): + print("Bird") + + def swim(self): + print("Swim Faster") + +#Child Class + +class Penguin(bird): + def __init__ (self): + super(). __init__() + print("Penguin is Ready") + + def whoisthis(self): + print("Penguin") + + def run(self): + print("Run Faster") + +imam=Penguin() +imam.whoisthis() +imam.swim() +imam.run() + \ No newline at end of file diff --git a/method_oop.py b/method_oop.py new file mode 100644 index 0000000..cea0858 --- /dev/null +++ b/method_oop.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Nov 20 21:43:46 2019 + +@author: DELL +""" +class Employee: + 'common base class for all emplyes' + empCount =0 + + def __init__(self,name,salary): + self.name =name + self.salary=salary + Employee.empCount+=1 + + def displaycount(self): + print("Total Employee %d "%Employee.empCount) + + def displayemployee(self): + print("name :",self.name,",salary :",self.salary) + +emp1=Employee("meng",1000000) +emp=Employee("yogi",2000000) +emp.displaycount() +emp.displayemployee() +emp1.displayemployee() +Employee.empCount \ No newline at end of file diff --git a/oop1.py b/oop1.py new file mode 100644 index 0000000..1d19e96 --- /dev/null +++ b/oop1.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Nov 20 22:24:39 2019 + +@author: DELL +""" + +class Employee: + 'common base class for all emplyes' + empCount =0 + def Employee(self): + print("Total Employee %d"%Employee.empCount) + + def __init__(self,name,salary): + print("Constructor created") + self.name =name + self.salary=salary + Employee.empCount+=1 + + def displaycount(self): + print("Total Employee %d "%Employee.empCount) + + def displayemployee(self): + print("name :",self.name,",salary :",self.salary) +"This would create first object employee class" +emp1=Employee("zara",2000) +emp1.displayemployee() +"This would create second object employee class" +emp2=Employee("yogi",4000) \ No newline at end of file diff --git a/overloading_oop.py b/overloading_oop.py new file mode 100644 index 0000000..0e1071e --- /dev/null +++ b/overloading_oop.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Nov 20 22:04:01 2019 + +@author: DELL +""" + +class A: + def __init__ (self,a): + self.a =a + # adding two Object + def __add__(self,o): + return self.a + o.a + +ob1=A(1) +ob2=A(2) +ob3=A("geeks") +ob4=A("For") + +print(ob1 + ob2) +print(ob3 + ob4) \ No newline at end of file diff --git a/polymorphism_oop.py b/polymorphism_oop.py new file mode 100644 index 0000000..e915f76 --- /dev/null +++ b/polymorphism_oop.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Nov 20 22:11:36 2019 + +@author: DELL +""" + +class Parrot : + def fly(self): + print("Parrot can fly ") + def swim (self): + print("Parrot can't Swim") +class Penguin: + def fly(self): + print("Penguin can't fly") + def swim(self): + print("Penguin can swim") + +#Common Interface +def flying_test(bird): + bird.fly() + +#instance object +blu=Parrot() +peggy=Penguin() +#passing the object +flying_test(blu) +flying_test(peggy) \ No newline at end of file