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

tiada ringkasan suntingan
(Mencipta laman baru dengan kandungan '=== Apa yang anda perlu tahu === Anda perlu tahu bagaimana menyunting program denagn penyunting teks atau IDLE, menyimpan fail dan menjalankan fail sebaik s...')
 
Tiada ringkasan suntingan
=== Pencetakan ===
 
Semenjak zaman dulu tutorial pengaturcaraan bermula dengadengan program kecil bergelar "Hello World!" (Hello Dunia!)<ref>{{en}} [[Computer Programming/Hello world|List of "Hello, world!" programs in many programming languages]]</ref> Sintaks cetak (<tt>print</tt>) 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:
 
<source lang="python">
Dua tiga boleh kucari
mana nak sama anda seorang.
 
Apabila komputer melaksanakan program, ia melihat baris:
 
<source lang="python">
print "Dua tiga kucing berlari"
</source>
 
jadi komputer mencetak:
 
Dua tiga kucing berlari
 
Kemudian komputer turun ke barisan kedua dan melihat:
 
<source lang="python">
print "mana nak sama sikucing belang;"
</source>
 
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&ndash;dan sedikit istilah pengaturcaraan.
 
Apa yang kita lakukan di atas ialah mengguna satu ''perintah'' yang disebut <code>print</code>. Perintah <code>print</code> di ikuti satu atau lebih ''argumen''. Jadi dalam contoh ini
 
<source lang="python">
print "Hello, World!"
</source>
 
terdapat satu ''argumen'', yakni <code>"Hello, World!"</code>. 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 <code>"Dua tiga kucing berlari"</code>.
 
Perintah dan argumennya digelar secara kolektif sebagai sebuah ''statement'' (penyata); justeru,
 
<source lang="python">
print "Hello, World!"
</source>
 
ialah satu contoh penyata.
 
Istilah yang telah diperikan di atas mungkin mencukupi buat masa ini.
 
=== Ungkapan ===
Berikut ialah satu lagi program:
 
<source lang="python">
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
</source>
 
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.
 
<!-- Sorok sementara -->
<!--
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">
print "2 + 2 is", 2 + 2
</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''.
 
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:
 
{| class="wikitable"
! Operation
! Symbol
! Example
|-
|Power (exponentiation)
| <code>**</code>
| <code>5 ** 2 == 25</code>
|-
|Multiplication
| <code>*</code>
|<code>2 * 3 == 6</code>
|-
|Division
| <code>/</code>
| <code>14 / 3 == 4</code>
|-
|Remainder (modulo)
| <code>%</code>
| <code>14 % 3 == 2</code>
|-
|Addition
| <code>+</code>
| <code>1 + 2 == 3</code>
|-
|Subtraction
| <code>-</code>
| <code>4 - 3 == 1</code>
|}
 
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:
 
<source lang="python">
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
</source>
 
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 <code>()</code>
* exponents <code>**</code>
* multiplication <code>*</code>, division <code>/</code>, and remainder <code>%</code>
* addition <code>+</code> and subtraction <code>-</code>
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:
 
<source lang="python">
# Not quite PI, but an incredible simulation
print 22.0 / 7.0 # 355/113 is even more incredible rational approx to PI
</source>
 
Which outputs
 
3.14285714286
 
Notice that the comment starts with a hash: <code>#</code>. 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 <code>#</code> through to the end of that line is ignored. The <code>#</code> does not have to be at the beginning of a new line:
 
<source lang="python">
# Output PI on the screen
print 22.0 / 7.0 # Well, just a good approximation
</source>
 
=== 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'''
 
<source lang="python">
print "Something's rotten in the state of Denmark."
print " -- Shakespeare"
</source>
 
Output:
 
Something's rotten in the state of Denmark.
-- Shakespeare
 
'''School.py'''
 
<source lang="python">
# 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
</source>
 
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 ===
 
# Write a program that prints your full name and your birthday as separate strings.
# Write a program that shows the use of all 6 math functions.
 
 
{{Solution|title=Solution|text=
 
1. Write a program that prints your full name and your birthday as separate strings.
 
<source lang="python">
print "Ada Lovelace", "born on", "November 27, 1852"
</source>
 
2. Write a program that shows the use of all 6 math operations.
 
<source lang="python">
#Anything along these lines is acceptable:
 
#Addition
print "2 + 5 = ", 2 + 5
 
#subtraction
print "9 - 3 = ", 9 - 3
 
#multiplication
print "3 * 3 = ", 3 * 3
 
#division
print "90 / 5 = ", 90 / 5
 
#exponents
print "7 to the power of 2 (squared) = ", 7 ** 2
 
#remainder
print "the remainder when doing 22 / 9 = ", 22 % 9
</source>
}}
 
----
 
-->
==== Nota kaki ====
<references/>
 
{{Non-Programmer's Tutorial for Python 2.6/Navigation|Intro|Who Goes There?}}
 
 
== Rujukan ==
1,163

suntingan