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. GrammarLanguages have a grammar which can be represented for instance with the Backus-Nour form (
Python Grammar for Arithmetic ExpressionsThe basic grammar for Python to do math is the following:
It is a recursive definition which allows to build up an indefinitely possibilities of calculations::
VariablesVariables 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
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. CommentsComments 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. StringsA 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. IndexingCharacter in a string can be access by an index: SlicingSlicing is the derivation of sub-sequences. To select sub-sequences of string a start and stop index can be used: Index starting from position but not including stop. A string can be reverted with: Finding Strings in String
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 linksIn html a link starts with an a tag. To find the first 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. ControlHow to RepeatProcedureA procedure takes input in, does some work on these inputs and produces outputs as results. The syntax for a procedure is::
For instance the built-in operator The following procedure finds the first occurrence of html link and outputs that link as well as its end position:
What a procedure outputs is defined with 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 DecisionsIf one needs to make comparisons there are different operators for doing comparisons:
The output is a Boolean-value which is either A decision in code is made with the if statement. The syntax for an if test is first the keyword
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 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.
With a very few simple operation its possible to simulate any other machine:
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. LoopsLoops are s a way of doing things over and over. It syntax is just
This enables for instance to count:: i = 0 while i != 10: i += 1 print(i)
BreakBreak allows to break out of a loop. while testExpression:
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 EncodingIf different characters are use with the code (comments, strings, etc.), such as é, í, ç or Chinese/Japanese chars one needs to add:
on the beginning of the file [http://www.python.org/dev/peps/pep-0263/]. Structured DataA 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. AliasingAliasing is haven multiple ways to refer the the same object. Consider this example::
Mutation is visible through any reference to the same list object.
Storing Data1 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 LoopsThe structure of a forloop is like this. There is the keyword for followed by a name for a variable
IndexThe 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.
If the in
Pop
Tags: coding Edit this page |
|