Programming

Programming is the core of computer sciences. A computer is a universal machine. It can be programmed to do anything. So anything we can imagine (everything we can figure out to write a program for), we can make the computer to do.

A program is a very precise sequence of steps. A computer has only a few instructions. What need to be done is to put this instructions together to create something.

A programming language is designed to be read and written by humans, and executed by computers.

Natural language (like English, German, Chinese, etc.) are ambiguious and very verbose. They are not suitable.

Grammar

Languages have a grammar which can be represented for instance with the Backus-Nour form ( -> replacement)::

Sentence -> Subject Verb Object
Subject -> Noun
Object -> Noun
Verb -> search
Verb -> teach
Noun -> Hevok
Noun -> EVA

Python Grammar for Arithmetic Expressions

The basic grammar for Python to do math is the following: Expression -> Expression Operator Expression

It is a recursive definition which allows to build up an indefinitely possibilities of calculations::

Expression -> Number
Operator -> +
Operator -> *
Number -> 0,1, ...
Expression -> (Expression)

Variables

Variables can very, that is why the are called variables. Variables are set be a assignment statements where a name is set equal to an expression: Name = Expression

variables are usually in lowercase as convention.

The value of a variable can be referred by its name and it be changed dynamically.

Multiple Assignment

a, b = b, a

Swaps the variables values as long as the number of the variables on the right side are exactly the same as the number of values on the right side.

Comments

Comments start with a hash symbol. Everything on the right site of it (from the has to the end of line) will be ignored by the interpreter.

Strings

A string is a sequence of characters embedded in quotes (single or double quotes).

Strings can be concatenated with the plus operator. Strings can even be multiplied with numbers to repeat it.

the function 'str' turns any number or object into a string.

Indexing

