Cara menggunakan python integer cube root

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.

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 −

  • Using a simple math equation.

  • Using math.pow() function.

  • Using cbrt() function in numpy.

Input Output Scenarios

Let 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 Equation

Let 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 Numbers

Given below is a python program that computes the cube root of a positive number.

Output

The output of the above python code is −

Cube root of 216 is 5.999999999999999

Example 2: For Negative Numbers

Given below is a python program that computes the cube root of a negative number.

Output

Cube root of -216 is -5.999999999999999

Using math.pow() Function

The 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 Numbers

In the python program below, we find the cube root of a positive input number

Output

The output achieved is −

Cube root of 64 is 3.9999999999999996

Example 2: For Negative Numbers

In the python program below, we find the cube root of a negative input number.

Output

The output achieved is −

Cube root of -64 is -3.9999999999999996

Using numpy’s cbrt() function

cbrt() 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.

Example

In 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.44225
26
Cubic root of 3 is 1.44225
27
Cubic root of 3 is 1.44225
28
Cubic root of 3 is 1.44225
29
Cubic root of 3 is 1.44225
30

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(**) function

Python3




Cubic root of 3 is 1.44225
31

Cubic root of 3 is 1.44225
16

# of a number using Binary Search2# Python 3 program to find cubic root7

Cubic root of 3 is 1.44225
19

Cubic root of 3 is 1.44225
20# Returns the absolute value of1
Cubic root of 3 is 1.44225
22
Cubic root of 3 is 1.44225
23
Cubic root of 3 is 1.44225
24
Cubic root of 3 is 1.44225
25
Cubic root of 3 is 1.44225
27
Cubic root of 3 is 1.44225
77
Cubic root of 3 is 1.44225
29
Cubic root of 3 is 1.44225
30

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
900
1 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
900
2, 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 Kalkulator

Mari kita coba beberapa perintah Python sederhana. Mulai interpreter dan tunggu prompt utama,

>>> width = 20
>>> height = 5 * 9
>>> width * height
900
1. (Seharusnya tidak butuh waktu lama.)

3.1.1. Angka

Interpreter 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
900
4,
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
5,
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
6 dan
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
7 berfungsi seperti di sebagian besar bahasa lain (misalnya, Pascal atau C); tanda kurung (
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
8) 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
900
9,
>>> n  # try to access an undefined variable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
0,
>>> n  # try to access an undefined variable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
1) 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
900
7) 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 defined
5 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 defined
6:

>>> 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 defined
7 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 defined
8) 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 defined
9. 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.0
3 atau
>>> 4 * 3.75 - 1
14.0
4 untuk menunjukkan bagian imajiner (mis.
>>> 4 * 3.75 - 1
14.0
5).

3.1.2. String

Selain 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.0
6) atau tanda kutip ganda (
>>> 4 * 3.75 - 1
14.0
7) dengan hasil yang sama .
>>> 4 * 3.75 - 1
14.0
8 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.0
8 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.06
1 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.6
0

There is one subtle aspect to raw strings: a raw string may not end in an odd number of

>>> 4 * 3.75 - 1
14.0
8 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.06
3 atau
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
4. Akhir baris secara otomatis termasuk dalam string, tetapi dimungkinkan untuk mencegahnya dengan menambahkan
>>> 4 * 3.75 - 1
14.0
8 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.6
1

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.6
2

String dapat digabungkan (direkatkan) dengan operator

>>> width = 20
>>> height = 5 * 9
>>> width * height
900
4, dan diulangi dengan
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
6:

>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5  # division always returns a floating point number
1.6
3

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.6
4

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.6
5

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.6
6

Jika Anda ingin menggabungkan variabel atau variabel dan literal, gunakan

>>> width = 20
>>> height = 5 * 9
>>> width * height
900
4:

>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5  # division always returns a floating point number
1.6
7

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.6
8

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.6
9

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
17
0

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
17
1

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.06
9 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
17
2

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
17
3

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
17
4

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
17
5

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
17
6

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
17
7

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
17
8

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 defined
6 dijelaskan secara lebih rinci di sini.

3.1.3. List

Python 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
17
9

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
128
0

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
128
1

List atau daftar juga mendukung operasi seperti perangkaian:

