Preparation

Currently, multiple IDEs support Python programming, such as the official IDLE, PyCharm, Anaconda, VS Code, etc. For teaching purposes, this book uses the simplest IDLE.

Software Installation and Programming Environment

Connect your computer to the internet, visit the Python official website https://www.python.org, and download the Python installer matching your operating system. If your OS is 64-bit, download the 64-bit installer. After downloading, double-click the executable installer and follow the prompts step by step to complete the installation.

Taking Python 3.10 installed by the author as an example: after installing the Python software, click "IDLE" under the "Python 3.10" directory from the "Start" menu in the lower-left corner of Windows to open the "IDLE Shell" window, as shown in Figure 2-1.

Figure 2-1 IDLE Shell Window The first line of the window displays software and system information, including the Python version, start-up time, and system details. The second line prompts that typing keywords like help after the prompt (>>>) will provide more information such as help and copyright.

The third line shows the prompt >>>. Type Python statements after it and press Enter—another prompt will appear, allowing you to continue typing. This programming method is called command-line programming, where statements are entered and executed line by line. In subsequent chapters, any Python statement preceded by >>> indicates command-line mode programming in the "IDLE Shell" window.

Click "New File" in the "File" menu of the "IDLE Shell" window to open the window shown in Figure 2-2. Enter statements or functions continuously here, save them as a .py file, and click "Run Module" in the "Run" menu to execute multiple lines of code at once. This method is called script-file programming. In subsequent chapters, any Python statement without the >>> prompt indicates script-file mode programming.

Figure 2-2 Writing Script Files

Programming Examples

The following examples demonstrate three programming methods in IDLE: command-line, script-file, and function-file. For now, focus on following the steps and feeling the Python programming workflow—there’s no need to dive deep into the code meaning. Compare the similarities and differences between these methods.

Example 1: Command-Line Mode

This example demonstrates addition and cumulative summation in command-line mode. In the "Python Shell" window, type the following statements after the prompt to calculate the sum of two numbers:

code.python
>>> a=1
>>> b=2
>>> a+b
3

Here, a and b are called variables, referencing objects 1 and 2, respectively. a=1 is an assignment expression, using the assignment operator = to assign the number 1 to variable a. a+b is an arithmetic expression, using the + operator to return the sum of a and b.

Next, a cumulative summation example: sum integers from 0 to 4 using a for loop and the range() function (which generates integers in the range 0–4). The loop iterates through each number and adds it to variable s. Variable i is an iteration variable, taking the next value in the range each loop. s += i is a compound assignment expression (equivalent to s = s + i). Finally, output s (the sum of 0–4). Notes starting with # are comments explaining the code.

code.python
>>> s=0
>>> for i in range(5):  # Loop through 0–4
... s += i  # Cumulative summation
... 
>>> s
10

Note: The loop body under the for statement must be indented by 4 spaces.

Example 2: Writing and Running Script Files

Click "New File" in the "File" menu of the "Python Shell" window to open the script file window. Enter the following code (with line numbers for explanation) to perform the addition and summation from Example 1:

code.python
1   a=1
2   b=2
3   print(a+b)
4
5   s=0
6   for i in range(5):
7       s += i
8   print(s)

Lines 1–3: Calculate the sum of 1 and 2, then output the result using print().

Lines 5–8: Use a for loop to sum integers 0–4, then output the result.

In the IDLE file window, click "Run Module" in the "Run" menu. The IDLE command-line window will display:

code.python
>>> =RESTART: …/Samples/ch02/sam02.py
3
10

This runs all code at once (instead of line-by-line input). Such files are called script py-files (analogous to macros, defining sequential actions). Saving code as a file allows reuse without re-typing commands.

Example 3: Writing and Running Function Files

Rewrite Example 2’s script by converting addition and summation into functions, then call the functions with parameters (numbers to add or upper limits for summation). Details about functions will be covered later—for now, focus on the workflow and benefits.

code.python
1   def MySum(a, b):
2       return a + b
3
4   def MySum2(c):
5       s = 0
6       for i in range(c + 1):
7           s += i
8       return s
9
10  print(MySum(1, 2))  # Call MySum
11  print(MySum(3, 5))
12  print(MySum(8, 12))
13  print(MySum2(4))   # Call MySum2
14  print(MySum2(10))

Lines 1–2: Define MySum(a, b) to return the sum of a and b.

Lines 4–8: Define MySum2(c) to return the sum of integers 0–c.

Lines 10–12: Call MySum with different parameters to compute sums.

Lines 13–14: Call MySum2 to compute sums of 0–4 and 0–10.

Run the script: the IDLE command-line window outputs:

code.python
>>> =RESTART: …/Samples/ch02/sam03.py
3
8
20
10
55

Defining functions improves reusability—you can call them repeatedly with different parameters instead of rewriting code.