ผลต่างระหว่างรุ่นของ "204111:Python missing details"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
 
(ไม่แสดง 3 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน)
แถว 1: แถว 1:
There are a number of topics left out from the teaching slides, because they are not central in programming concepts.  However, these are details that are useful.  Many of these are related to Python syntax.  Some is related to string formatting.   
+
There are a number of topics left out from the teaching slides, because they are not central in programming concepts.  However, these details are useful.  Many of them are related to Python syntax.  Some is related to string formatting.   
  
 
: ''[[204111:Python รายละเอียดที่ละไว้|อ่านเอกสารนี้ในภาษาไทย'']]
 
: ''[[204111:Python รายละเอียดที่ละไว้|อ่านเอกสารนี้ในภาษาไทย'']]
แถว 27: แถว 27:
  
 
There are examples of incorrect names (with explanation):
 
There are examples of incorrect names (with explanation):
* <tt>16monkeys</tt> --- starts with digits
+
* <tt>16monkeys</tt> --- it starts with digits.
* <tt>number-of-primes</tt> --- contains "-"
+
* <tt>number-of-primes</tt> --- it contains "-".
* <tt>def</tt> --- "def" is a keyword
+
* <tt>def</tt> --- "def" is a keyword.
  
 
== String formatting ==
 
== String formatting ==

รุ่นแก้ไขปัจจุบันเมื่อ 04:33, 7 กรกฎาคม 2553

There are a number of topics left out from the teaching slides, because they are not central in programming concepts. However, these details are useful. Many of them are related to Python syntax. Some is related to string formatting.

อ่านเอกสารนี้ในภาษาไทย

Naming rules

When you want to use a variable, you have to give it a name. This is also true, again, with functions that you want to create. Providing good names helps people, including yourself, understand your programs faster and better.

In Python, there are rules regarding how to name things. You can choose names like "x", "total", or even "I_love_You_so_Much", but you can't have variables or functions with names like "2things", "x/y", or "sum of numbers". From this example, you can probably guess the reason. These forbidden names confuse the Python interpreter. For example, it would not be able to distinguish between a variable called x/y and an expression x/y

Here are the rules:

  • Names must start with English alphabets, either in lower case or upper case, or an underline (_).
  • The other letters in the names can be either English alphabets, digits, or underlines.

Also, there are names that you cannot use, because Python has already given them special meaning. For example, you can't have a variable named "def". These names are called keywords, and they are listed here:

and       del       from      not       while
as        elif      global    or        with
assert    else      if        pass      yield
break     except    import    print
class     exec      in        raise
continue  finally   is        return
def       for       lambda    try

These are examples of possible names: total, is_correct, MeaningOfString, _when, number2, primeCount,

There are examples of incorrect names (with explanation):

  • 16monkeys --- it starts with digits.
  • number-of-primes --- it contains "-".
  • def --- "def" is a keyword.

String formatting

to be added later

Full operator precedence list

to be added later