204111:Python missing details

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา

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