Variables, constants & data types · Variabel, konstanta & tipe data
A variable is a named box
- A variable is a name that stores a value.
- You put a value in with
=— this is called assignment. - Use a clear name so the program is easy to read.
Variabel adalah kotak bernama
- Variabel adalah nama yang menyimpan nilai.
- Anda memasukkan nilai dengan
=— ini disebut penugasan. - Gunakan nama yang jelas agar program mudah dibaca.
age = 15
price = 2.50
name = "Ada"
print(age, price, name)
Variables as named boxes · Variabel sebagai kotak bernama
Each = puts a value into a named box you can use later. · Setiap = memasukkan nilai ke dalam kotak bernama yang bisa Anda gunakan nanti.
The five data types
- IGCSE uses five basic data types:
- integer (whole number) → Python
int; real (decimal) →float. - char (one letter), string (text) →
str; Boolean (True/False) →bool.
Lima tipe data
- IGCSE menggunakan lima tipe data dasar:
- integer (bilangan bulat) → Python
int; real (desimal) →float. - char (satu huruf), string (teks) →
str; Boolean (True/False) →bool.
score = 42 # integer
height = 1.75 # real
grade = "A" # char
town = "Cairo" # string
passed = True # Boolean
Constants stay the same
- A constant is a value that never changes while the program runs.
- Python has no special keyword, so we write constants in
CAPITALS. - This warns other readers: do not change me.
Konstanta tetap sama
- Konstanta adalah nilai yang tidak berubah selama program berjalan.
- Python tidak memiliki kata kunci khusus, jadi kami menulis konstanta dalam
CAPITALS. - Ini memperingatkan pembaca lain: jangan ubah saya.
PI = 3.14159
VAT_RATE = 0.20
print(PI)
Changing between types
input()gives a string, so convert it when you need a number.int(x)makes a whole number;float(x)a decimal;str(x)makes text.
Mengubah antara tipe
input()memberikan string, jadi konversikan saat Anda membutuhkan angka.int(x)membuat bilangan bulat;float(x)desimal;str(x)membuat teks.
n = int("5")
print(n + 1) # 6
print("Total: " + str(n)) # Total: 5
In the exam: DECLARE and CONSTANT
- Pseudocode names a type with
DECLARE, and fixes a value withCONSTANT.
Dalam ujian: DECLARE dan CONSTANT
- Pseudocode menamai tipe dengan
DECLARE, dan menetapkan nilai denganCONSTANT.
DECLARE Age : INTEGER
Age ← 15
CONSTANT Pi ← 3.14
Common mistakes
- Give a variable a value before you use it.
- A constant is a value you do not change — name it clearly.
- Keep the right type: numbers for maths, strings for text.
Kesalahan umum
- Berikan nilai pada variabel sebelum menggunakannya.
- Konstanta adalah nilai yang tidak Anda ubah — beri nama dengan jelas.
- Jaga tipe yang benar: angka untuk matematika, string untuk teks.
Now you try
- Make variables and do a little maths with them.
- Press Check answer to test your code.
Sekarang Anda coba
- Buat variabel dan lakukan sedikit matematika dengannya.
- Tekan Periksa jawaban untuk menguji kode Anda.
The variables width and height are set for you. Work out the rectangle's area (width × height) and store it in a variable called area. · Variabel width dan height diatur untuk Anda. Hitung luas persegi panjang (lebar × tinggi) dan simpan dalam variabel bernama area.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
price is a real number and quantity is an integer. Work out the total cost (price × quantity). · price adalah bilangan real dan quantity adalah integer. Hitung biaya total (harga × jumlah).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Read a whole number with input(), convert it with int(...), then print the number plus 3. For input 7, the output should be 10. · Baca bilangan bulat dengan input(), konversikan dengan int(...), lalu cetak bilangan ditambah 3. Untuk input 7, output seharusnya 10.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.