Professional Documents
Culture Documents
Doc
Doc
In [2]: kurs=14500
def konversi(jumlah,mataUang1="IDR",mataUang2="USD"):
if (mataUang1=="IDR") & (mataUang2== "USD"):return jumlah/kurs
if (mataUang1=="USD") & (mataUang2== "IDR"):return jumlah*kurs
print("pilih menu: ")
print("1. USD->IDR")
print("2. IDR->USD")
x=int(input("pilihan menu: "))
y=float(input("jumlah uang: "))
if x==1:
print("hasil konversi : ",konversi(y,"USD","IDR"),"IDR")
elif x==2:konversi(y,"USD","IDR")
print("hasil konversi : ",konversi(y),"USD")
else:
print("pilihan tidak ada")
pilih menu:
1. USD->IDR
2. IDR->USD
pilihan menu: 2
jumlah uang: 50000
hasil konversi : 3.4482758620689653 USD
pi=200
print(math.pi)
print(numpy.pi)
print(scipy.pi)
print(pi)
3.141592653589793
3.141592653589793
3.141592653589793
200
for i in range(5):
print(random())
0.24516837795765656
0.11252806600161691
0.5644423091550225
0.042197256203403755
0.04727528370329481
1 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...
x = 1.4
y = 2.6
print(floor(x), floor(y))
print(floor(-x), floor(-y))
print(ceil(x), ceil(y))
print(ceil(-x), ceil(-y))
print(trunc(x), trunc(y))
print(trunc(-x), trunc(-y))
1 2
-2 -3
2 3
-1 -2
1 2
-1 -2
pi=200
print(math.pi)
print(numpy.pi)
print(scipy.pi)
print(pi)
3.141592653589793
3.141592653589793
3.141592653589793
200
print(firstNumber / secondNumber)
print("THE END.")
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-29-9049adf936eb> in <module>
3
4
----> 5 print(firstNumber / secondNumber)
6
7 print("This operation cannot be done.")
2 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...
# class attribute
merk = "avanza"
warna= "hitam"
top_speed=200
# instance attribute
# constructor
def __init__(self, pemilik, tahun):
self.pemilik = pemilik
self.tahun = tahun
# program utama
mobilku = mobil("doni", 2015)
mobilmu = mobil("budi", 2010)
# akses atribut
print("mobilku " + mobilku.merk)
print("mobilmu berwarna " + mobilmu.warna)
print("mobilku kecepatannya: "+str(mobilku.top_speed)+" km/h")
# akses method
print("mobil "+mobilku.pemilik + " dibeli tahun " + str(mobilku.tahun))
print("mobil "+mobilmu.pemilik + " dibeli tahun " + str(mobilmu.tahun))
mobilku avanza
mobilmu berwarna hitam
mobilku kecepatannya: 200 km/h
mobil doni dibeli tahun 2015
mobil budi dibeli tahun 2010
input: pemilik,tahun
3 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...
# class attribute
__merk = "avanza"
warna= "hitam"
top_speed=200
# instance attribute
# constructor
def __init__(self, pemilik, tahun):
self.pemilik = pemilik
self.tahun = tahun
def umur_mobil(self,tahun_sekarang):
c=tahun_sekarang-self.tahun
return c
mobilku
mobilmu berwarna hitam
mobilku kecepatannya: 200 km/h
mobil doni dibeli tahun 2015
mobil budi dibeli tahun 2010
umur mobil doni sudah 4 tahun
4 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...
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):
# call super() function
super().__init__()
print("Penguin is ready")
def whoisThis(self):
super().whoisThis()
print("Penguin")
def run(self):
print("Run faster")
peggy = Penguin()
peggy.whoisThis()
peggy.swim()
peggy.run()
Bird is ready
Penguin is ready
Bird
Penguin
Swim faster
Run faster
Buat sebuah class hewan, dan ada 3 jenis sub class, yaitu karnivora, herbivora, dan omnivora. Jalankan method eat,
jumlah_kaki,top_speed -ketika method eat pada karnivora maka akan muncul hasil “makan daging”, -ketika method eat
pada herbivora maka akan muncul “makan tumbuhan” -ketika method eat pada omnivora akan muncul “makan daging
dan tumbuhan”.
5 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...
# child class
class Karnivora(Hewan):
def __init__(self):
# call super() function
#super().__init__()
print("Ini hewan karnivora")
def eat(self):
print("makan daging")
def jumlah_kaki(self):
print("biasanya empat")
def top_speed(self):
print(200)
class Herbivora(Hewan):
def __init__(self):
# call super() function
#super().__init__()
print("Ini hewan herbivora")
def eat(self):
print("makan tumbuhan")
def jumlah_kaki(self):
print("kebanyakan empat")
def top_speed(self):
print(10)
class Omnivora(Hewan):
def __init__(self):
# call super() function
#super().__init__()
print("Ini hewan omnivora")
def eat(self):
print("makan daging dan tumbuhan")
def jumlah_kaki(self):
print("mungkin dua")
def top_speed(self):
print(100)
harimau = Karnivora()
sapi = Herbivora()
kera = Omnivora()
harimau.eat()
sapi.eat()
kera.eat()
sapi.top_speed()
macan.top_speed()
6 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-4-248fd27cc954> in <module>
53 kera.eat()
54 sapi.top_speed()
---> 55 macan.top_speed()
7 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...
# child class
class Mobil(Kendaraan):
def __init__(self):
print("Mobil")
class MPV(Mobil):
def __init__(self,merk,warna,kecepatan,harga_awal):
self.merk=merk
self.warna=warna
self.kecepatan=kecepatan
self.harga_awal=harga_awal
class SUV(Mobil):
def __init__(self,merk,warna,kecepatan,harga_awal):
self.merk=merk
self.warna=warna
self.kecepatan=kecepatan
self.harga_awal=harga_awal
class Truk(Kendaraan):
def __init__(self,merk,warna,kecepatan,harga_awal):
self.merk=merk
self.warna=warna
self.kecepatan=kecepatan
self.harga_awal=harga_awal
def harga_beli(self):
return "Harga beli Truk {} dengan warna {} adalah {} juta".format(self.m
erk, self.warna, self.harga_awal)
def harga_jual(self):
return "Harga jual Truk {} dengan warna {} adalah {} juta".format(self.m
erk, self.warna, self.harga_awal - 1)
class Motor(Kendaraan):
def __init__(self):
print("motor")
def harga_beli(self):
return "Harga beli motor {} dengan warna {} adalah {} juta".format(self.
merk, self.warna, self.harga_awal)
def harga_jual(self):
return "Harga jual motor {} dengan warna {} adalah {} juta".format(self.
merk, self.warna, self.harga_awal - 1)
class Manual(Motor):
def __init__(self,merk,warna,kecepatan,harga_awal):
self.merk=merk
self.warna=warna
self.kecepatan=kecepatan
self.harga_awal=harga_awal
class Matic(Motor):
def __init__(self,merk,warna,kecepatan,harga_awal):
self.merk=merk
self.warna=warna
self.kecepatan=kecepatan
self.harga_awal=harga_awal
avanza=MPV("toyota","hitam",250,210)
print(avanza.harga_beli())
print(avanza.harga_jual())
print(avanza.waktu_tempuh(200))
print();
d T k("hi " "k i " 200 500)
8 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...
Harga beli mobil toyota dengan warna hitam adalah 210 juta
Harga jual mobil toyota dengan warna hitam adalah 209 juta
toyota dapat menempuh jarak 200 dalam 0.8 jam
Harga beli Truk hino dengan warna kuning adalah 500 juta
Harga jual Truk hino dengan warna kuning adalah 499 juta
hino dapat menempuh jarak 200 dalam 1.0 jam
try:
cnt = 0
s = open('teks.txt', "rt")
ch = s.read(1)
while ch != '':
print(ch, end='')
cnt += 1
ch = s.read(1)
s.close()
print("\n\nCharacters in file:", cnt)
except IOError as e:
print("I/O error occurred: ", strerror(e.errno))
Characters in file: 15
[1, 2, 4, 8, 16]
1 4 16 64 256
miauwww
try:
cnt = 0
s = open('teks.txt', "rt")
ch = s.read(0)
print(ch)
while ch != '':
print(ch, end='')
cnt += 1
ch = s.read(1)
s.close()
print("\n\nCharacters in file:", cnt)
except IOError as e:
print("I/O error occurred: ", strerr(e.errno))
Characters in file: 0
9 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...
try:
cnt = 0
s = open('teks.txt', "rt")
content = s.read()
for ch in content:
print(ch, end='')
cnt += 1
ch = s.read(1)
s.close()
print("\n\nCharacters in file:", cnt)
except IOError as e:
print("I/O error occurred: ", strerr(e.errno))
try:
ccnt = lcnt = 0
s = open('newtext.txt', 'rt')
line = s.readline()
while line != '':
lcnt += 1
for ch in line:
print(ch, end='')
ccnt += 1
line = s.readline()
s.close()
print("\n\nCharacters in file:", ccnt)
print("Lines in file: ", lcnt)
except IOError as e:
print("I/O error occurred:", strerr(e.errno))
Characters in file: 38
Lines in file: 2
try:
fo = open('text.txt', 'wt') # a new file (newtext.txt) is created
#for i in range(10):
# s = "line #" + str(i+1) + "\n"
s="contoh penulisan di file\n"
for ch in s:
fo.write(ch)
s="beda tulisan\n"
for ch in s:
fo.write(ch)
fo.close()
except IOError as e:
print("I/O error occurred: ", strerr(e.errno))
10 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
x00\x00\x00\x00\x00\x00')
In [ ]:
11 of 11 11/07/2019, 16:50