-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter 3 - Operators.py
162 lines (122 loc) · 2.95 KB
/
Chapter 3 - Operators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# operasi aritmetika
result = 10 + 30
result = 40 - 10
result = 50 * 5
result = 16 / 4
result = 25 % 2
result = 5**3
# Plus-Equal Operator
counter = 0
counter += 10
# ini bentuk panjangnya
counter = 0
counter = counter + 10
# Modulo operations
# 8 bagi 4 hasilnya 2 sisa 0 jadi jawabannya 0
zero = 8 % 4
# 12 bagi 5 hasilnya 10 sisa 2 jadi jawabannya 2
nonzero = 12 % 5
# if Statement
test_value = 100
if test_value > 1:
# Expression evaluates to True
print("This code is executed!")
if test_value > 1000:
# Expression evaluates to False
print("This code is NOT executed!")
print("Program continues at this point.")
# elif Statement
pet_type = "fish"
if pet_type == "dog":
print("You have a dog.")
elif pet_type == "cat":
print("You have a cat.")
elif pet_type == "fish":
# this is performed
print("You have a fish")
else:
print("Not sure!")
# else Statement
test_value = 50
if test_value < 1:
print("Value is < 1")
else:
print("Value is >= 1")
# equal equal operator
test_string = "VALID"
if test_string == "NOT_VALID":
print("String equals NOT_VALID")
else:
print("String equals something else!")
# boolean operator
is_true = True
is_false = False
print(type(is_true))
# will output: <class 'bool'>
# comparison operators
a = 2
b = 3
a < b # evaluates to True
a > b # evaluates to False
a >= b # evaluates to False
a <= b # evaluates to True
nilai = 80
if nilai <= 80:
print(nilai, "Nilai Anda A")
elif nilai < 80:
print(nilai, "Nilai Anda B")
# Equal operator
if "Yes" == "Yes":
# evaluates to True
print("They are equal")
if (2 > 1) == (5 < 10):
# evaluates to True
print("Both expressions give the same result")
c = "2"
d = 2
if c == d:
print("They are equal")
else:
print("They are not equal")
# Not Equals Operator
if "Yes" != "No":
# evaluates to True
print("They are NOT equal")
val1 = 10
val2 = 20
if val1 != val2:
print("They are NOT equal")
if (10 > 1) != (10 > 1000):
# True != False
print("They are NOT equal")
# and Operator
True and True # Evaluates to True
True and False # Evaluates to False
False and False # Evaluates to False
1 == 1 and 1 < 2 # Evaluates to True
1 < 2 and 3 < 1 # Evaluates to False
"Yes" and 100 # Evaluates to True
# or Operator
True or True # Evaluates to True
True or False # Evaluates to True
False or False # Evaluates to False
1 < 2 or 3 < 1 # Evaluates to True
3 < 1 or 1 > 6 # Evaluates to False
1 == 1 or 1 < 2 # Evaluates to True
# not Operator
not True # Evaluates to False
not False # Evaluates to True
1 > 2 # Evaluates to False
not 1 > 2 # Evaluates to True
1 == 1 # Evaluates to True
not 1 == 1 # Evaluates to False
nilai_matkul_a = 80
nilai_matkul_b = 70
if nilai_matkul_a >= 80 and nilai_matkul_b >= 80:
print("Selamat Anda Lulus")
else:
print("Maaf Anda Tidak Lulus")
if nilai_matkul_a >= 80 or nilai_matkul_b >= 80:
print("Selamat Anda Lulus")
else:
print("Maaf Anda Tidak Lulus")