Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...

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

In [3]: import numpy


import math
import scipy

pi=200
print(math.pi)
print(numpy.pi)
print(scipy.pi)
print(pi)

3.141592653589793
3.141592653589793
3.141592653589793
200

In [4]: from random import random

for i in range(5):
print(random())

0.24516837795765656
0.11252806600161691
0.5644423091550225
0.042197256203403755
0.04727528370329481

In [5]: def aku(a=1,b=2):


return a*b
print(aku(2))

1 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...

In [11]: from math import ceil, floor, trunc

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

In [15]: import numpy


import math
import scipy

pi=200
print(math.pi)
print(numpy.pi)
print(scipy.pi)
print(pi)

3.141592653589793
3.141592653589793
3.141592653589793
200

In [29]: firstNumber = int(input("Enter the first number: "))


secondNumber = int(input("Enter the second number: "))

print(firstNumber / secondNumber)

print("This operation cannot be done.")

print("THE END.")

Enter the first number: 1


Enter the second number: 0

---------------------------------------------------------------------------
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.")

ZeroDivisionError: division by zero

2 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...

In [3]: class mobil:

# 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

buat class mobil atributnya: merk warna top speed

input: pemilik,tahun

buat method umur_mobil(tahun)

3 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...

In [102]: class mobil:

# 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

# instantiate the Parrot class


mobilku = mobil("doni", 2015)
mobilmu = mobil("budi", 2010)

# access the class attributes


print("mobilku ")
print("mobilmu berwarna " + mobilmu.warna)
print("mobilku kecepatannya: "+str(mobilku.top_speed)+" km/h")

# access the instance attributes


print("mobil "+mobilku.pemilik + " dibeli tahun " + str(mobilku.tahun))
print("mobil "+mobilmu.pemilik + " dibeli tahun " + str(mobilmu.tahun))
print("umur mobil "+mobilku.pemilik+" sudah "+str(mobilku.umur_mobil(2019))+" t
ahun")

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...

In [85]: # 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):
# 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...

In [4]: # parent class


class Hewan:
def __init__(self):
print("Ini kelas hewan")
def eat(self):
print("Makan")
def jumlah_kaki(self):
print("Swim faster")

# 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...

Ini hewan karnivora


Ini hewan herbivora
Ini hewan omnivora
makan daging
makan tumbuhan
makan daging dan tumbuhan
10

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-4-248fd27cc954> in <module>
53 kera.eat()
54 sapi.top_speed()
---> 55 macan.top_speed()

NameError: name 'macan' is not defined

buat class kendaraan yang terdiri dari

Mobil(punya sub MPV dan SUV)


Truk
Motor(punya 2 sub manual dan matic) masing-masing jenis kendaraan memiliki atribut input merk, warna, kecepatan
dan harga awal. Tambahkan method untuk dapat menghitung
harga beli(harga awal),
harga jual(harga awal – 1jt),
waktu tempuh(input method adalah jarak).

7 of 11 11/07/2019, 16:50
catatan2 http://localhost:8889/nbconvert/html/Desktop/catatan2.ipynb?downloa...

In [5]: class Kendaraan:


def __init__(self):
print("Kendaraan")
def harga_beli(self):
return "Harga beli mobil {} dengan warna {} adalah {} juta".format(self.
merk, self.warna, self.harga_awal)
def harga_jual(self):
return "Harga jual mobil {} dengan warna {} adalah {} juta".format(self.
merk, self.warna, self.harga_awal - 1)
def waktu_tempuh(self,jarak):
return "{} dapat menempuh jarak {} dalam {} jam".format(self.merk, jara
k, jarak/self.kecepatan)

# 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

Harga beli motor honda dengan warna merah adalah 14 juta


Harga jual motor honda dengan warna merah adalah 13 juta
honda dapat menempuh jarak 200 dalam 1.3333333333333333 jam

In [9]: from os import strerror

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))

miauwww wew wew

Characters in file: 15

In [126]: list1 = [x for x in range(5)]


list2 = list(map(lambda x: 2 ** x, list1))
print(list2)
for x in map(lambda x: x * x, list2):
print(x, end=' ')
print()

[1, 2, 4, 8, 16]
1 4 16 64 256

In [4]: stream = open("tulisan.txt", "rt", encoding = "utf-8")


print(stream.read())

miauwww

In [16]: from os import strerror

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...

In [132]: from os import strerror

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))

Beautiful is better than ugly.


Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

Characters in file: 131

In [150]: from os import strerror

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))

contoh penulisan di file


beda tulisan

Characters in file: 38
Lines in file: 2

In [11]: from os import strerror

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...

In [140]: data = bytearray(100)


print(data)

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

You might also like