Monday, February 26, 2024

Python Tutorial - If Statement - 003

 Sekarang, kita bicara tentang "if statement" dalam bahasa Python. Seperti biasa, kita akan mulai dengan contoh. 


Image 01 - Aneka lilin.

Contoh pertama, menggunakan variable x:
x = 3
if x > 10:
print("x smaller than 10")
else:
print("x is bigger than 10 or equal")
output: x is bigger than 10 or equal


Contoh kedua: Variables dalam Python tidak mesti "hardcode,"
usia = 25
print("Tamu usia saya, anda punya 1 kesempatan!")
tamu = int(raw_input("Tamu:))

if Tamu != age:
print("Wrong!")
else:
print("Correct")

Output: "Correct"


Image 02 - Singapura dari laut.

Operator Descriptions:
!= not equal
== equals
greater than
smaller than

It’s essential to understand the difference between the assignment operator (=) and the equals operator (==).

Jika multiple conditions, beberapa "if statements" bisa dipakai:
a = 13
b = 34
if a > 11:
if b > 21:
print("Good")
Output: Good

More combine conditions:
guess = 25
if guess > 11 and guess < 21:
print("in range")
else:
print("out of range")
Output: out of range

# Posting sebelumnya:

No comments:

Post a Comment