Rabu, 25 Januari 2017

Latihan Fungsi 2

def cetak(kalimat,h, ops):
    print( "proses", ops, " kalimat ", kalimat, " adalah :",h)
   
def uppercase(kalimat,ops):
    str=kalimat.upper();
    cetak(kalimat,str,ops)

def lowercase(kalimat,ops):
    str=kalimat.lower();
    cetak(kalimat,str,ops)

def last(kalimat,ops):
    p=len(kalimat)
    str=kalimat[p-1];
    cetak(kalimat,str,ops)
   
def first(kalimat,ops):
    str=kalimat[0];
    cetak(kalimat,str,ops)

def replace(kalimat,ops):
    y=input("masukan huruf")
    z=input("masukan huruf")
    str=kalimat.replace(y,z);
    cetak(kalimat,str,ops)
   
def reverse(kalimat,ops):
    balikan = kalimat[::-1]
    cetak(kalimat,balikan,ops)

def indexof(kalimat,ops):
    str=kalimat.find('a');
    cetak(kalimat,str,ops)

def gabung(kalimat,ops):
    y=input("masukan kalimat")
    str= kalimat + ' ' + y;
    cetak(kalimat,str,ops)

def mulai():
    kalimat=str(input("masukan kalimat:"))
   

    print("Silakan pilih ops?")
    print("    1) uppercase")
    print("    2) lowercase")
    print("    3) last")
    print("    4) first")
    print("    5) replace")
    print("    6) reverse")
    print("    7) indexof")
    print("    8) gabung" )

    pil=int(input("masukan pilihan anda"))
    if pil==1:
       uppercase(kalimat,"uppercase")
    elif pil==2:
       lowercase(kalimat,"lowercase")
    elif pil==3:
       last(kalimat,"last")
    elif pil==4:
       first(kalimat,"first")
    elif pil==5:
       replace(kalimat,"replace")
    elif pil==6:
       reverse(kalimat,"reverse")
    elif pil==7:
       indexof(kalimat,"indexof")
    elif pil==8:
       gabung(kalimat, "gabung")
     
    else:
        print("tidak ada dalam opearasi")

    print("apakah anda ingin melanjutkan operasi?")
    print("    1) lanjut")
    print("    2) keluar")
    pil=int(input("Pilihan"))
   
    if pil==1:
        mulai()
 
    else:
        print("terimakasih")


print("*****************************************")
print("           Latihan Python 2              ")          
print("*****************************************")
mulai()

Latihan Fungsi 1



def penjumlahan(b1,b2, ops):
    h=b1 + b2
    cetak(b1,b2,h,ops)

def pengurangan(b1,b2, ops):
    h=b1 - b2
    cetak(b1,b2,h,ops)

def perkalian(b1,b2, ops):
    h=b1 * b2
    cetak(b1,b2,h,ops)
   
def pembagian(b1,b2, ops):
    h=b1 / b2
    cetak(b1,b2,h,ops)

def modus(b1,b2, ops):
    h=b1 % b2
    cetak(b1,b2,h,ops)

def kuadrat(b1,b2, ops):
    h=b1**b2
    cetak(b1,b2,h,ops)

def cetak(b1,b2,h, ops):
    print( "proses", ops, "bilangan", b1, "dengan", b2, "adalah", h)
   

def mulai():
    b1=int(input("masukan nilai1:"))
    b2=int(input("masukan nilai2:"))

    print("Silakan pilih ops?")
    print("    1) penjumlahan")
    print("    2) pengurangan")
    print("    3) perkalian")
    print("    4) pembagian")
    print("    5) modus")
    print("    6) kuadrat")
   
    pil=int(input(" Pilihan= "))
   
    if pil==1:
       penjumlahan(b1,b2,"penjumlahan")
    elif pil==2:
       pengurangan(b1,b2,"pengurangan")
    elif pil==3:
       perkalian(b1,b2,"perkalian")
    elif pil==4:
       pembagian(b1,b2,"pembagian")
    elif pil==5:
       modus(b1,b2,"modus")
    elif pil==6:
       kuadrat(b1,b2,"kuadrat")  
    else:
        print("tidak ada dalam opearasi")

    print("apakah anda ingin melanjutkan operasi?")
    print("    1) lanjut")
    print("    2) keluar")
    pil=int(input("Pilihan"))
   
    if pil==1:
        mulai()
 
    else:
        print("terimakasih")


print("*****************************************")
print("           Latihan Python 1              ")          
print("*****************************************")
mulai()


Rabu, 18 Januari 2017

Python in Range

0
1
2
3
4
------------------------
3
4
5
------------------------
4
6
8
------------------------
0
-2
-4
-6
-8
------------------------
>>>

Dengan code sbb:

for i in range(5):
    print(i)
print("------------------------")

for i in range(3, 6):
    print(i)
print("------------------------")

for i in range(4, 10, 2):
    print(i)
print("------------------------")

for i in range(0, -10, -2):
    print(i)
print("------------------------")




MEmbangun Array Python



import random

def loop(start, stop, step):
    i = start
    while i < stop:
        yield i
        i += step

def cetakm(nama,M):
    print("Array ",nama)
    b=len(M)
    c=len(M[0])
    for i in  loop(0,b,1):
        gab=""
        for j in  loop(0,c,1):
            gab+=str(M[i][j])+"\t"
        print(gab)
    print("++++++++++++++++++++++++++++")  

def penjumlahan(M1,M2,ops):
    print ("Operasi Penjumlahan")
    b=len(M)
    c=len(M[0])
    H=[]
    for i in  loop(0,b,1):
        T=[]
        for j in  loop(0,c,1):
            tmp=M1[i][j]+M2[i][j]
            T.append(tmp)
        H.append(T)
       
    cetakm("HasilM",H)


##################################################  
print("+++++++++++++++++++++++++++++++")  
print("+++ BELAJAR MATRIK LP2MARAY +++")  
print("+++++++++++++++++++++++++++++++")  

   
M=[]
N=[]



b=6
c=5

for i in loop(0,b,1):
    T1=[]
    T2=[]
    for j in loop(0,c,1):
        T1.append(random.randint(0,9))
        T2.append(random.randint(0,9))
    M.append(T1)
    N.append(T2)


b=len(M)
c=len(M[0])
print("b:",b)
print("c:",c)
   
cetakm("Matrix 1 ",M)
cetakm("Matrix 2 ",N)

H=M+N

penjumlahan(M,N,"Penjumlahan")