Character in a string can be access by an index: `string[position]

Slicing

Slicing is the derivation of sub-sequences. To select sub-sequences of string a start and stop index can be used: string[start:stop] If start or/and stop are omitted default to 0 and -2 (i.e. very start and end of the string).

Index starting from position but not including stop.

A string can be reverted with: string[::-1]

Finding Strings in String

find is built-in procedure (a method) to retrieve the starting position of the first occurrence of string in another one:

search_string.find(target_string, number)

Find takes an optional argument and returns a number (location) that gives the first position in search string where the target string appears [at or after the number].

If there is no occurrence found it will evaluate to "-1".

Extracting links

In html a link starts with an a tag. <a href= is how to start an link. It is followed by a string surrounded by double quotes. The a means that is a anker tag and the href indicates that it is a hyperlink.

To find the first link::

start_link = page.find('<a href=')+9
stop_link = page.find('>"', start_link)
url = page[start_link, stop_link]

In python white-space matters. Thus newlines as well as indentation are used to define block of codes. The python interpreter knows when a block ends on where the indenting is.

Control

How to Repeat

Procedure

A procedure takes input in, does some work on these inputs and produces outputs as results.

The syntax for a procedure is::

def name(parameters):
    block

def stands for definition.

For instance the built-in operator + takes two number and outputs the sum. The name of procedure can be anything a variable can be. Parameters are the input of the procedure. The inputs are just a list of names separated by commas. There can be as many inputs as desired, even no input. They are surrounded by parens and end with a colon. After the colon the block starts and is the body of the procedure. Basically it consists just of statements. The block is indented inside the definition. Usually 4 spaces (convention).

The following procedure finds the first occurrence of html link and outputs that link as well as its end position:

:::python
def get_next_target(s):
    """Finds the next url target in a string (s) and return the url as well as the end_quote position."""
    start_link = s.find('<a href=')
    start_quote = s.find('"', start_link)
    end_quote = s.find('"', start_quote+1)
    url = s[start_quote: end_quote]
    return url, end_quote

What a procedure outputs is defined with return followed by a list of any number of expression. There can be Zero ("side effects") to any number of expressions outputted.

The inputs are also referred as operands or arguments as well as parameters.

Procedure composition is using a procedure as the input for another one.

Making Decisions

If one needs to make comparisons there are different operators for doing comparisons:

  • == Equal
  • != Unequal
  • < less than
  • > greater than
  • <= less than equal
  • >= greater than equal

The output is a Boolean-value which is either True or False.

A decision in code is made with the if statement. The syntax for an if test is first the keyword if followed by test expression, then double colon and intended block code which only become execute if the text expression evaluates to True.

if test:
    block

Multiple alternative tests can be included with an elif statement. Of no one evulates to True the else statement will be executed (if given)::

if test:
    block
elif another test:
    block
else:
    block

or operator. It only valuates what it need to::

 expression or expression

If the first expression evaluates to True, the value is True and the second expression is not evaluated. However if the first expression evaluates to False, the value is the of the second expression.

def bigger(a,b):
    if a > b:
        return a
    return b

def biggest(a,b,c):
    return bigger(bigger(a,b), c)

With a very few simple operation its possible to simulate any other machine:

  • arithmetic
  • comparisons
  • procedures
  • tests (if)

In such this knowledge is enough (in theory) to write every possible computer program.

However there are more powerful constructs which enable in a more efficient and elegant way to build sophisticated programs with no time.

Loops

Loops are s a way of doing things over and over. It syntax is just while followed by a text expression and colon. The block is intended and is executed as long as the test evaluates to True.

while test expression:
    block

This enables for instance to count::

i = 0 while i != 10: i += 1 print(i)

def print_numbers(x):
    """Prints out the all numbers up to and including x."""
    num = 0
    while num < x:
        num += 1
        print(num)

def factorial(n):
    """n * (n-1) * (n-2)...
    Takes a number as its input and outputs the factorial of that number"""
    result = 1
    sub = 0
    while sub != n:
        result *= (n - sub)
        sub += 1

def factorial(n):
   result = 1
   while n >= 1:
       result += n
       n -= 1
   return result
    return  result

Break

Break allows to break out of a loop.

while testExpression: if breakTest: break

def get_next_target() if start_link == -1: return None, 0

def find_all_links(page): while True: url, endpos = get_next_target(page) if url: print url page = page[endpos:] else: break

Encoding

If different characters are use with the code (comments, strings, etc.), such as é, í, ç or Chinese/Japanese chars one needs to add:

# -*- coding: utf-8 -*-

on the beginning of the file [http://www.python.org/dev/peps/pep-0263/].

Structured Data

A string is just a bunch of characters. In contrast, lists is a sequence of anything (strings, numbers, other list, etc.).

Lists are indicated with square brackets and the elements are separated by commas.

Lists have two main differences from strings. Firstly list can hold anything, while the elements in string can only be characters. Secondly lists support mutation, they are mutable. Mutation means that it can be changed after it has been created. In contrasts string can only be recreated, while lists can be changed in situ.

Aliasing

Aliasing is haven multiple ways to refer the the same object. Consider this example::

a = ['D', 'e', 'n', 'i', 'g', 'm', 'a']
b = a
a[0], a[1] = 'd', 'E'
print(a==b)
print(b)

Mutation is visible through any reference to the same list object. + executes similar to the concatenation of strings the combination of both lists.

Storing Data

1 byte are 8 bites. One bite can be in one of two states, 0 or 1.

Register is the fastest memory that is built right in the processor.

Latency is the time it takes to get a response, i.e. the time to retrieve a value from memory.

Data that need to be accessed frequently should be stored in close, fast-to-reach places such as CPU register or DRAM, not the hard drive (which is like traveling between Munich and Moscow to get to the data.

For Loops

The structure of a forloop is like this. There is the keyword for followed by a name for a variable

for <name> in <list>:
    <Block>

Index

The build in index is a method of a list that takes in a value and the output is the position that that value exists in the list.

.index()

If the is in the , returns the first position where is found in . otherweise, produces an error.

in

in if is in the ,list>, output is True, otherwise , output is False.

<value> not in <list> == not <value> in <list>

Pop

<list>.pop() -> element mutates the list by removing and returning its last element.


Tags: coding

Edit this page
Wiki-logo