Tech

What is a program?–Notes…

What is a program?

A program is a sequence of instructions that specifies how to perform a computation.

  • A few basic instructions appear in just about every language:
    • input–get data from a keyboard, a file, or some other device.
    • output–display data on the screen or send data to a file or other device.
    • maths–perform basic mathematical operations like addition & multiplication.
    • conditional execution–check for certain conditions and execute the appropriate sequence of statements.
    • repetition–perform some action repeatedly, usually with some variation.

That’s pretty much all there is to it! Every program is made up of instructions that look more or less like these!

We can describe programming as the process of breaking a large, complex task into smaller and smaller subtasks, until the subtasks are simple enough to be performed with sequences of these basic instructions.

semantics–the meaning of the program.

syntax–the structure of the program; the rules of a programming language.

PROBLEM SOLVING!

Problem Solving–the process of formulating a problem, finding a creative solution, and expressing the solution, clearly.

The single most important skill for a computer scientist is problem solving!

  • A value is one of the fundamental things (like a letter or number) that a program manipulates.
  • Values are classified into different classes, or data types.
    • Ex: strings, integers, floats
  • The Python function ‘type‘ can tell us which value something represents. For example:
    • >>> type (“Hello, World!”)
      • <class ‘str’>
    • or,
    • >>> type (17)
      • <class ‘int’>

Variables

A variable is a name that refers to a value. Or a container for a value! One of the most powerful features of a programming language is the ability to manipulate variables.

Variables can contain both letters & digits, but they have to begin with a letter, or an underscore. They CANNOT begin with a digit. Variable names CANNOT be one of that languages ‘Keywords’.

In Python, a variable is a symbolic name that is a reference, or pointer (->) to an object!

Once you assign an object to a variable, you can refer to the object by that variable name BUT the data itself is still contained in that object. A variable gets a NAME (what do we call it?), a TYPE (what type of data does it contain?), and an INITVAL (what is its initial value?).

  • Whenever you make a variable think these 3 things:
    • NAME
    • TYPE
    • InitValue

A statement is an instruction that the Python interpreter can execute. It is the actions that the function is to perform. When you type a statement on the command line, Python executes it. Statements don’t produce any result. Ex’s: ‘assignment’ statements, ‘while’ statements, ‘for’ statements, ‘if’ statements, “import” statements, and there are others still, too!

An expression is a combination of values, variables, constants, operators, and calls to functions. When you type an expression at the Python prompt, the interpreter evaluates it, and displays the result (i.e.–computes to produce another value). Ex: >>> 1+1 evaluates to 2 OR >>> len(“hello”) evaluates to 5.

Composition is the ability to combine simple expressions and statements into compound statements and expressions, in order to represent complex computations concisely. One of the most useful features of programming languages is their ability to take small building blocks and compose them into larger chunks.

modulus operator (%)–An operator, denoted with a percent sign, that works on integers and yields the remainder when one number is divided by another. (Note! When %, modulo, is used inside a “string” it is something different known as string formatting, or interpolation!)–This has been superseded in Python v.3 by the .format string operation. See Python docs for more!

Python Version 3 is the latest version and consists of the Python interpreter and its batteries-included standard library, to allow easy access to Mac features. It also includes the Python integrated development environment, aka I.D.E. or IDLE.

To make a comment in a Python script, use ‘#‘. Anything after the # is ignored by Python.

“Using classes, building inheritance, and constructing objects are keys to developing yourself as a professional programmer.” —TechBeamers.com

  • Points To Remember:
    • You need logical thinking and curiosity to program.
    • Common features of all programs:

Structured Programming

-coined by Edsger Dijkstra

  • Structured Programming says that all programs could be structured in the following four ways. These “Structured Programming” concepts are common features of all programs:
    1. Sequences of instructions–the program flows from one step to the next in strict sequence.
    2. Branches (a.k.a. conditional construct)–because the program flow is dependent on the result of a test condition. That is, the program reaches a decision point, and if the result of the test is true, then the program performs the instructions in one path, and if false, it performs the actions in another path.
    3. Loops–in this construct the program steps are repeated continuously until some test condition is reached, at which point control then flows past the loop into the next piece of program logic.
    4. Modules–a.k.a. a sub-routine, procedure, or function. Here the program performs an identical sequence of actions several times. For convenience, these common actions are placed in a module, which is a kind of mini-program which can be executed from within the main program.

Along with these structures, programs also need a few more features to make them useful:

  • Data
  • Operations (add, subtract, compare, etc.)
  • Input/Output capability (to display results)

Once you understand these previous concepts, and how a particular programming language implements them, then you can write a program in that language.

  • Points To Remember:
    • Programs control the computer.
    • Programming languages allow us to ‘speak’ to the computer at a level that is closer to how humans think than how computers ‘think’.
    • Programs operate on data.
    • Programs can be either ‘Batch’-oriented or ‘Event’-driven.