Skip to content

Instantly share code, notes, and snippets.

@fajran fajran/.gitignore
Created Sep 7, 2012

Embed
What would you like to do?
Pertemuan 1 - Python - UVT
print "hore hore hore"
print 1 + 2
print 3
a = 10
b = 20
c = a + b
print c
# Operator lain:
# + - * /
# %
# //
nama = "iang"
tanggal = 29
print "nama saya adalah", nama, "saya lahir pada tanggal", tanggal
# print "pada tanggal", tanggal, ",", nama, "lahir"
# referensi: http://docs.python.org/library/stdtypes.html#string-formatting-operations
tempat = "Jakarta"
nama = "Iang"
tanggal = 29
print "nama saya: %s" % nama
kalimat = "Pada tanggal %d, %s lahir di %s" % (tanggal, nama, tempat)
print kalimat
# cek tipe data: type()
angka = 100
teks = "seratus"
teks2 = "100"
desimal = 4.5
benarsalah = True
print type(angka)
print type(teks)
print type(teks2)
print type(desimal)
print type(benarsalah)
# konversi tipe
teks = "100"
angka = int(teks)
print type(teks)
print type(angka)
# coba juga:
# float("2.4")
# str(234)
# str(1.23)
# bool("True")
# bool("False")
# bool("")
# bool(5)
# Operasi matematika
a = 1
b = 2
c = 1.0
d = 2.0
print a + b
print a - b
print a + d
print a * b
print b * d
print a / b
print a / d
print 10 % 3
print 10 / 3
# evaluasi nilai benar/salah
a = 1
b = 2
c = 3
lebih_kecil = a < b
sama = a == b
lebih_besar = a > b
tengah = a < b < c
print "Lebih kecil?", lebih_kecil
print "Sama?", sama
print "Lebih besar?", lebih_besar
print "Tengah?", tengah
print "a genap?", a % 2 == 0
print "b genap?", b % 2 == 0
print "a ganjil?", a % 2 != 0
print "b ganjil?", b % 2 != 0
# operator lain:
# <= >= !=
# Konjungsi (and)
a = 10
b = 20
c = 30
a_paling_kecil = a < b and a < c
a_paling_besar = a > b and a > c
# referensi: http://learnpythonthehardway.org/book/ex27.html
# Disjungsi (or)
punya_kakak = True
punya_adik = False
punya_saudara = punya_kakak or punya_adik
anak_tunggal = not punya_kakak and not punya_adik
# referensi: http://learnpythonthehardway.org/book/ex27.html
# Nilai "tidak ada"
nama_kakak = None
anak_pertama = nama_kakak is None
punya_kakak = nama_kakak is not None
# percabangan: if-then-else
angka = raw_input("masukkan angka: ")
angka = int(angka)
if angka % 2 == 0:
print "Angka", angka, "adalah genap"
else:
print "Angka", angka, "adalah ganjil"
# percabangan II
nilai = raw_input("masukkan nilai 1-10: ")
nilai = int(nilai)
if nilai <= 0:
print "Terlalu kecil"
elif nilai > 10:
print "Terlalu besar"
else:
if nilai % 2 == 0:
print "genap"
else:
print "ganjil"
# fungsi
a = 10
if a % 2 == 0:
print "genap"
else:
print "ganjil"
b = 20
if b % 2 == 0:
print "genap"
else:
print "ganjil"
def cetak_ganjil_genap(angka):
if angka % 2 == 0:
print "genap"
else:
print "ganjil"
cetak_ganjil_genap(10)
cetak_ganjil_genap(3)
cetak_ganjil_genap(7)
cetak_ganjil_genap(4)
# return
def jumlah_detik(detik, menit=0, jam=0, hari=0):
return detik + menit * 60 + jam * 3600 + hari * 86400
print "Durasi:", jumlah_detik(45, 50, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.