As stated in the previous part, once we have the first sketch written down we should have a wish list that looks like this:
Wish_list.txt Problem: Get the computer to add 2 plus three and print the result. Target users: Me Target System: Microsoft Windows Interface: Cmd Functional requirements: Add two numbers, print the result, input some numeric data. Testing: Simple run-test -expecting the result of a sum to appear. Maintainer: https://steemit.com/@joelsegovia
Then, we are going to transform this wish list into a program. In order to do that, we save our text file as a .py and then we delimitate the wish list between triple quotation marks to tell the interpreter that it is a text string. Also, it will be a part of the built-in documentation for this script, since a text string that occurs at the first statement of a module, function or class becomes a Python docstring.
Sum_program.py """ Problem: Get the computer to add 2 plus three and print the result. Target users: Me Target System: Microsoft Windows Interface: Cmd Functional requirements: Add two numbers, print the result, input some numeric data. Testing: Simple run-test -expecting the result of a sum to appear. Maintainer: https://steemit.com/@joelsegovia """
Now we are going to start sketching the actual logic flow of the program. We will do so in the natural way we express our ideas, they are going to be a series of logical statements that express the actions that the program should perform pseudocode. Those statements will be made as comments (placing # before each statement)
Sum_program.py """ Problem: Get the computer to add 2 plus three and print the result. Target users: Me Target System: Microsoft Windows Interface: Cmd Functional requirements: Add two numbers, print the result, input some numeric data. Testing: Simple run-test -expecting the result of a sum to appear. Maintainer: https://steemit.com/@joelsegovia """ # First task # Second task # Third task
Here we start to state with more details on what the program should do on each statement. In order to do that, we have to break the statements into nested steps (nesting is also known as indentation).
Sum_program.py
"""
Problem: Get the computer to add 2 plus three and print the result.
Target users: Me
Target System: Microsoft Windows
Interface: Cmd
Functional requirements: Add two numbers, print the result, input some numeric data.
Testing: Simple run-test -expecting the result of a sum to appear.
Maintainer: https://steemit.com/@joelsegovia
"""
# First task
# add two plus two
# Second task
# store the result in a variable called "sum"
# Third task
# print the result
The data we are going to handle will be stored in elements called variables. The core idea behind this concept is that a variable is a vault where a single piece of data (a string or number) will be stored and will be readily edited by assigning a new value to the variable.
To create a variable, two stages have to be carried out. First, the initialization stage is where the container is created and an identification label is assigned to it. Second, the assignment stage is where a value is assigned to the labeled container. In Python, both stages occur in the same statement. Initialization and assignment are performed using the = sign. One of the main characteristics of Python is that its interpreter automatically determines what data type is stored (some number, text or any other kind of data). Each variable is referred to by a label (name) called identifier. This time we will add directly our two numeric values, store them into one variable, and finally print the result.
Sum_program.py
"""
Problem: Get the computer to add 2 plus three and print the result.
Target users: Me
Target System: Microsoft Windows
Interface: Cmd
Functional requirements: Add two numbers, print the result, input some numeric data.
Testing: Simple run-test -expecting the result of a sum to appear.
Maintainer: https://steemit.com/@joelsegovia
"""
# First task
# add two plus two
2+2
# Second task
# store the result in a variable called "sum"
sum = 2+2
# Third task
# print the result
print(sum)
Finally, we have to test the program if all goes well this really simple script should print the number 4 which is the result of adding 2 + 2
|
Basic Concepts. Part 1 Whaleshares Link Steemit Link |
Basic Concepts. Part 2 Whaleshares Link Steemit Link |
Basic Concepts. Part 3 Whaleshares Link Steemit Link |