Mathematically, a cube root of a certain number is defined as a value obtained when the number is divided by itself thrice in a row. It is the reverse of a cube value. For example, the cube root of 216 is 6, as 6 × 6 × 6 = 216. Our task in this article is to find the cube root of a given number using python. Show
The cube root is represented using the symbol “$\mathrm{\sqrt[3]{a}}$”. The 3 in the symbol denotes that the value is divided thrice in order to achieve the cube root. There are various ways in python to calculate the cube root of a number. Let us look at them one by one below −
Input Output ScenariosLet us now look at some input output scenarios to calculate thee cube root of a given number − Assume the given input number is positive, the output is displayed as − Input: 8 Result: 2 Assume the given input is negative, the output is displayed as − Input: -8 Result: -2 Assume the input is a list of elements, the output is obtained as − Input: [8, -125] Result: [2, -5] Using Mathematical EquationLet us start simple; we use a simple mathematical equation to find the cube root of a number in python. In here, we find the $\mathrm{\frac{1rd}{3}}$. power of the input number. Example 1: For Positive NumbersGiven below is a python program that computes the cube root of a positive number. OutputThe output of the above python code is − Cube root of 216 is 5.999999999999999 Example 2: For Negative NumbersGiven below is a python program that computes the cube root of a negative number. OutputCube root of -216 is -5.999999999999999 Using math.pow() FunctionThe math.pow(x, y) function returns the value of x raised to the power y, with the condition of x being always a positive value. So in this case, we use this function to raise the input number to its $\mathrm{\frac{1rd}{3}}$. power. Example 1: For Positive NumbersIn the python program below, we find the cube root of a positive input number OutputThe output achieved is − Cube root of 64 is 3.9999999999999996 Example 2: For Negative NumbersIn the python program below, we find the cube root of a negative input number. OutputThe output achieved is − Cube root of -64 is -3.9999999999999996 Using numpy’s cbrt() functioncbrt() is a built-in function in the numpy library that returns the cube root of every element present in an array inputted. This method does not raise an error while finding the cube root of a negative number therefore making it more efficient than previous approaches. ExampleIn the python example below, we are taking inputs using python lists and using cbrt() function we find the cube root. Cubic root of 3 is 1.4422526 Cubic root of 3 is 1.4422527 Cubic root of 3 is 1.4422528 Cubic root of 3 is 1.4422529 Cubic root of 3 is 1.4422530 Output Cubic root of 3 is 1.44225 Time Complexity: O(logn) Auxiliary Space: O(1) Please refer complete article on Find cubic root of a number for more details! Method #2: Using power(**) functionPython3Cubic root of 3 is 1.4422531 Cubic root of 3 is 1.4422516
Cubic root of 3 is 1.4422519 Cubic root of 3 is 1.4422520 # Returns the absolute value of 1Cubic root of 3 is 1.4422522 Cubic root of 3 is 1.4422523 Cubic root of 3 is 1.4422524 Cubic root of 3 is 1.4422525 Cubic root of 3 is 1.4422527 Cubic root of 3 is 1.4422577 Cubic root of 3 is 1.4422529 Cubic root of 3 is 1.4422530 Dalam contoh berikut, masukan dan keluaran dibedakan dengan ada atau tidaknya prompt ( dan ): untuk mengulangi contoh, Anda harus mengetikkan semuanya setelah prompt, saat prompt muncul; baris yang tidak dimulai dengan prompt adalah output dari interpreter. Perhatikan bahwa baris yang hanya berisi prompt sekunder dalam contoh berarti Anda harus mengetikkan baris kosong; ini digunakan untuk mengakhiri perintah multi-baris. You can toggle the display of prompts and output by clicking on >>> width = 20 >>> height = 5 * 9 >>> width * height 9001 in the upper-right corner of an example box. If you hide the prompts and output for an example, then you can easily copy and paste the input lines into your interpreter. Banyak contoh dalam manual ini, bahkan yang dimasukkan pada prompt interaktif, termasuk komentar. Komentar dalam Python dimulai dengan karakter hash, >>> width = 20 >>> height = 5 * 9 >>> width * height 9002, dan diperluas hingga akhir garis fisik. Sebuah komentar dapat muncul di awal baris atau mengikuti spasi atau kode, tetapi tidak dalam string literal. Karakter hash dalam string literal hanyalah karakter hash. Karena komentar adalah untuk mengklarifikasi kode dan tidak ditafsirkan oleh Python, mereka dapat dihilangkan saat mengetikkan contoh. Beberapa contoh: # this is the first comment spam = 1 # and this is the second comment # ... and now a third! text = "# This is not a comment because it's inside quotes." 3.1. Menggunakan Python sebagai KalkulatorMari kita coba beberapa perintah Python sederhana. Mulai interpreter dan tunggu prompt utama, >>> width = 20 >>> height = 5 * 9 >>> width * height 9001. (Seharusnya tidak butuh waktu lama.) 3.1.1. AngkaInterpreter bertindak sebagai kalkulator sederhana: Anda dapat mengetikkan ekspresi padanya dan itu akan menulis nilainya. Sintaksis ekspresi mudah: operator >>> width = 20 >>> height = 5 * 9 >>> width * height 9004, >>> width = 20 >>> height = 5 * 9 >>> width * height 9005, >>> width = 20 >>> height = 5 * 9 >>> width * height 9006 dan >>> width = 20 >>> height = 5 * 9 >>> width * height 9007 berfungsi seperti di sebagian besar bahasa lain (misalnya, Pascal atau C); tanda kurung ( >>> width = 20 >>> height = 5 * 9 >>> width * height 9008) dapat digunakan untuk pengelompokan. Sebagai contoh: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.6 Bilangan bulat (mis. >>> width = 20 >>> height = 5 * 9 >>> width * height 9009, >>> n # try to access an undefined variable Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined0, >>> n # try to access an undefined variable Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined1) memiliki tipe , yang memiliki bagian pecahan (mis.``5.0``,``1.6``) memiliki tipe . Kita akan melihat lebih banyak tentang tipe bilangan nanti dalam tutorial. Division ( >>> width = 20 >>> height = 5 * 9 >>> width * height 9007) always returns a float. To do and get an integer result you can use the >>> n # try to access an undefined variable Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined5 operator; to calculate the remainder you can use >>> n # try to access an undefined variable Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined6: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 17 Dengan Python, dimungkinkan untuk menggunakan operator >>> n # try to access an undefined variable Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined7 untuk menghitung pemangkatan : >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 128 Tanda sama dengan ( >>> n # try to access an undefined variable Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined8) digunakan untuk memberikan nilai ke variabel. Setelah itu, tidak ada hasil yang ditampilkan sebelum prompt interaktif berikutnya: >>> width = 20 >>> height = 5 * 9 >>> width * height 900 Jika variabel tidak "didefinisikan" (diberi nilai), mencoba menggunakannya akan menghasilkan kesalahan: >>> n # try to access an undefined variable Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined Ada dukungan penuh untuk floating point; operator dengan operan tipe campuran akan mengubah operan integer ke floating point: >>> 4 * 3.75 - 1 14.0 Dalam mode interaktif, ekspresi cetak terakhir diberikan ke variabel >>> n # try to access an undefined variable Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined9. Ini berarti bahwa ketika Anda menggunakan Python sebagai kalkulator meja, agak lebih mudah untuk melanjutkan perhitungan, misalnya: >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.06 Variabel ini harus diperlakukan sebagai baca-saja oleh pengguna. Jangan secara eksplisit memberikan nilai padanya --- Anda akan membuat variabel lokal independen dengan nama yang sama menutupi variabel bawaan dengan perilaku saktinya. Selain dan :class:` float`, Python mendukung tipe angka lainnya, seperti dan . Python juga memiliki dukungan bawaan untuk , dan menggunakan akhiran >>> 4 * 3.75 - 1 14.03 atau >>> 4 * 3.75 - 1 14.04 untuk menunjukkan bagian imajiner (mis. >>> 4 * 3.75 - 1 14.05). 3.1.2. StringSelain angka, Python juga dapat memanipulasi string atau teks, yang dapat diekspresikan dalam beberapa cara. Mereka dapat disertakan dalam tanda kutip tunggal ( >>> 4 * 3.75 - 1 14.06) atau tanda kutip ganda ( >>> 4 * 3.75 - 1 14.07) dengan hasil yang sama . >>> 4 * 3.75 - 1 14.08 dapat digunakan untuk keluar dari kutipan: >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.' Dalam interpreter interaktif, string keluaran diapit dengan tanda kutip dan karakter khusus dipisahkan dengan garis miring terbalik. Meskipun ini kadang-kadang terlihat berbeda dari input (tanda kutip terlampir dapat berubah), kedua string tersebut setara. String disertakan dalam tanda kutip ganda jika string berisi kutipan tunggal dan tidak ada tanda kutip ganda, jika tidak maka akan dilampirkan dalam tanda kutip tunggal. Fungsi menghasilkan keluaran yang lebih mudah dibaca, dengan menghilangkan tanda kutip terlampir dan dengan mencetak karakter yang dipisahkan dan spesial: >>> '"Isn\'t," they said.' '"Isn\'t," they said.' >>> print('"Isn\'t," they said.') "Isn't," they said. >>> s = 'First line.\nSecond line.' # \n means newline >>> s # without print(), \n is included in the output 'First line.\nSecond line.' >>> print(s) # with print(), \n produces a new line First line. Second line. Jika Anda tidak ingin karakter yang diawali dengan >>> 4 * 3.75 - 1 14.08 ditafsirkan sebagai karakter khusus, Anda dapat menggunakan raw strings dengan menambahkan >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.061 sebelum kutipan pertama: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.60 There is one subtle aspect to raw strings: a raw string may not end in an odd number of >>> 4 * 3.75 - 1 14.08 characters; see for more information and workarounds. String literal dapat melebar hingga beberapa baris. Salah satu caranya adalah dengan menggunakan tanda kutip tiga: >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.063 atau >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.064. Akhir baris secara otomatis termasuk dalam string, tetapi dimungkinkan untuk mencegahnya dengan menambahkan >>> 4 * 3.75 - 1 14.08 di akhir baris. Contoh berikut: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.61 menghasilkan keluaran berikut (perhatikan bahwa awal baris baru tidak termasuk): >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.62 String dapat digabungkan (direkatkan) dengan operator >>> width = 20 >>> height = 5 * 9 >>> width * height 9004, dan diulangi dengan >>> width = 20 >>> height = 5 * 9 >>> width * height 9006: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.63 Dua atau lebih string literals (yaitu yang terlampir di antara tanda kutip) di sebelah satu sama lain secara otomatis digabungkan. >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.64 Fitur ini sangat berguna ketika Anda ingin memecah string panjang: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.65 Ini hanya bekerja dengan dua literal, tidak dengan variabel atau ekspresi: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.66 Jika Anda ingin menggabungkan variabel atau variabel dan literal, gunakan >>> width = 20 >>> height = 5 * 9 >>> width * height 9004: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.67 String dapat diindeks atau indexed (disandikan), dengan karakter pertama memiliki indeks 0. Tidak ada tipe karakter yang terpisah; sebuah karakter hanyalah sebuah string berukuran satu: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.68 Indeks juga bisa berupa angka negatif, untuk mulai menghitung dari kanan: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.69 Perhatikan bahwa karena -0 sama dengan 0, indeks negatif mulai dari -1. Selain pengindeksan, slicing atau mengiris juga didukung. Sementara pengindeksan digunakan untuk mendapatkan karakter individual, slicing memungkinkan Anda untuk mendapatkan substring: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 170 Indeks irisan memiliki nilai bawaan yang berguna; indeks pertama yang hilang akan digantikan ke nilai nol, indeks kedua yang hilang akan digantikan ke nilai ukuran atau panjang string yang diiris. >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 171 Perhatikan bagaimana awal selalu disertakan, dan akhirnya selalu dikecualikan. Ini memastikan bahwa >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.069 selalu sama dengan >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.'0: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 172 Salah satu cara untuk mengingat bagaimana irisan bekerja adalah dengan menganggap indeks sebagai menunjuk between karakter, dengan tepi kiri karakter pertama bernomor 0. Kemudian tepi kanan karakter terakhir dari string n karakter memiliki indeks n, misalnya: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 173 Baris pertama angka memberikan posisi indeks 0...6 dalam string; baris kedua memberikan indeks negatif yang sesuai. Irisan dari i ke j terdiri dari semua karakter di antara kedua sisi yang berlabel i dan j. Untuk indeks non-negatif, panjang irisan adalah selisih indeks, jika keduanya berada dalam batas. Misalnya, panjang >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.'1 adalah 2. Mencoba menggunakan indeks yang terlalu besar akan menghasilkan kesalahan: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 174 Namun, indeks irisan di luar jangkauan ditangani dengan anggun ketika digunakan untuk mengiris: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 175 String python tidak dapat diubah --- mereka adalah . Oleh karena itu, menetapkan ke suatu indeks posisi dalam string menghasilkan kesalahan: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 176 Jika Anda membutuhkan string yang berbeda, Anda harus membuat yang baru: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 177 Fungsi bawaan mengembalikan panjang string: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 178 Lihat juga String adalah contoh sequence types atau jenis urutan, dan mendukung operasi umum yang didukung oleh jenis tersebut. String mendukung sejumlah besar metode untuk transformasi dasar dan pencarian. String literal yang memiliki ekspresi yang tersemat. Informasi tentang pemformatan string dengan . Operasi pemformatan lama dipanggil ketika string adalah operan kiri dari operator >>> n # try to access an undefined variable Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined6 dijelaskan secara lebih rinci di sini. 3.1.3. ListPython mengetahui sejumlah tipe data compound atau gabungan, yang digunakan untuk mengelompokkan nilai-nilai lainnya. Yang paling serbaguna adalah list, yang dapat ditulis sebagai daftar nilai yang dipisahkan koma (items) antara tanda kurung siku. List atau daftar mungkin berisi items dari tipe yang berbeda, tetapi biasanya semua items memiliki tipe yang sama. >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 179 Seperti string (dan semua bawaan lainnya tipe ), list atau daftar tersebut dapat diindeks dan diiris: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1280 Semua operasi iris mengembalikan list atau daftar baru yang berisi elemen yang diminta. Ini berarti bahwa irisan berikut mengembalikan dari list: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1281 List atau daftar juga mendukung operasi seperti perangkaian: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1282 Tidak seperti string, yang , list adalah , mis. dimungkinkan untuk mengubah kontennya: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1283 Anda juga dapat menambahkan items baru di akhir list, dengan menggunakan method >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.'5 (kita akan melihat lebih banyak tentang metode nanti): >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1284 Pemberian nilai untuk irisan juga dimungkinkan, dan ini bahkan dapat mengubah ukuran list atau menghapus seluruhnya: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1285 Fungsi bawaan juga berlaku untuk list: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1286 Dimungkinkan untuk membuat list atau daftar bersarang (membuat daftar yang berisi daftar lain), misalnya: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1287 3.2. Langkah Awal Menuju PemrogramanTentu saja, kita bisa menggunakan Python untuk tugas yang lebih rumit daripada menambahkan dua dan dua bersamaan. Sebagai contoh, kita dapat menulis awal dari sub-urutan Fibonacci series sebagai berikut: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1288 Contoh ini memperkenalkan beberapa fitur baru.
Catatan kaki Karena >>> n # try to access an undefined variable Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined7 memiliki prioritas lebih tinggi dari >>> width = 20 >>> height = 5 * 9 >>> width * height 9005, >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.600 akan ditafsirkan sebagai >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.601 dan karenanya menghasilkan >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.602. Untuk menghindari ini dan mendapatkan >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.603, Anda dapat menggunakan >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.604. Tidak seperti bahasa lain, karakter khusus seperti >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.605 memiliki arti yang sama dengan kedua tanda kutip tunggal ( >>> 4 * 3.75 - 1 14.06) dan ganda ( >>> 4 * 3.75 - 1 14.07). Satu-satunya perbedaan antara keduanya adalah bahwa dalam tanda kutip tunggal Anda tidak perlu memisahkan >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.608 (tetapi Anda harus memisahkan >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.609) dan sebaliknya. |