Stage 2: Basic Python

Chapter 1: The Basics of Programming

Basic CSS

At its fundamental level, computer science is the division of large, difficult problems into smaller component parts that can be systematically solved in a logical fashion. While a program-less computer is not much more complex than a toaster, a computer, when harnessed by a set of programs, can undertake many complicated tasks in an astonishing small period of time.

Languages and Python

Programs are essentially clear progressions of logical steps written in very specific languages that account for the natural stupidity of a computer by comprehensively dictating its behavior. While human languages like English are not ideal for programming due to their natural grammatic and syntactic ambiguity and subjectivity, languages written specifically for computers, like Python, rectify these problems by issuing minimal commands in a strict, precise, and comprehensive fashion.

Programming Grammar

Programming languages like Python follow the Backus-Naur Form, a precise syntactic convention created to reduce the ambiguity and subjectivity found in the earliest languages. Like the 'subject, verb, object' convention found in human languages, the Backus-Naur Form deals in terms of sentence structures, though it uses terms like non-terminals, replacements, and terminals to dictate the manner in which programs deal with expressions, operators, and numbers.

Chapter 2: More Detailed Python

Variables

As simplicity and readability are valued characteristics of quality code, the use of user-assigned variables is often a necessary step in the development process. The assignment of variables sets a value to a variable, which may be employed further down in the code, allowing for increased legibility and reducing needless repetition of values in elements. It is important to note that variables are set using the equals sign, which in programming does not mean 'equals to.' Rather, the equals sign is simple notation that dictates the value of the variable in question, much like an arrow would do.

Strings

Not all variable values have to be numbers exclusively. Strings are defined as sequences of characters or letters surrounded by either single or double quotation marks. It is possible to 'concatenate' strings together, which is, in layman terms, the linking of strings together in a series. It is important to note that strings and integers cannot be concatenated, though they can be multiplied together. Strings may also be indexed or subsequenced, that is, they may be analyzed for a specific character or group of characters within the string.

Fun with Find

The find command is especially helpful in locating strings within strings, particularly in cases where the coder is unaware of the exact index coordinates needed to produce the desired characters from the string. The find command locates the first instance of a desired set of characters only, though the addition of other integer parameters can force the command to produce other instances of the desired characters.

Chapter 3: Functions/Procedures

Basics of Functions

Functions, also known as procedures in Python terminology, are operations that take inputed code, known as operands or arguments, and return a designated output dependent upon the statements included within the function code. They are notated as def and provided a name and a set of associated parameters contained with parentheses.

Making and Using Functions

It is important to grasp the subtle distinction between making functions and using functions. Making a function involves assembling the appropriate code and input parameter variables, including the necessary return commands and such in the body of the function. Using, or calling, the function involves the replacement of the parameter variables with actual values, be those integers or strings, and the evaluation of the function though the use of the print command.

The Usefulness of Functions

The practical utility of functions/procedures cannot be overstated. At their most basic, they reduce needless repetition in code by conveniently and comprehensively setting immutable values to functions, values that may be called further in the code without the need to rewrite or copy/paste the function code.

Chapter 4: Control Flow and Loops

Loops

Loops are pieces of code embedded within procedures that enable programmers to modify their outputs based on the behavior of inputs. They are exceedingly useful in iteratively performing operations or modifying outputs for specific conditions, operations that would otherwise require extensive coding to do otherwise. As such, they are some of the most useful applications found in programming languages.

If Statements

The if statement is used in Python to modify the behavior of the procedure based on a number of preconditions against which the inputs are measured. If the conditions are not met, the else operator may be used, which dictates what the procedure will do in the event of a condition failure. Ifs and elses may be embeded within each other as well, alongside while loops and for loops, for increased specificity.

While Loops

The while loop performs operations iteratively until a condition or series of conditions are met. The while loop is useful for performing operations repetitively, and saves much unneeded code, thus reducing complexity. While loops, like if statements, may be embedded within other loop operators.

Chapter 5: Debugging

Understand Error Messages

Debugging is an important step in coding often overlooked by beginner programmers. Even when code runs, mistakes occur, and a wary programmer must be prepared to recognize the steps to be taken to rectify the problem. First, when code crashes, it is important to understand the information conveyed by the error messages. Terms like 'Syntax error' or 'unsupported operand' may confuse new coders, but they often give clues as to the specific parts of the program that are producing errors. Understanding them, and learning to translate them into human-speak, is an important debugging concept.

Examine Working Code

When mistakes are first detected, it is tempting to look first for what is not working. However, it is often more advantageous to look at what is working and why it is working before looking for errors. As working code is checked off, the problem area shrinks in size, and the issue will more quickly come to light.

Compare Old Versions

It is often helpful to keep old versions of a program handy for examination. It may be fast and easy to hit ctrl-z a few times, but doing so may erase new, correct developments in the code, and will make it difficult to see the full scale of the problem. Comparing code from an old version of the program to the new, problematic version often visually highlights the glaring error, and makes debugging that much easier.

Chapter 6: Structured Data

Lists

Sometimes, a solution that simply works is not always the best. Often, the solution that works best overall while preserving simplicity is best. Strings by themselves are often acceptable in a procedure, but multiple, unorganized strings can often clutter a program. Lists are used to store strings in an orderly way that allows them to be called, sequenced, and indexed by a procedure in a comprehensive way. Elements of a list can be anything; strings, integers, even other lists.

List Operations

There are a number of operations that can be used on lists to gather the desired values from their contents. The first is the append method, which works like find did on strings, adding a new element to the end of the list. The second is +, which is similar to string concatenation. + produces a new string with the new elements provided. The third is len, which, like a procedure call, returns the number of elements within the list, be those elements numbers, strings, or lists themselves.

For Loops

The second type of loop frequently seen in Python is the for loop, which loops through data stored in lists in a simple manner. Though the elements stored within a list could technically be evaluated and returned by a while loop, the built-in for loop allows coders to reduce extraeous loops in favor of one that evaluates all elements of the list against a set of conditions.

Chapter 7: Solving Problems

Breaking Down a Problem

Solving difficult or complex problems in a simple and effective manner can often be a struggle for new coders, so it is wise to formulate a basic strategy that will break down the problem into easily visualized smaller chunks that can be dealt with separately. Dividing the problem into multiple procedures that each deal with a specific aspect is a good place to start in visualizing how each cog of the wheel works in concert with the others.

Inputs -> Procedure -> Outputs

If the divisions are not readily apparent from the outset, sometimes it is wise to understand what the inputs are and how they are represented in the code. Once a clear understanding of inputs is acquired, the desired outputs should be examined and the relationship between them revealed through the working of real-world examples.

Pseudocode

Jumping right into the language-specific coding of a problem is not always the best solution. Unless the coder is an expert in the specifics of the language, he will often be mired down in the nitty-gritty coding syntax and quickly lose sight of the bigger picture. The implementation of pseudocode is often a better place to start. Pseudocode ignores the very specific syntax of the language and instead focuses on visually representing the inputs, outputs, and procedures necessary to solve the problem.