Created
September 7, 2012 08:38
-
-
Save fajran/3664394 to your computer and use it in GitHub Desktop.
Pertemuan 1 - Python - UVT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.swp | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print "hore hore hore" | |
print 1 + 2 | |
print 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = 10 | |
b = 20 | |
c = a + b | |
print c | |
# Operator lain: | |
# + - * / | |
# % | |
# // |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nama = "iang" | |
tanggal = 29 | |
print "nama saya adalah", nama, "saya lahir pada tanggal", tanggal | |
# print "pada tanggal", tanggal, ",", nama, "lahir" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: | |
# <= >= != | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Nilai "tidak ada" | |
nama_kakak = None | |
anak_pertama = nama_kakak is None | |
punya_kakak = nama_kakak is not None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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