Tutorial Python untuk bukan pengatur cara/Hello, World: Perbezaan antara semakan

Daripada Wikibooks
Kandungan dihapus Kandungan ditambah
Algazel (bincang | sumb.)
Tiada ringkasan suntingan
Algazel (bincang | sumb.)
Tiada ringkasan suntingan
Baris 99: Baris 99:


<!-- Sorok sementara -->
<!-- Sorok sementara -->

<!--
Dalam contoh ini, perintah <tt>print</tt> disusuli dua argumen, dengan setiap argumen dipisahkan dengan tanda koma. Jadi dengan baris pertama program
In this example, the print command is followed by two arguments, with each of the arguments separated by a comma. So with the first line of the program


<source lang="python">
<source lang="python">
print "2 + 2 is", 2 + 2
print "2 + 2 ialah", 2 + 2
</source>
</source>


The first argument is the string <code>"2 + 2 is"</code> and the second argument is the ''mathematical expression'' <code>2 + 2</code>, which is commonly referred to as an ''expression''.
Argumen pertama ialah string <code>"2 + 2 ialah"</code> dan argumen kedua ialah ''mathematical expression'' (ungkapan matematik) <code>2 + 2</code>, yang lazimnya disebut ''expression'' atau ungkapan.


Apa yang perlu diperhatikan ialah sebuah <tt>string</tt> dicetak seperti yang tertulis (string ialah apa yang terkandung dalam kurungan dua tanda petikan tetapi tidak termasuk tanda petikan itu sendiri. Jadi string dicetak tanpa tanda petikan berganda). Akan tetapi, sebuah ''ungkapan'' akan dinilai (dalam kata lain, ditukar) kepada nilai sebenarnya.
What is important to note is that a string is printed as is (the string is what is within the double quotes but doesn't include the double quotes themselves. So the string is printed without the enclosing double quotes.) But an ''expression'' is ''evaluated'', (in other words, converted) to its actual value.


Python has six basic operations for numbers:
Python mempunyai enam operasi asas bagi nombor:


{| class="wikitable"
{| class="wikitable"
! Operasi
! Operation
! Symbol
! Simbol
! Example
! Contoh
|-
|-
|Kuasa (eksponen)
|Power (exponentiation)
| <code>**</code>
| <code>**</code>
| <code>5 ** 2 == 25</code>
| <code>5 ** 2 == 25</code>
|-
|-
|Darab
|Multiplication
| <code>*</code>
| <code>*</code>
|<code>2 * 3 == 6</code>
|<code>2 * 3 == 6</code>
|-
|-
|Bahagi
|Division
| <code>/</code>
| <code>/</code>
| <code>14 / 3 == 4</code>
| <code>14 / 3 == 4</code>
|-
|-
|Remainder (modulo)
|Baki (''modulo'')
| <code>%</code>
| <code>%</code>
| <code>14 % 3 == 2</code>
| <code>14 % 3 == 2</code>
|-
|-
|Tambahan
|Addition
| <code>+</code>
| <code>+</code>
| <code>1 + 2 == 3</code>
| <code>1 + 2 == 3</code>
|-
|-
|Penolakan
|Subtraction
| <code>-</code>
| <code>-</code>
| <code>4 - 3 == 1</code>
| <code>4 - 3 == 1</code>

Semakan pada 01:57, 23 Januari 2013

Apa yang anda perlu tahu

Anda perlu tahu bagaimana menyunting program denagn penyunting teks atau IDLE, menyimpan fail dan menjalankan fail sebaik sahaja fail disimpan pada cakera.

Pencetakan

Semenjak zaman dulu tutorial pengaturcaraan bermula dengan program kecil bergelar "Hello World!" (Hello Dunia!)[1] Sintaks cetak (print) diubah dalam Python 3.0. Jika anda mengguna Python 3.0, anda harus membaca Non-Programmer's Tutorial for Python 3 (belum diterjemah). Ini ialah sampel Python 2.6 atau 2.7:

print "Hello, World!"

Sekiranya anda menggunakan garis perintah bagi menjalankan program, anda boleh menaipnya dengan penyunting teks, simpan sebagai hello.py dan jalankannya dengan perintah python hello.py

Jika tidak, buka IDLE, buat tetingkap baharu, dan buat program sebagai diperikan dalam Creating and Running Programs.

Apabila program ini dijalankan, ia akan mencetak:

Hello, World!

Saya tidak akan memberitahu anda setiap masa, tetapi bila saya menunjuk anda program, saya sarankan anda menaipnya dan menjalankannya. Saya belajar dengan lebih baik sekiranya saya menaip kod dan anda pun mungkin sama.

Ini program yang lebih rumit:

print "Dua tiga kucing berlari"
print "mana nak sama sikucing belang;"
print "Dua tiga boleh kucari"
print "mana nak sama anda seorang."

Bila anda menjlankan program ini, ia akan mencetak:

Dua tiga kucing berlari
mana nak sama sikucing belang;
Dua tiga boleh kucari
mana nak sama anda seorang.

Apabila komputer melaksanakan program, ia melihat baris:

print "Dua tiga kucing berlari"

jadi komputer mencetak:

Dua tiga kucing berlari

Kemudian komputer turun ke barisan kedua dan melihat:

print "mana nak sama sikucing belang;"

jadi komputer pun mencetak pada skrin:

mana nak sama sikucing belang;

Komputer terus melihat setiap baris, menatuhi perintah dan kemudiannya pergi ke barisan selepasnya. Komputer kekal melaksanakan perintah sehinggalah ia tiba di akhir program.

Istilah

Sekarang mungkin masa yang sesuai untuk emebri anda sedikit penerangan tentang apa yang berlaku–dan sedikit istilah pengaturcaraan.

Apa yang kita lakukan di atas ialah mengguna satu perintah yang disebut print. Perintah print di ikuti satu atau lebih argumen. Jadi dalam contoh ini

print "Hello, World!"

terdapat satu argumen, yakni "Hello, World!". Perhatikan bahawa argumen ini merupakan kumpulan aksara yang dikelilingi tanda petik berganda ("). Ini lazimnya dipanggil string of characters (rentetan aksara), atau panggilan pendeknya, string. Satu lagi contoh string ialah "Dua tiga kucing berlari".

Perintah dan argumennya digelar secara kolektif sebagai sebuah statement (penyata); justeru,

print "Hello, World!"

ialah satu contoh penyata.

Istilah yang telah diperikan di atas mungkin mencukupi buat masa ini.

Ungkapan

Berikut ialah satu lagi program:

print "2 + 2 ialah", 2 + 2
print "3 * 4 ialah", 3 * 4
print "100 - 1 ialah", 100 - 1
print "(33 + 2) / 5 + 11.5 ialah", (33 + 2) / 5 + 11.5

Ini output program:

2 + 2 ialah 4
3 * 4 ialah 12
100 - 1 ialah 99
(33 + 2) / 5 + 11.5 ialah 18.5

Seperti anda dapat lihat, Python mampu menjadikan komputer anda yang berharga dua atau tiga ribu ringgit sebuah kalkulator berharga 15 ringgit.


Dalam contoh ini, perintah print disusuli dua argumen, dengan setiap argumen dipisahkan dengan tanda koma. Jadi dengan baris pertama program

print "2 + 2 ialah", 2 + 2

Argumen pertama ialah string "2 + 2 ialah" dan argumen kedua ialah mathematical expression (ungkapan matematik) 2 + 2, yang lazimnya disebut expression atau ungkapan.

Apa yang perlu diperhatikan ialah sebuah string dicetak seperti yang tertulis (string ialah apa yang terkandung dalam kurungan dua tanda petikan tetapi tidak termasuk tanda petikan itu sendiri. Jadi string dicetak tanpa tanda petikan berganda). Akan tetapi, sebuah ungkapan akan dinilai (dalam kata lain, ditukar) kepada nilai sebenarnya.

Python mempunyai enam operasi asas bagi nombor:

Operasi Simbol Contoh
Kuasa (eksponen) ** 5 ** 2 == 25
Darab * 2 * 3 == 6
Bahagi / 14 / 3 == 4
Baki (modulo) % 14 % 3 == 2
Tambahan + 1 + 2 == 3
Penolakan - 4 - 3 == 1

Notice that division follows the rule, if there are no decimals to start with, there will be no decimals to end with. The following program shows this:

print "14 / 3 = ", 14 / 3
print "14 % 3 = ", 14 % 3
print
print "14.0 / 3.0 =", 14.0 / 3.0
print "14.0 % 3.0 =", 14.0 % 3.0
print
print "14.0 / 3 =", 14.0 / 3
print "14.0 % 3 =", 14.0 % 3
print
print "14 / 3.0 =", 14 / 3.0
print "14 % 3.0 =", 14 % 3.0
print

With the output:

14 / 3 = 4
14 % 3 = 2

14.0 / 3.0 = 4.66666666667
14.0 % 3.0 = 2.0

14.0 / 3 = 4.66666666667
14.0 % 3 = 2.0

14 / 3.0 = 4.66666666667
14 % 3.0 = 2.0

Notice how Python gives different answers for some problems depending on whether or not decimal values are used.

The order of operations is the same as in math:

  • parentheses ()
  • exponents **
  • multiplication *, division /, and remainder %
  • addition + and subtraction -

So use parentheses to structure your formulas when needed.

Talking to humans (and other intelligent beings)

Often in programming you are doing something complicated and may not in the future remember what you did. When this happens, the program should probably be commented. A comment is a note to you and other programmers explaining what is happening. For example:

# Not quite PI, but an incredible simulation
print 22.0 / 7.0    # 355/113 is even more incredible rational approx to PI

Which outputs

3.14285714286

Notice that the comment starts with a hash: #. Comments are used to communicate with others who read the program and your future self to make clear what is complicated.

Note that any text can follow a comment, and that when the program is run, the text after the # through to the end of that line is ignored. The # does not have to be at the beginning of a new line:

# Output PI on the screen
print 22.0 / 7.0 # Well, just a good approximation

Examples

Each chapter (eventually) will contain examples of the programming features introduced in the chapter. You should at least look over them and see if you understand them. If you don't, you may want to type them in and see what happens. Mess around with them, change them and see what happens.

Denmark.py

print "Something's rotten in the state of Denmark."
print "                -- Shakespeare"

Output:

Something's rotten in the state of Denmark.
                -- Shakespeare

School.py

# This is not quite true outside of USA
# and is based on my dim memories of my younger years
print "First Grade"
print "1 + 1 =", 1 + 1
print "2 + 4 =", 2 + 4
print "5 - 2 =", 5 - 2
print
print "Third Grade"
print "243 - 23 =", 243 - 23
print "12 * 4 =", 12 * 4
print "12 / 3 =", 12 / 3
print "13 / 3 =", 13 / 3, "R", 13 % 3
print
print "Junior High"
print "123.56 - 62.12 =", 123.56 - 62.12
print "(4 + 3) * 2 =", (4 + 3) * 2
print "4 + 3 * 2 =", 4 + 3 * 2
print "3 ** 2 =", 3 ** 2
print

Output:

First Grade
1 + 1 = 2
2 + 4 = 6
5 - 2 = 3

Third Grade
243 - 23 = 220
12 * 4 = 48
12 / 3 = 4
13 / 3 = 4 R 1

Junior High
123.56 - 62.12 = 61.44
(4 + 3) * 2 = 14
4 + 3 * 2 = 10
3 ** 2 = 9

Exercises

  1. Write a program that prints your full name and your birthday as separate strings.
  2. Write a program that shows the use of all 6 math functions.


Templat:Solution


-->

Nota kaki

  1. Templat:Simbol bahasa List of "Hello, world!" programs in many programming languages

Templat:Non-Programmer's Tutorial for Python 2.6/Navigation


Rujukan