>>> 5 ** 2  # 5 squared
25
>>> 2 ** 7  # 2 to the power of 7
128
2

Tidak seperti string, yang , list adalah , mis. dimungkinkan untuk mengubah kontennya:

>>> 5 ** 2  # 5 squared
25
>>> 2 ** 7  # 2 to the power of 7
128
3

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
128
4

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
128
5

Fungsi bawaan juga berlaku untuk list:

>>> 5 ** 2  # 5 squared
25
>>> 2 ** 7  # 2 to the power of 7
128
6

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
128
7

3.2. Langkah Awal Menuju Pemrograman

Tentu 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
128
8

Contoh ini memperkenalkan beberapa fitur baru.

  • Baris pertama berisi multiple assignment: variabel

    >>> '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.'
    
    7 dan
    >>> '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.'
    
    8 secara bersamaan mendapatkan nilai-nilai baru 0 dan 1. Pada baris terakhir ini digunakan lagi, menunjukkan bahwa ekspresi di sisi sebelah kanan, semua dievaluasi terlebih dahulu sebelum salah satu pemberian nilai berlangsung. Ekspresi sisi kanan dievaluasi dari kiri ke kanan.

  • Perulangan dieksekusi selama kondisi (di sini:

    >>> '"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.
    
    0) masih benar. Dalam Python, seperti dalam C, nilai integer bukan nol bernilai benar; nol itu bernilai salah. Kondisi ini juga bisa berupa nilai string atau daftar, sebenarnya urutan apa pun; apapun dengan panjang yang tidak nol bernilai benar, urutan kosong bernilai salah. Tes yang digunakan dalam contoh adalah perbandingan sederhana. Operator perbandingan standar ditulis sama seperti dalam C:
    >>> '"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.
    
    1 (kurang dari),
    >>> '"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.
    
    2 (lebih besar dari),
    >>> '"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.
    
    3 (sama dengan),
    >>> '"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.
    
    4 ( kurang dari atau sama dengan),
    >>> '"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.
    
    5 (lebih besar atau sama dengan) dan
    >>> '"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.
    
    6 (tidak sama dengan).

  • body dari pengulangan adalah indentasi: indentasi adalah cara Python untuk pengelompokan pernyataan. Pada prompt interaktif, Anda harus mengetikkan tab atau spasi(-spasi) untuk setiap baris yang diberikan indentasi. Dalam praktiknya Anda akan menyiapkan masukan yang lebih rumit untuk Python dengan editor teks; semua editor teks yang baik memiliki fasilitas indentasi otomatis. Ketika pernyataan majemuk dimasukkan secara interaktif, harus diikuti oleh baris kosong untuk menunjukkan penyelesaian (karena pengurai tidak dapat menebak kapan Anda mengetik baris terakhir). Perhatikan bahwa setiap baris dalam blok dasar harus diindentasi dengan jumlah yang sama.

  • Fungsi menulis nilai argumen(-argumen) yang diberikan. Ini berbeda dari hanya menulis ekspresi yang ingin Anda tulis (seperti yang kami lakukan sebelumnya dalam contoh kalkulator) dalam cara menangani beberapa argumen, jumlah floating point, dan string. String dicetak tanpa tanda kutip, dan spasi dimasukkan di antara items, sehingga Anda dapat memformat sesuatu dengan baik, seperti ini:

    >>> 5 ** 2  # 5 squared
    25
    >>> 2 ** 7  # 2 to the power of 7
    128
    
    9

    Argumen kata kunci end dapat digunakan untuk menghindari baris baru setelah keluaran, atau mengakhiri keluaran dengan string yang berbeda:

    >>> width = 20
    >>> height = 5 * 9
    >>> width * height
    900
    
    0

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 defined
7 memiliki prioritas lebih tinggi dari
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
5,
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5  # division always returns a floating point number
1.6
00 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.6
01 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.6
02. 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.6
03, 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.6
04.

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.6
05 memiliki arti yang sama dengan kedua tanda kutip tunggal (
>>> 4 * 3.75 - 1
14.0
6) dan ganda (
>>> 4 * 3.75 - 1
14.0
7). 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.6
08 (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.6
09) dan sebaliknya.