A.4 Essential Operators and Expressions

Python is intended for programmers who want to get work done quickly. Thus, it was designed to have a terse syntax, which even permeates the writability of Python programs. For instance, in what follows notice that a Python programmer rarely needs to use a ; (semicolon).

  • Character conversions. The ord and chr functions are used for character conversions:

    A set of six code lines in Python using o r d and c h r functions.
    Description
  • Numeric conversions.

    A set of four code lines in Python for numeric conversions.
    Description
  • String concatenation. The + is the infix binary append operator that is used for concatenating two strings.

    A set of two code lines in Python for string concatenation.
    Description
  • Arithmetic. The infix binary operators +, -, and * have the usual semantics. Python has two division operators: // and /. The // operator is a floor division operator for integer and float operands:

    A set of 12 code lines in Python with a floor division operator.
    Description

    Thus, integer division with // in Python floors, unlike integer division in C which truncates. Unlike //, the / division operator always returns a float:

    A set of 12 code lines in Python with a division operator.
    Description
  • Comparison. The infix binary operators == (equal to), <, >, <=, >=, and != (not equal to) compare integers, floats, characters, strings, and values of other types:

    A set of 10 code lines in Python with comparison operators.
    Description
  • Boolean operators. The infix operators or, and, and not are used with the usual semantics. The operators or and and use short-circuit evaluation (or lazy evaluation as discussed in Chapter 12):

    A set of six code lines in Python with Boolean operators.
    Description
  • Conditionals. Use if and ifelse statements:

    A set of five code lines in Python with if statement.
    Description
  • Code indentation. Indentation, rather than curly braces, is used in Python to delimit blocks of code. Code indentation is significant in Python. Two programs that are identical lexically when ignoring indentation are not the same in Python. One may be syntactically correct while the other may not. For instance:

    A set of 16 code lines in Python with code indentation.
    Description

    The indentation conventions enforced by Python are for the benefit of the programmer—to avoid buggy code. As Bruce Eckel says:

    [B]ecause blocks are denoted by indentation in Python, indentation is uniform in Python programs. And indentation is meaningful to us as readers. So because we have consistent code formatting, I can read somebody else’s code and I’m not constantly tripping over, “Oh, I see. They’re putting their curly braces here or there.” I don’t have to think about that. (Venners 2003)

  • Comments.

    • Single-line comments:

      A single-line comment in Python.
      Description
    • Multi-line comments. While Python does not have a special syntax for multi-line comments, a multi-line comment can be simulated using a multi-line string because Python ignores a string if it is not being used in an expression or statement. The syntax for multi-line strings in Python uses triple quotes—either single or double:

      A set of five code lines in Python with multi-line comments.
      Description

      In a Python source code file, a mutli-line string can be used as a comment if the first and last triple quotes are not on the same lines as other code:

      A set of 10 code lines in Python with a multi-line string.
      Description
    • Docstrings are also used to comment, annotate, and document functions and classes:

      A set of 22 code lines in Python with docstrings.
      Description
  • The list/split and join functions are Python’s analogs of the explode and implode functions in ML, respectively:

    A set of eight code lines in Python with list and join functions.
    Description
    Continuation of the code in Python with list and join functions.
    Description
  • To run a Python program:

    • Enter python2 at the command prompt and then enter expressions interactively to evaluate them:

      A set of four code lines in Python for running a program.
      Description

      Using this method of execution, the programmer can create bindings and define new functions at the prompt of the interpreter:

      A set of 10 code lines in Python for creating bindings and defining new functions at the prompt of the interpreter.
      Description

      Enter the EOF character [which is <ctrl-d> on UNIX systems (line 9) and <ctrl-z> on Windows systems] or quit() to exit the interpreter.

    • Enter python <filename>.py from the command prompt using file I/O, which causes the program in <filename>.py to be evaluated line by line by the interpreter:3

      A set of 13 code lines in Python for evaluating a program line by line by an interpreter.
      Description

      Using this method of execution, the return value of the expressions (lines 5 and 10 in the preceding example) is not shown unless explicitly printed (lines 5 and 10 in the next example):

      A set of 15 code lines in Python with the return value of the expressions not explicitly shown.
      Description
    • Enter python at the command prompt and then load a program by entering import <filename> (without the .py filename extension) into the interpreter (line 18 in the next example):

      A set of 21 code lines in Python with the python command prompt entered and a program loaded by entering the import statement.
      Description

      If the program is modified, enter the following lines into the interpreter to reload it:

      A set of five code lines in Python to be entered into the interpreter for reloading a modified program.
      Description
    • Redirect standard input into the interpreter from the keyboard to a file by entering python < <filename>.py at the command prompt:4

      A set of 10 code lines in Python for redirecting standard input into the interpreter from the keyboard to a file.
      Description

2. The name of the executable file for the Python interpreter may vary across systems (e.g., python3.8).

3. The interpreter automatically exits once EOF is reached and evaluation is complete.

4. Again, the interpreter automatically exits once EOF is reached and evaluation is complete.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.241.116