Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

Variables and Objects on Python

Posted on June 21, 2012

In most programming languages, including C++ and Java, we must declare each variable, specifying its type, before it can be used. This is called static typing, because the compiler knows at compile time what type each variable is. Python, like most very high level languages, uses a different approach: Vari- ables have no type restrictions (dynamic typing), and they don’t need to be declared.

We could learn about Python’s variables and identifiers by creating and executing a file, as we did with hello.py in the preceding section. But for trying out small code snippets we don’t need to create a file at all. We can just enter the lines directly in the IDLE Python Shell window at the >>> prompt:

[code lang=”python”]
>>> x = 71
>>> y = "Dove"
[/code]

The whitespace around the assignment operator = is optional but is included because it makes the code easier to read. As a matter of style we will always put one space before and after binary operators. On the other hand, it is important that each statement occupies its own line and has no extraneous leading whitespace. This is because Python uses indentation and line breaks to signify its block structure, rather than the braces and semicolons used by many other programming languages.

Now we are ready to review what the two lines actually do. The first line creates an object of type int and binds the name x to it.★ The second line creates an object of type str (an 8-bit string type) and binds the name y to it.

Some Python programmers refer to names (such as the x and y used earlier), as object references since they refer to objects rather than being objects in their own right. For basic data types like int and str it makes no difference whether we see their variables as “objects” or as “object references”; they behave in the same way as they do in other programming languages:

[code lang=”python”]>>> x = 82
>>> x += 7
>>> x
89[/code]

Later on we will see cases where the fact that Python variables are object references makes a difference.

Python has two ways of comparing objects: by “identity” and by “value”. An object’s identity is effectively its address in memory, and this is what an object reference holds. If we use comparison operators, such as == and <, we get value comparison. For example, two strings are equal using == if they both contain the same text. If we use is we get identity comparison, which is fast because we are just comparing two addresses and don’t have to look at the objects themselves. An object’s identity can be obtained by calling id() on an object reference. Python has a special object called None. This can be assigned to any variable and it means that the variable has no value. There is only ever one instance of the None object, so we can always use the fast is and is not comparisons when testing for it. Notice that we wrote x on its own at the >>> prompt. If we write an expression or variable in IDLE, its value is automatically printed. In a program, we must use an explicit print statement to print an expression. For example:
[code lang=”python”]print x
[/code]

Python’s print statement is an operator, not a function, and for this reason it is invoked without using parentheses (just as we use + and other operators without them).

Earlier we said that Python uses dynamic typing. There are two factors involved in this. First, we can assign any object to any variable; for example, we could write:

[code lang=”python”]x = 47
x = "Heron"[/code]

After the first line x’s type is int, and after the second line x’s type is str, so clearly the type associated with the name x is determined by what the name is bound to, and not by any intrinsic property of its own. For this reason , we do not need to associate a particular type with a particular name.

The second aspect of Python’s dynamic typing is that the typing is strong: Python does not permit operations between incompatible types, as the following example, typed into IDLE, shows:

[code lang=”python”]>>> x = 41
>>> y = "Flamingo"
>>> x + y

Traceback (most recent call last):
File <pyshell#2>, line 1, in <module>
x + y
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’ [/code]

When we attempted to apply the binary + operator, Python raised a TypeError exception and refused to perform the operation.

If we were to assign to y a type compatible with x’s type, such as an int or float, the addition would work fine:

[code lang=”python”]>>> x = 41
>>> y = 8.5
>>> x + y
49.5[/code]

Although x and y are of different types (int and float), Python provides the same kind of automatic type promotion that other languages use, so the x is converted to a float and the calculation performed is actually 41.0 + 8.5.


Object references and objects

Assigning a value to a variable is called binding, since we bind names to objects. If we assign a new object to an existing variable, we are said to be rebinding the name. This is illustrated in Figure 1.2. When we do this, what happens to the object the name was originally bound to? For example:

[code lang=”python”]>>> x = "Sparrow"
>>> x = 9.8[/code]

What has happened to the str object that holds the text “Sparrow”? Once an object has no names bound to it, it is scheduled for garbage collection, and in due course it may be deleted from memory. This is very similar to how things work in Java.

Python variable names consist of ASCII letters, digits, and underscores (_). Variable names should begin with a letter, and they are case-sensitive (rowan, Rowan, and roWan are three different variables). No Python variable should be given the name of any of Python’s keywords (see Table 1.1), nor of Python’s built-in constants such as None, True, or False.

