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

  • Inilah Cara Main Aplikasi Layla Biar Dapat Teman dan Cuan Sekaligus!
  • Apa itu Apple Creator Studio?
  • Inilah Alasan Kenapa Tidak Bisa Melihat Status WA Padahal Tidak Diprivasi dan Trik Mengatasinya!
  • Lupa Email Akun Higgs Domino? Ini Cara Mengatasinya
  • Apa itu WhatsApp Aero? Aman atau Tidak + Cara Downloadnya
  • Inilah Kenapa Paket JNE Muncul Status Nobody At Home dan Cara Mengatasinya Biar Nggak Panik!
  • Gagal Aktivasi BSI Mobile? Inilah Arti Pesan Error 53 Saving Account Not Registered dan Solusinya
  • Cara Cuan dari Hobi Baca Novel/Komik Online
  • Hp Vivo Kalian Muncul Notif Data Spasial Sistem Rusak? Begini Trik Mengatasinya Sampai Tuntas!
  • Cara Buat Link Ujian Mencintai Diam-Diam Google Form, Tes Seberapa Besar Perasaan Kalian ke Crush!
  • Ini Penjelasan Mengenai Cara Mengubah Dosa Menjadi Diamond Game FF ML dan Saldo Shopeepay yang Sedang Viral
  • Trik Supaya Bisa Dapat Potongan Harga Rp100 di TikTok Tanpa Harus Reset HP!
  • Cara Input Bantuan IFP dan Laptop di Dapodik 2026.B, Aset Sekolah Aman
  • Cara Cairkan Rp170.000 dari Clear Blast, Terbukti Membayar ke DANA Tanpa Ribet!
  • Inilah Fakta Video Viral Arohi Mim 3 Menit 24 Detik yang Bikin Geger Netizen!
  • Inilah Sebenarnya Video Botol Aqua Viral yang Bikin Netizen TikTok Heboh dan Penasaran!
  • Takut Chat Hilang? Ini Cara Mudah Backup WA GB ke Google Drive yang Wajib Kalian Tahu!
  • Tutorial Ambil Barang Gratis di Akulaku, Modal Undang Teman Doang!
  • Ini Arti Kode Error FP26EV dan FP27EV di Aplikasi BriMO Serta Cara Mengatasinya!
  • Cara Jadi Clipper Modal Ngedit Video di TryBuzzer, Cocok Banget Buat Pemula!
  • Cara Pakai Beb6 Wifi Password Untuk Cek Jaringan di Sekitar Kalian
  • Inilah Review Lengkap Apakah Turbo VPN Extension Aman
  • Apa itu Toko Biru? Ini Istilah Olshop yang Perlu Kamu Tahu
  • Adswerve Inc Penipu? Ini Fakta di Balik Konsultan Digital Terkemuka Dunia!
  • Sering Ditelepon 08111? Simak Apakah Ini Penipuan atau Marketing Resmi Telkomsel
  • Inilah Cara Daftar dan Login Subsidi Tepat LPG 3 kg di Merchant Apps MyPertamina!
  • Akademi Crypto Penipu?
  • Belum Tahu Arti Paket Sedang Transit di TikTok Shop? Ternyata Ini Maksudnya Biar Kalian Nggak Panik!
  • Akun Paylater Tiba-tiba Diblokir? Jangan Panik, Gini Cara Mengatasinya Biar Lancar Jaya!
  • HP Vivo Sering Mati Sendiri Padahal Baterai Masih Banyak? Ini Penyebab dan Solusinya!
  • Apa itu Zoom Stealer? Ini Definisi dan Bahaya Tersembunyi di Balik Ekstensi Browser Kalian
  • Apa itu Skandal BlackCat Ransomware?
  • Grain DataLoader Python Library Explained for Beginners
  • Controlling Ansible with AI: The New MCP Server Explained for Beginners
  • Is Your Headset Safe? The Scary Truth Bluetooth Vulnerability WhisperPair
  • Perbedaan GPU vs TPU, Mana yang Terbaik
  • Tutorial Langfuse: Pantau & Optimasi Aplikasi LLM
  • Begini Teknik KV Caching dan Hemat Memori GPU saat Menjalankan LLM
  • Apa itu State Space Models (SSM) dalam AI?
  • Begini Cara Mencegah Output Agen AI Melenceng Menggunakan Task Guardrails di CrewAI
  • Apa itu Zoom Stealer? Ini Definisi dan Bahaya Tersembunyi di Balik Ekstensi Browser Kalian
  • Apa itu Skandal BlackCat Ransomware?
  • Apa itu ToneShell? Backdoor atau Malware Biasa?
  • Apa itu Parrot OS 7? Ini Review dan Update Terbesarnya
  • Clipper Malware? Ini Pengertian dan Bahaya yang Mengintai Kalian
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV

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