Sekarang kita belajar kondisi dimana kondisi ini digunakan untuk pengambilan keputusan. Selanjutnya kita mulai dengan “if statement ” oh iya kalo di python statement “else if” bisa kita singkat loh jadi “elif”. Sebelum mulai saya berikan sedikit tabel “conditional operators”.
- == equal to
- != not equal to
- < less than
- > greater than
- <= less than or equal to
- >= greater than or equal to
Code
x = 42
if x < 10:
print (“one digit number”)
elif x < 100:
print (“two digit number”)
else:
print (“big number”)
Output:
C:\Python33\Latihan>python ifa.py
two digit number
Code
z = 4
if z > 70:
print (“Something is very wrong”)
elif z < 7:
print (“This is normal”)
Output:
C:\Python33\Latihan>python ifb.py
This is normal
The complete “if” syntax
{conditions}:
{run this code}
elif {conditions}:
{run this code}
elif {conditions}:
{run this code}
else:
{run this code}
Jangan lupa untuk setiap akhir statement(‘if’, ‘elif’, ‘else’ or ‘while’) untuk memberikan tanda titik dua (:). Sekian saja dulu…