Terbaru

  • Masih Pusing Hitung Gaji Manual? Waktunya Pakai Aplikasi HR
  • Apa Itu Dustruco? Ini Pengertian dan Cara Pasangnya di HP Kalian
  • Apa Itu Aplikasi Dooie Live? Ini Pengertian dan Cara Pakai Aplikasinya untuk Cari Jodoh
  • Apa Itu Battle Emote Jefri Nichol dan Om Telolet Om di MLBB? Ini Penjelasannya
  • Apa itu Game Luna Mobile dan Bagaimana Cara Menangnya?
  • Apa Itu Kompensasi Sistem Trail Mobile Legends? Ini Penjelasan dan Cara Klaim Hadiahnya
  • Apa Itu Update Google Pixel 2 Desember 2025? Ini Penjelasannya!
  • Ini Cara Reset Desil di Aplikasi Cek Bansos Biar Valid (Update Januari 2026)
  • Apa Itu EZNET Wireless dan Fiber Optic? Ini Perbedaan dan Pengertian Lengkapnya
  • Pengertian Rework Magic Wheel dan Rank Mythic Eternal: Apa itu Perubahan Sistem Baru Mobile Legends?
  • Apa Itu Diamond Combo? Pengertian Game Puzzle Viral yang Katanya Bisa Hasilkan Cuan
  • Apa Itu Showbox? Pengertian, Fungsi, dan Cara Menggunakannya di Android
  • Cara Mengatasi Fitur Monet Facebook Pro Tiba-tiba Hilang
  • Google Bikin Kejutan! Pixel 10 Diskon Gila-gilaan di YouTube Premium
  • Apa Itu Google CC? Ini Pengertian Agen Produktivitas AI Eksperimental Terbaru
  • Apa Itu Ultras Seblak di eSport? Pengertian dan Fenomena Baru Suporter eSport
  • Android 16: Animasi Folder Baru yang Mengubah Cara Kita Berinteraksi!
  • Android 16: Notifikasi Lokasi ‘Blue Dot’ – Fitur Baru yang Perlu Kalian Ketahui!
  • Apa Itu Risiko Auto Click di Event Spongebob Mobile Legends? Ini Penjelasannya
  • Apa Itu Fitur Eksperimental Windows? Ini Pengertian dan Cara Menonaktifkannya
  • Apa Itu Android 16 Beta 1? Ini Pengertian dan Fitur Terbarunya
  • Belum Tahu? Ini Trik Supaya Bisa Dapat Skin Patrick Mobile Legends dengan Harga Murah
  • Pixel Desember 2025: Update Besar Siap Meluncur, Apa yang Baru?
  • Apa Itu HYFE XL Prioritas? Ini Pengertian, FUP, dan Realita Kecepatannya
  • Pengertian Render dan Convert: Apa Bedanya dalam Video Editing?
  • Cara Mengatasi Aplikasi Office yang Terus Muncul dan Menerapkan Perubahan Pengaturan Privasi
  • Pixel Launcher Mendapatkan Sentuhan Google Search Baru!
  • Penyebab Aplikasi Wondr BNI Tidak Bisa Dibuka
  • Kode 0425 Daerah Mana? Ini Pengertian dan Fakta Sebenarnya
  • Apa Itu SSS CapCut? Pengertian Downloader Video Tanpa Watermark yang Wajib Kalian Tahu
  • Apa Itu AI Kill Switch di Firefox? Ini Pengertian dan Detail Fitur Terbarunya
  • Apa Itu Platform Modular Intel Alder Lake N (N100)? Ini Pengertian dan Spesifikasinya
  • Apa Itu Armbian Imager? Pengertian Utilitas Flashing Resmi untuk Perangkat ARM Kalian
  • Apa Itu OpenShot 3.4? Pengertian dan Fitur LUT Terbaru untuk Grading Warna
  • Flatpak 1.16.2: Sandbox Baru untuk GPU Intel Xe dan VA-API
  • Apa Itu Elestio Get A Team? Ini Pengertian Karyawan AI Digital
  • Apa itu RunPod? Ini Pengertian dan Tutorial Cara Deploy Pod Pertamamu
  • Apa Itu Migrasi Pod di RunPod? Ini Pengertian dan Cara Kerjanya
  • Loading Model AI Lama? Coba Fitur Cached Models RunPod Ini, Hemat Waktu & Biaya!
  • Replicate Diakuisisi Cloudflare? Tenang, Ini Justru Kabar Baik Buat Developer AI
  • Apa Itu Kerentanan UEFI? Pengertian Celah Keamanan DMA pada Booting Awal
  • Apa Itu Serangan Siber Infrastruktur Kritikal? Belajar dari Kasus Peretasan Air Bersih Denmark
  • Apa Itu Error Update MSMQ di Windows 10? Ini Penjelasan Lengkap dan Solusinya
  • Apa Itu “I Am Not a Robot – reCAPTCHA Verification ID: 2165”? Ini Pengertian dan Bahayanya
  • Apa Itu Serangan Clop Ransomware pada CentreStack? Ini Pengertian dan Dampaknya
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ

©2025 emka.web.id | Design: Newspaperly WordPress Theme